FileHandlerTest.php 5.9 KB

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