FileRemovalTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * @var bool
  33. */
  34. private static $removeFilesOnTearDown = true;
  35. public static function tearDownAfterClass(): void
  36. {
  37. if (self::$removeFilesOnTearDown) {
  38. @unlink(sys_get_temp_dir().'/cs_fixer_foo.php');
  39. @unlink(sys_get_temp_dir().'/cs_fixer_bar.php');
  40. }
  41. }
  42. public function testCleanRemovesObservedFiles(): void
  43. {
  44. $fs = $this->getMockFileSystem();
  45. $fileRemoval = new FileRemoval();
  46. $fileRemoval->observe($fs->url().'/foo.php');
  47. $fileRemoval->observe($fs->url().'/baz.php');
  48. $fileRemoval->clean();
  49. self::assertFileDoesNotExist($fs->url().'/foo.php');
  50. self::assertFileDoesNotExist($fs->url().'/baz.php');
  51. self::assertFileExists($fs->url().'/bar.php');
  52. }
  53. public function testDestructRemovesObservedFiles(): void
  54. {
  55. $fs = $this->getMockFileSystem();
  56. $fileRemoval = new FileRemoval();
  57. $fileRemoval->observe($fs->url().'/foo.php');
  58. $fileRemoval->observe($fs->url().'/baz.php');
  59. $fileRemoval->__destruct();
  60. self::assertFileDoesNotExist($fs->url().'/foo.php');
  61. self::assertFileDoesNotExist($fs->url().'/baz.php');
  62. self::assertFileExists($fs->url().'/bar.php');
  63. }
  64. public function testDeleteObservedFile(): void
  65. {
  66. $fs = $this->getMockFileSystem();
  67. $fileRemoval = new FileRemoval();
  68. $fileRemoval->observe($fs->url().'/foo.php');
  69. $fileRemoval->observe($fs->url().'/baz.php');
  70. $fileRemoval->delete($fs->url().'/foo.php');
  71. self::assertFileDoesNotExist($fs->url().'/foo.php');
  72. self::assertFileExists($fs->url().'/baz.php');
  73. }
  74. public function testDeleteNonObservedFile(): void
  75. {
  76. $fs = $this->getMockFileSystem();
  77. $fileRemoval = new FileRemoval();
  78. $fileRemoval->delete($fs->url().'/foo.php');
  79. self::assertFileDoesNotExist($fs->url().'/foo.php');
  80. }
  81. public function testSleep(): void
  82. {
  83. $this->expectException(\BadMethodCallException::class);
  84. $this->expectExceptionMessage('Cannot serialize PhpCsFixer\FileRemoval');
  85. $fileRemoval = new FileRemoval();
  86. $fileRemoval->__sleep();
  87. }
  88. public function testWakeup(): void
  89. {
  90. $this->expectException(\BadMethodCallException::class);
  91. $this->expectExceptionMessage('Cannot unserialize PhpCsFixer\FileRemoval');
  92. $fileRemoval = new FileRemoval();
  93. $fileRemoval->__wakeup();
  94. }
  95. /**
  96. * Must NOT be run as first test, see https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/7104.
  97. *
  98. * @runInSeparateProcess
  99. *
  100. * @preserveGlobalState disabled
  101. *
  102. * @doesNotPerformAssertions
  103. */
  104. public function testShutdownRemovesObservedFilesSetup(): void
  105. {
  106. self::$removeFilesOnTearDown = false;
  107. $fileToBeDeleted = sys_get_temp_dir().'/cs_fixer_foo.php';
  108. $fileNotToBeDeleted = sys_get_temp_dir().'/cs_fixer_bar.php';
  109. file_put_contents($fileToBeDeleted, '');
  110. file_put_contents($fileNotToBeDeleted, '');
  111. $fileRemoval = new FileRemoval();
  112. $fileRemoval->observe($fileToBeDeleted);
  113. }
  114. /**
  115. * @depends testShutdownRemovesObservedFilesSetup
  116. */
  117. public function testShutdownRemovesObservedFiles(): void
  118. {
  119. self::assertFileDoesNotExist(sys_get_temp_dir().'/cs_fixer_foo.php');
  120. self::assertFileExists(sys_get_temp_dir().'/cs_fixer_bar.php');
  121. }
  122. private function getMockFileSystem(): vfsStreamDirectory
  123. {
  124. return vfsStream::setup('root', null, [
  125. 'foo.php' => '',
  126. 'bar.php' => '',
  127. 'baz.php' => '',
  128. ]);
  129. }
  130. }