FileReaderTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests;
  13. use org\bovigo\vfs\vfsStream;
  14. use PhpCsFixer\FileReader;
  15. /**
  16. * @author ntzm
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\FileReader
  21. */
  22. final class FileReaderTest extends TestCase
  23. {
  24. public static function tearDownAfterClass(): void
  25. {
  26. parent::tearDownAfterClass();
  27. // testReadStdinCaches registers a stream wrapper for PHP so we can mock
  28. // php://stdin. Restore the original stream wrapper after this class so
  29. // we don't affect other tests running after it
  30. stream_wrapper_restore('php');
  31. }
  32. public function testCreateSingleton(): void
  33. {
  34. $instance = FileReader::createSingleton();
  35. self::assertSame($instance, FileReader::createSingleton());
  36. }
  37. public function testRead(): void
  38. {
  39. $fs = vfsStream::setup('root', null, [
  40. 'foo.php' => '<?php echo "hi";',
  41. ]);
  42. $reader = new FileReader();
  43. self::assertSame('<?php echo "hi";', $reader->read($fs->url().'/foo.php'));
  44. }
  45. public function testReadStdinCaches(): void
  46. {
  47. $reader = new FileReader();
  48. $stdinStream = $this->createStdinStreamDouble();
  49. stream_wrapper_unregister('php');
  50. stream_wrapper_register('php', \get_class($stdinStream));
  51. self::assertSame('<?php echo "foo";', $reader->read('php://stdin'));
  52. self::assertSame('<?php echo "foo";', $reader->read('php://stdin'));
  53. }
  54. public function testThrowsExceptionOnFail(): void
  55. {
  56. $fs = vfsStream::setup();
  57. $nonExistentFilePath = $fs->url().'/non-existent.php';
  58. $reader = new FileReader();
  59. $this->expectException(\RuntimeException::class);
  60. $this->expectExceptionMessageMatches('#^Failed to read content from "'.preg_quote($nonExistentFilePath, '#').'.*$#');
  61. $reader->read($nonExistentFilePath);
  62. }
  63. private function createStdinStreamDouble(): object
  64. {
  65. return new class {
  66. /**
  67. * @var resource
  68. */
  69. public $context;
  70. private static bool $hasReadContent = false;
  71. private string $content = '<?php echo "foo";';
  72. private bool $hasReadCurrentString = false;
  73. public function stream_open(string $path): bool
  74. {
  75. return 'php://stdin' === $path;
  76. }
  77. /**
  78. * @return false|string
  79. */
  80. public function stream_read()
  81. {
  82. if ($this->stream_eof()) {
  83. return false;
  84. }
  85. $this->hasReadCurrentString = true;
  86. if (self::$hasReadContent) {
  87. return '';
  88. }
  89. self::$hasReadContent = true;
  90. return $this->content;
  91. }
  92. public function stream_eof(): bool
  93. {
  94. return $this->hasReadCurrentString;
  95. }
  96. /**
  97. * @return array{}
  98. */
  99. public function stream_stat(): array
  100. {
  101. return [];
  102. }
  103. };
  104. }
  105. }