CacheTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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\Signature;
  14. use PhpCsFixer\Cache\SignatureInterface;
  15. use PhpCsFixer\ToolInfo;
  16. use PHPUnit\Framework\TestCase;
  17. /**
  18. * @author Andreas Möller <am@localheinz.com>
  19. *
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\Cache\Cache
  23. */
  24. final class CacheTest extends TestCase
  25. {
  26. public function testIsFinal()
  27. {
  28. $reflection = new \ReflectionClass(\PhpCsFixer\Cache\Cache::class);
  29. $this->assertTrue($reflection->isFinal());
  30. }
  31. public function testImplementsCacheInterface()
  32. {
  33. $reflection = new \ReflectionClass(\PhpCsFixer\Cache\Cache::class);
  34. $this->assertTrue($reflection->implementsInterface(\PhpCsFixer\Cache\CacheInterface::class));
  35. }
  36. public function testConstructorSetsValues()
  37. {
  38. $signature = $this->getSignatureDouble();
  39. $cache = new Cache($signature);
  40. $this->assertSame($signature, $cache->getSignature());
  41. }
  42. public function testDefaults()
  43. {
  44. $signature = $this->getSignatureDouble();
  45. $cache = new Cache($signature);
  46. $file = 'test.php';
  47. $this->assertFalse($cache->has($file));
  48. $this->assertNull($cache->get($file));
  49. }
  50. public function testSetThrowsInvalidArgumentExceptionIfValueIsNotAnInteger()
  51. {
  52. $this->expectException(\InvalidArgumentException::class);
  53. $signature = $this->getSignatureDouble();
  54. $cache = new Cache($signature);
  55. $file = 'test.php';
  56. $cache->set($file, null);
  57. }
  58. public function testCanSetAndGetValue()
  59. {
  60. $signature = $this->getSignatureDouble();
  61. $cache = new Cache($signature);
  62. $file = 'test.php';
  63. $hash = crc32('hello');
  64. $cache->set($file, $hash);
  65. $this->assertTrue($cache->has($file));
  66. $this->assertSame($hash, $cache->get($file));
  67. }
  68. public function testCanClearValue()
  69. {
  70. $signature = $this->getSignatureDouble();
  71. $cache = new Cache($signature);
  72. $file = 'test.php';
  73. $hash = crc32('hello');
  74. $cache->set($file, $hash);
  75. $cache->clear($file);
  76. $this->assertNull($cache->get($file));
  77. }
  78. public function testFromJsonThrowsInvalidArgumentExceptionIfJsonIsInvalid()
  79. {
  80. $this->expectException(\InvalidArgumentException::class);
  81. $json = '{"foo';
  82. Cache::fromJson($json);
  83. }
  84. /**
  85. * @dataProvider provideMissingDataCases
  86. *
  87. * @param array $data
  88. */
  89. public function testFromJsonThrowsInvalidArgumentExceptionIfJsonIsMissingKey(array $data)
  90. {
  91. $this->expectException(\InvalidArgumentException::class);
  92. $json = json_encode($data);
  93. Cache::fromJson($json);
  94. }
  95. /**
  96. * @return array
  97. */
  98. public function provideMissingDataCases()
  99. {
  100. $data = [
  101. 'php' => '7.1.2',
  102. 'version' => '2.0',
  103. 'rules' => [
  104. 'foo' => true,
  105. 'bar' => false,
  106. ],
  107. 'hashes' => [],
  108. ];
  109. return array_map(function ($missingKey) use ($data) {
  110. unset($data[$missingKey]);
  111. return [
  112. $data,
  113. ];
  114. }, array_keys($data));
  115. }
  116. /**
  117. * @dataProvider provideCanConvertToAndFromJsonCases
  118. */
  119. public function testCanConvertToAndFromJson(SignatureInterface $signature)
  120. {
  121. $cache = new Cache($signature);
  122. $file = 'test.php';
  123. $hash = crc32('hello');
  124. $cache->set($file, $hash);
  125. $cached = Cache::fromJson($cache->toJson());
  126. $this->assertTrue($cached->getSignature()->equals($signature));
  127. $this->assertTrue($cached->has($file));
  128. $this->assertSame($hash, $cached->get($file));
  129. }
  130. public function provideCanConvertToAndFromJsonCases()
  131. {
  132. $toolInfo = new ToolInfo();
  133. return [
  134. [new Signature(
  135. PHP_VERSION,
  136. '2.0',
  137. [
  138. 'foo' => true,
  139. 'bar' => true,
  140. ]
  141. )],
  142. [new Signature(
  143. PHP_VERSION,
  144. $toolInfo->getVersion(),
  145. [
  146. // value encoded in ANSI, not UTF
  147. 'header_comment' => ['header' => 'Dariusz '.base64_decode('UnVtafFza2k=', true)],
  148. ]
  149. )],
  150. ];
  151. }
  152. /**
  153. * @return SignatureInterface
  154. */
  155. private function getSignatureDouble()
  156. {
  157. return $this->prophesize(\PhpCsFixer\Cache\SignatureInterface::class)->reveal();
  158. }
  159. }