FileReaderTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. use PhpCsFixer\Tests\Fixtures\Test\FileReaderTest\StdinFakeStream;
  16. /**
  17. * @author ntzm
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\FileReader
  22. */
  23. final class FileReaderTest extends TestCase
  24. {
  25. public static function tearDownAfterClass(): void
  26. {
  27. parent::tearDownAfterClass();
  28. // testReadStdinCaches registers a stream wrapper for PHP so we can mock
  29. // php://stdin. Restore the original stream wrapper after this class so
  30. // we don't affect other tests running after it
  31. stream_wrapper_restore('php');
  32. }
  33. public function testCreateSingleton(): void
  34. {
  35. $instance = FileReader::createSingleton();
  36. self::assertSame($instance, FileReader::createSingleton());
  37. }
  38. public function testRead(): void
  39. {
  40. $fs = vfsStream::setup('root', null, [
  41. 'foo.php' => '<?php echo "hi";',
  42. ]);
  43. $reader = new FileReader();
  44. self::assertSame('<?php echo "hi";', $reader->read($fs->url().'/foo.php'));
  45. }
  46. public function testReadStdinCaches(): void
  47. {
  48. $reader = new FileReader();
  49. stream_wrapper_unregister('php');
  50. stream_wrapper_register('php', StdinFakeStream::class);
  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. }