FileRemovalTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\FileRemoval;
  14. /**
  15. * @author ntzm
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\FileRemoval
  20. */
  21. final class FileRemovalTest extends TestCase
  22. {
  23. /**
  24. * Should temporary files be removed on tear down?
  25. *
  26. * This is necessary for testShutdownRemovesObserved files, as the setup
  27. * runs in a separate process to trigger the shutdown function, and
  28. * tearDownAfterClass is called for every separate process
  29. *
  30. * @var bool
  31. */
  32. private static $removeFilesOnTearDown = true;
  33. public static function tearDownAfterClass()
  34. {
  35. if (self::$removeFilesOnTearDown) {
  36. @unlink(sys_get_temp_dir().'/cs_fixer_foo.php');
  37. @unlink(sys_get_temp_dir().'/cs_fixer_bar.php');
  38. }
  39. }
  40. /**
  41. * @runInSeparateProcess
  42. * @doesNotPerformAssertions
  43. */
  44. public function testShutdownRemovesObservedFilesSetup()
  45. {
  46. self::$removeFilesOnTearDown = false;
  47. $fileToBeDeleted = sys_get_temp_dir().'/cs_fixer_foo.php';
  48. $fileNotToBeDeleted = sys_get_temp_dir().'/cs_fixer_bar.php';
  49. file_put_contents($fileToBeDeleted, '');
  50. file_put_contents($fileNotToBeDeleted, '');
  51. $fileRemoval = new FileRemoval();
  52. $fileRemoval->observe($fileToBeDeleted);
  53. }
  54. /**
  55. * @depends testShutdownRemovesObservedFilesSetup
  56. */
  57. public function testShutdownRemovesObservedFiles()
  58. {
  59. $this->assertFileNotExists(sys_get_temp_dir().'/cs_fixer_foo.php');
  60. $this->assertFileExists(sys_get_temp_dir().'/cs_fixer_bar.php');
  61. }
  62. public function testCleanRemovesObservedFiles()
  63. {
  64. $fs = $this->getMockFileSystem();
  65. $fileRemoval = new FileRemoval();
  66. $fileRemoval->observe($fs->url().'/foo.php');
  67. $fileRemoval->observe($fs->url().'/baz.php');
  68. $fileRemoval->clean();
  69. $this->assertFileNotExists($fs->url().'/foo.php');
  70. $this->assertFileNotExists($fs->url().'/baz.php');
  71. $this->assertFileExists($fs->url().'/bar.php');
  72. }
  73. public function testDestructRemovesObservedFiles()
  74. {
  75. $fs = $this->getMockFileSystem();
  76. $fileRemoval = new FileRemoval();
  77. $fileRemoval->observe($fs->url().'/foo.php');
  78. $fileRemoval->observe($fs->url().'/baz.php');
  79. $fileRemoval->__destruct();
  80. $this->assertFileNotExists($fs->url().'/foo.php');
  81. $this->assertFileNotExists($fs->url().'/baz.php');
  82. $this->assertFileExists($fs->url().'/bar.php');
  83. }
  84. public function testDeleteObservedFile()
  85. {
  86. $fs = $this->getMockFileSystem();
  87. $fileRemoval = new FileRemoval();
  88. $fileRemoval->observe($fs->url().'/foo.php');
  89. $fileRemoval->observe($fs->url().'/baz.php');
  90. $fileRemoval->delete($fs->url().'/foo.php');
  91. $this->assertFileNotExists($fs->url().'/foo.php');
  92. $this->assertFileExists($fs->url().'/baz.php');
  93. }
  94. public function testDeleteNonObservedFile()
  95. {
  96. $fs = $this->getMockFileSystem();
  97. $fileRemoval = new FileRemoval();
  98. $fileRemoval->delete($fs->url().'/foo.php');
  99. $this->assertFileNotExists($fs->url().'/foo.php');
  100. }
  101. private function getMockFileSystem()
  102. {
  103. return vfsStream::setup('root', null, [
  104. 'foo.php' => '',
  105. 'bar.php' => '',
  106. 'baz.php' => '',
  107. ]);
  108. }
  109. }