FileRemovalTest.php 4.3 KB

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