FileRemovalTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 org\bovigo\vfs\vfsStreamDirectory;
  15. use PhpCsFixer\FileRemoval;
  16. /**
  17. * @author ntzm
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\FileRemoval
  22. */
  23. final class FileRemovalTest extends TestCase
  24. {
  25. /**
  26. * Should temporary files be removed on tear down?
  27. *
  28. * This is necessary for testShutdownRemovesObserved files, as the setup
  29. * runs in a separate process to trigger the shutdown function, and
  30. * tearDownAfterClass is called for every separate process
  31. */
  32. private static bool $removeFilesOnTearDown = true;
  33. public static function tearDownAfterClass(): void
  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. public function testCleanRemovesObservedFiles(): void
  41. {
  42. $fs = $this->getMockFileSystem();
  43. $fileRemoval = new FileRemoval();
  44. $fileRemoval->observe($fs->url().'/foo.php');
  45. $fileRemoval->observe($fs->url().'/baz.php');
  46. $fileRemoval->clean();
  47. self::assertFileDoesNotExist($fs->url().'/foo.php');
  48. self::assertFileDoesNotExist($fs->url().'/baz.php');
  49. self::assertFileExists($fs->url().'/bar.php');
  50. }
  51. public function testDestructRemovesObservedFiles(): void
  52. {
  53. $fs = $this->getMockFileSystem();
  54. $fileRemoval = new FileRemoval();
  55. $fileRemoval->observe($fs->url().'/foo.php');
  56. $fileRemoval->observe($fs->url().'/baz.php');
  57. $fileRemoval->__destruct();
  58. self::assertFileDoesNotExist($fs->url().'/foo.php');
  59. self::assertFileDoesNotExist($fs->url().'/baz.php');
  60. self::assertFileExists($fs->url().'/bar.php');
  61. }
  62. public function testDeleteObservedFile(): void
  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->delete($fs->url().'/foo.php');
  69. self::assertFileDoesNotExist($fs->url().'/foo.php');
  70. self::assertFileExists($fs->url().'/baz.php');
  71. }
  72. public function testDeleteNonObservedFile(): void
  73. {
  74. $fs = $this->getMockFileSystem();
  75. $fileRemoval = new FileRemoval();
  76. $fileRemoval->delete($fs->url().'/foo.php');
  77. self::assertFileDoesNotExist($fs->url().'/foo.php');
  78. }
  79. public function testSleep(): void
  80. {
  81. $this->expectException(\BadMethodCallException::class);
  82. $this->expectExceptionMessage('Cannot serialize PhpCsFixer\FileRemoval');
  83. $fileRemoval = new FileRemoval();
  84. $fileRemoval->__sleep();
  85. }
  86. public function testWakeup(): void
  87. {
  88. $this->expectException(\BadMethodCallException::class);
  89. $this->expectExceptionMessage('Cannot unserialize PhpCsFixer\FileRemoval');
  90. $fileRemoval = new FileRemoval();
  91. $fileRemoval->__wakeup();
  92. }
  93. /**
  94. * Must NOT be run as first test, see https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/7104.
  95. *
  96. * @runInSeparateProcess
  97. *
  98. * @preserveGlobalState disabled
  99. *
  100. * @doesNotPerformAssertions
  101. */
  102. public function testShutdownRemovesObservedFilesSetup(): void
  103. {
  104. self::$removeFilesOnTearDown = false;
  105. $fileToBeDeleted = sys_get_temp_dir().'/cs_fixer_foo.php';
  106. $fileNotToBeDeleted = sys_get_temp_dir().'/cs_fixer_bar.php';
  107. file_put_contents($fileToBeDeleted, '');
  108. file_put_contents($fileNotToBeDeleted, '');
  109. $fileRemoval = new FileRemoval();
  110. $fileRemoval->observe($fileToBeDeleted);
  111. }
  112. /**
  113. * @depends testShutdownRemovesObservedFilesSetup
  114. */
  115. public function testShutdownRemovesObservedFiles(): void
  116. {
  117. self::assertFileDoesNotExist(sys_get_temp_dir().'/cs_fixer_foo.php');
  118. self::assertFileExists(sys_get_temp_dir().'/cs_fixer_bar.php');
  119. }
  120. private function getMockFileSystem(): vfsStreamDirectory
  121. {
  122. return vfsStream::setup('root', null, [
  123. 'foo.php' => '',
  124. 'bar.php' => '',
  125. 'baz.php' => '',
  126. ]);
  127. }
  128. }