FileHandlerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 PHPUnit\Framework\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->setExpectedExceptionRegExp(\Symfony\Component\Filesystem\Exception\IOException::class, sprintf(
  80. '#^Failed to write file "%s"(, ".*")?.#',
  81. preg_quote($file, '#')
  82. ));
  83. $cache = new Cache(new Signature(
  84. PHP_VERSION,
  85. '2.0',
  86. [
  87. 'foo',
  88. 'bar',
  89. ]
  90. ));
  91. $handler = new FileHandler($file);
  92. $handler->write($cache);
  93. }
  94. public function testWriteWritesCache()
  95. {
  96. $file = $this->getFile();
  97. $cache = new Cache(new Signature(
  98. PHP_VERSION,
  99. '2.0',
  100. [
  101. 'foo',
  102. 'bar',
  103. ]
  104. ));
  105. $handler = new FileHandler($file);
  106. $handler->write($cache);
  107. $this->assertFileExists($file);
  108. $actualCacheJson = file_get_contents($file);
  109. $this->assertSame($cache->toJson(), $actualCacheJson);
  110. }
  111. /**
  112. * @return string
  113. */
  114. private function getFile()
  115. {
  116. return __DIR__.'/.php_cs.cache';
  117. }
  118. }