FileReaderTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests;
  12. use org\bovigo\vfs\vfsStream;
  13. use PhpCsFixer\FileReader;
  14. use PhpCsFixer\Tests\Fixtures\Test\FileReaderTest\StdinFakeStream;
  15. /**
  16. * @author ntzm
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\FileReader
  21. */
  22. final class FileReaderTest extends TestCase
  23. {
  24. public static function doTearDownAfterClass()
  25. {
  26. parent::doTearDownAfterClass();
  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()
  33. {
  34. $instance = FileReader::createSingleton();
  35. static::assertInstanceOf(\PhpCsFixer\FileReader::class, $instance);
  36. static::assertSame($instance, FileReader::createSingleton());
  37. }
  38. public function testRead()
  39. {
  40. $fs = vfsStream::setup('root', null, [
  41. 'foo.php' => '<?php echo "hi";',
  42. ]);
  43. $reader = new FileReader();
  44. static::assertSame('<?php echo "hi";', $reader->read($fs->url().'/foo.php'));
  45. }
  46. public function testReadStdinCaches()
  47. {
  48. $reader = new FileReader();
  49. stream_wrapper_unregister('php');
  50. stream_wrapper_register('php', StdinFakeStream::class);
  51. static::assertSame('<?php echo "foo";', $reader->read('php://stdin'));
  52. static::assertSame('<?php echo "foo";', $reader->read('php://stdin'));
  53. }
  54. public function testThrowsExceptionOnFail()
  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. }