FileRemovalTest.php 4.3 KB

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