FileHandlerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Cache;
  12. use PhpCsFixer\Cache\Cache;
  13. use PhpCsFixer\Cache\FileHandler;
  14. use PhpCsFixer\Cache\Signature;
  15. use PhpCsFixer\Tests\TestCase;
  16. /**
  17. * @author Andreas Möller <am@localheinz.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Cache\FileHandler
  22. */
  23. final class FileHandlerTest extends TestCase
  24. {
  25. protected function tearDown()
  26. {
  27. parent::tearDown();
  28. $file = $this->getFile();
  29. if (file_exists($file)) {
  30. unlink($file);
  31. }
  32. }
  33. public function testImplementsHandlerInterface()
  34. {
  35. $file = $this->getFile();
  36. $handler = new FileHandler($file);
  37. $this->assertInstanceOf(\PhpCsFixer\Cache\FileHandlerInterface::class, $handler);
  38. }
  39. public function testConstructorSetsFile()
  40. {
  41. $file = $this->getFile();
  42. $handler = new FileHandler($file);
  43. $this->assertSame($file, $handler->getFile());
  44. }
  45. public function testReadReturnsNullIfFileDoesNotExist()
  46. {
  47. $file = $this->getFile();
  48. $handler = new FileHandler($file);
  49. $this->assertNull($handler->read());
  50. }
  51. public function testReadReturnsNullIfContentCanNotBeDeserialized()
  52. {
  53. $file = $this->getFile();
  54. file_put_contents($file, 'hello');
  55. $handler = new FileHandler($file);
  56. $this->assertNull($handler->read());
  57. }
  58. public function testReadReturnsCache()
  59. {
  60. $file = $this->getFile();
  61. $signature = new Signature(
  62. PHP_VERSION,
  63. '2.0',
  64. [
  65. 'foo',
  66. 'bar',
  67. ]
  68. );
  69. $cache = new Cache($signature);
  70. file_put_contents($file, $cache->toJson());
  71. $handler = new FileHandler($file);
  72. $cached = $handler->read();
  73. $this->assertInstanceOf(\PhpCsFixer\Cache\CacheInterface::class, $cached);
  74. $this->assertTrue($cached->getSignature()->equals($signature));
  75. }
  76. public function testWriteThrowsIOExceptionIfFileCanNotBeWritten()
  77. {
  78. $file = __DIR__.'/non-existent-directory/.php_cs.cache';
  79. $this->expectException(\Symfony\Component\Filesystem\Exception\IOException::class);
  80. $this->expectExceptionMessageRegExp(sprintf(
  81. '#^Failed to write file "%s"(, ".*")?.#',
  82. preg_quote($file, '#')
  83. ));
  84. $cache = new Cache(new Signature(
  85. PHP_VERSION,
  86. '2.0',
  87. [
  88. 'foo',
  89. 'bar',
  90. ]
  91. ));
  92. $handler = new FileHandler($file);
  93. $handler->write($cache);
  94. }
  95. public function testWriteWritesCache()
  96. {
  97. $file = $this->getFile();
  98. $cache = new Cache(new Signature(
  99. PHP_VERSION,
  100. '2.0',
  101. [
  102. 'foo',
  103. 'bar',
  104. ]
  105. ));
  106. $handler = new FileHandler($file);
  107. $handler->write($cache);
  108. $this->assertFileExists($file);
  109. $actualCacheJson = file_get_contents($file);
  110. $this->assertSame($cache->toJson(), $actualCacheJson);
  111. }
  112. public function testWriteCacheToDirectory()
  113. {
  114. $dir = __DIR__.'/../Fixtures/cache-file-handler';
  115. $handler = new FileHandler($dir);
  116. $this->expectException(\Symfony\Component\Filesystem\Exception\IOException::class);
  117. $this->expectExceptionMessageRegExp(sprintf(
  118. '#^%s$#',
  119. preg_quote('Cannot write cache file "'.realpath($dir).'" as the location exists as directory.', '#')
  120. ));
  121. $handler->write(new Cache(new Signature(
  122. PHP_VERSION,
  123. '2.0',
  124. [
  125. 'foo',
  126. 'bar',
  127. ]
  128. )));
  129. }
  130. public function testWriteCacheToNonWriteableFile()
  131. {
  132. $file = __DIR__.'/../Fixtures/cache-file-handler/cache-file';
  133. if (is_writable($file)) {
  134. $this->markTestSkipped(sprintf('File "%s" must be not writeable for this tests.', realpath($file)));
  135. return;
  136. }
  137. $handler = new FileHandler($file);
  138. $this->expectException(\Symfony\Component\Filesystem\Exception\IOException::class);
  139. $this->expectExceptionMessageRegExp(sprintf(
  140. '#^%s$#',
  141. preg_quote('Cannot write to file "'.realpath($file).'" as it is not writable.', '#')
  142. ));
  143. $handler->write(new Cache(new Signature(
  144. PHP_VERSION,
  145. '2.0',
  146. [
  147. 'foo',
  148. 'bar',
  149. ]
  150. )));
  151. }
  152. public function testWriteCacheFilePermissions()
  153. {
  154. $file = __DIR__.'/../Fixtures/cache-file-handler/rw_cache.test';
  155. @unlink($file);
  156. $this->assertFileNotExists($file);
  157. $handler = new FileHandler($file);
  158. $handler->write(new Cache(new Signature(
  159. PHP_VERSION,
  160. '2.0',
  161. [
  162. 'foo',
  163. 'bar',
  164. ]
  165. )));
  166. $this->assertFileExists($file);
  167. $this->assertTrue(@is_file($file), sprintf('Failed cache "%s" `is_file`.', $file));
  168. $this->assertTrue(@is_writable($file), sprintf('Failed cache "%s" `is_writable`.', $file));
  169. $this->assertTrue(@is_readable($file), sprintf('Failed cache "%s" `is_readable`.', $file));
  170. @unlink($file);
  171. }
  172. /**
  173. * @return string
  174. */
  175. private function getFile()
  176. {
  177. return __DIR__.'/.php_cs.cache';
  178. }
  179. }