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. * @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. * @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. }