FileHandlerTest.php 5.3 KB

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