FileHandlerTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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\Cache;
  13. use PhpCsFixer\Cache\Cache;
  14. use PhpCsFixer\Cache\CacheInterface;
  15. use PhpCsFixer\Cache\FileHandler;
  16. use PhpCsFixer\Cache\Signature;
  17. use PhpCsFixer\Cache\SignatureInterface;
  18. use PhpCsFixer\Tests\TestCase;
  19. use Symfony\Component\Filesystem\Exception\IOException;
  20. /**
  21. * @author Andreas Möller <am@localheinz.com>
  22. *
  23. * @internal
  24. *
  25. * @covers \PhpCsFixer\Cache\FileHandler
  26. */
  27. final class FileHandlerTest extends TestCase
  28. {
  29. protected function tearDown(): void
  30. {
  31. parent::tearDown();
  32. $file = $this->getFile();
  33. if (file_exists($file)) {
  34. unlink($file);
  35. }
  36. }
  37. public function testConstructorSetsFile(): void
  38. {
  39. $file = $this->getFile();
  40. $handler = new FileHandler($file);
  41. self::assertSame($file, $handler->getFile());
  42. }
  43. public function testReadReturnsNullIfFileDoesNotExist(): void
  44. {
  45. $file = $this->getFile();
  46. $handler = new FileHandler($file);
  47. self::assertNull($handler->read());
  48. }
  49. public function testReadReturnsNullIfContentCanNotBeDeserialized(): void
  50. {
  51. $file = $this->getFile();
  52. file_put_contents($file, 'hello');
  53. $handler = new FileHandler($file);
  54. self::assertNull($handler->read());
  55. }
  56. public function testReadReturnsCache(): void
  57. {
  58. $file = $this->getFile();
  59. $signature = $this->createSignature();
  60. $cache = new Cache($signature);
  61. file_put_contents($file, $cache->toJson());
  62. $handler = new FileHandler($file);
  63. $cached = $handler->read();
  64. self::assertInstanceOf(CacheInterface::class, $cached);
  65. self::assertTrue($cached->getSignature()->equals($signature));
  66. }
  67. public function testWriteThrowsIOExceptionIfFileCanNotBeWritten(): void
  68. {
  69. $file = '/../"/out/of/range/cache.json'; // impossible path
  70. $this->expectException(IOException::class);
  71. $this->expectExceptionMessageMatches(\sprintf(
  72. '#^Directory of cache file "%s" does not exists and couldn\'t be created\.#',
  73. preg_quote($file, '#')
  74. ));
  75. $cache = new Cache($this->createSignature());
  76. $handler = new FileHandler($file);
  77. $handler->write($cache);
  78. }
  79. public function testWriteWritesCache(): void
  80. {
  81. $file = $this->getFile();
  82. $cache = new Cache($this->createSignature());
  83. $handler = new FileHandler($file);
  84. $handler->write($cache);
  85. self::assertFileExists($file);
  86. $actualCacheJson = file_get_contents($file);
  87. self::assertSame($cache->toJson(), $actualCacheJson);
  88. }
  89. public function testWriteCacheToDirectory(): void
  90. {
  91. $dir = __DIR__.'/../Fixtures/cache-file-handler';
  92. $handler = new FileHandler($dir);
  93. $this->expectException(IOException::class);
  94. $this->expectExceptionMessageMatches(\sprintf(
  95. '#^%s$#',
  96. preg_quote('Cannot write cache file "'.realpath($dir).'" as the location exists as directory.', '#')
  97. ));
  98. $handler->write(new Cache($this->createSignature()));
  99. }
  100. public function testWriteCacheToNonWriteableFile(): void
  101. {
  102. $file = __DIR__.'/../Fixtures/cache-file-handler/cache-file';
  103. if (is_writable($file)) {
  104. self::markTestSkipped(\sprintf('File "%s" must be not writeable for this tests.', realpath($file)));
  105. }
  106. $handler = new FileHandler($file);
  107. $this->expectException(IOException::class);
  108. $this->expectExceptionMessageMatches(\sprintf(
  109. '#^%s$#',
  110. preg_quote('Cannot write to file "'.realpath($file).'" as it is not writable.', '#')
  111. ));
  112. $handler->write(new Cache($this->createSignature()));
  113. }
  114. public function testWriteCacheFilePermissions(): void
  115. {
  116. $file = __DIR__.'/../Fixtures/cache-file-handler/rw_cache.test';
  117. @unlink($file);
  118. self::assertFileDoesNotExist($file);
  119. $handler = new FileHandler($file);
  120. $handler->write(new Cache($this->createSignature()));
  121. self::assertFileExists($file);
  122. self::assertTrue(@is_file($file), \sprintf('Failed cache "%s" `is_file`.', $file));
  123. self::assertTrue(@is_writable($file), \sprintf('Failed cache "%s" `is_writable`.', $file));
  124. self::assertTrue(@is_readable($file), \sprintf('Failed cache "%s" `is_readable`.', $file));
  125. @unlink($file);
  126. }
  127. public function testCachePathIsCreated(): void
  128. {
  129. $dir = __DIR__.'/../Fixtures/cache-file-handler/one/two/three';
  130. $file = $dir.'/cache.json';
  131. $cleanPath = static function () use ($dir, $file): void {
  132. @unlink($file);
  133. for ($i = 0; $i <= 2; ++$i) {
  134. @rmdir(0 === $i ? $dir : \dirname($dir, $i));
  135. }
  136. };
  137. $cleanPath();
  138. self::assertDirectoryDoesNotExist($dir);
  139. self::assertFileDoesNotExist($file);
  140. $handler = new FileHandler($file);
  141. $handler->write(new Cache($this->createSignature()));
  142. self::assertDirectoryExists($dir);
  143. self::assertFileExists($file);
  144. $cleanPath();
  145. }
  146. private function getFile(): string
  147. {
  148. return __DIR__.'/.php-cs-fixer.cache';
  149. }
  150. private function createSignature(): SignatureInterface
  151. {
  152. return new Signature(
  153. PHP_VERSION,
  154. '2.0',
  155. ' ',
  156. PHP_EOL,
  157. ['foo' => true, 'bar' => false],
  158. );
  159. }
  160. }