TraceableCacheTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Tests\Simple;
  11. use Symfony\Component\Cache\Simple\FilesystemCache;
  12. use Symfony\Component\Cache\Simple\TraceableCache;
  13. /**
  14. * @group time-sensitive
  15. * @group legacy
  16. */
  17. class TraceableCacheTest extends CacheTestCase
  18. {
  19. protected $skippedTests = [
  20. 'testPrune' => 'TraceableCache just proxies',
  21. ];
  22. public function createSimpleCache($defaultLifetime = 0)
  23. {
  24. return new TraceableCache(new FilesystemCache('', $defaultLifetime));
  25. }
  26. public function testGetMissTrace()
  27. {
  28. $pool = $this->createSimpleCache();
  29. $pool->get('k');
  30. $calls = $pool->getCalls();
  31. $this->assertCount(1, $calls);
  32. $call = $calls[0];
  33. $this->assertSame('get', $call->name);
  34. $this->assertSame(['k' => false], $call->result);
  35. $this->assertSame(0, $call->hits);
  36. $this->assertSame(1, $call->misses);
  37. $this->assertNotEmpty($call->start);
  38. $this->assertNotEmpty($call->end);
  39. }
  40. public function testGetHitTrace()
  41. {
  42. $pool = $this->createSimpleCache();
  43. $pool->set('k', 'foo');
  44. $pool->get('k');
  45. $calls = $pool->getCalls();
  46. $this->assertCount(2, $calls);
  47. $call = $calls[1];
  48. $this->assertSame(1, $call->hits);
  49. $this->assertSame(0, $call->misses);
  50. }
  51. public function testGetMultipleMissTrace()
  52. {
  53. $pool = $this->createSimpleCache();
  54. $pool->set('k1', 123);
  55. $values = $pool->getMultiple(['k0', 'k1']);
  56. foreach ($values as $value) {
  57. }
  58. $calls = $pool->getCalls();
  59. $this->assertCount(2, $calls);
  60. $call = $calls[1];
  61. $this->assertSame('getMultiple', $call->name);
  62. $this->assertSame(['k1' => true, 'k0' => false], $call->result);
  63. $this->assertSame(1, $call->misses);
  64. $this->assertNotEmpty($call->start);
  65. $this->assertNotEmpty($call->end);
  66. }
  67. public function testHasMissTrace()
  68. {
  69. $pool = $this->createSimpleCache();
  70. $pool->has('k');
  71. $calls = $pool->getCalls();
  72. $this->assertCount(1, $calls);
  73. $call = $calls[0];
  74. $this->assertSame('has', $call->name);
  75. $this->assertSame(['k' => false], $call->result);
  76. $this->assertNotEmpty($call->start);
  77. $this->assertNotEmpty($call->end);
  78. }
  79. public function testHasHitTrace()
  80. {
  81. $pool = $this->createSimpleCache();
  82. $pool->set('k', 'foo');
  83. $pool->has('k');
  84. $calls = $pool->getCalls();
  85. $this->assertCount(2, $calls);
  86. $call = $calls[1];
  87. $this->assertSame('has', $call->name);
  88. $this->assertSame(['k' => true], $call->result);
  89. $this->assertNotEmpty($call->start);
  90. $this->assertNotEmpty($call->end);
  91. }
  92. public function testDeleteTrace()
  93. {
  94. $pool = $this->createSimpleCache();
  95. $pool->delete('k');
  96. $calls = $pool->getCalls();
  97. $this->assertCount(1, $calls);
  98. $call = $calls[0];
  99. $this->assertSame('delete', $call->name);
  100. $this->assertSame(['k' => true], $call->result);
  101. $this->assertSame(0, $call->hits);
  102. $this->assertSame(0, $call->misses);
  103. $this->assertNotEmpty($call->start);
  104. $this->assertNotEmpty($call->end);
  105. }
  106. public function testDeleteMultipleTrace()
  107. {
  108. $pool = $this->createSimpleCache();
  109. $arg = ['k0', 'k1'];
  110. $pool->deleteMultiple($arg);
  111. $calls = $pool->getCalls();
  112. $this->assertCount(1, $calls);
  113. $call = $calls[0];
  114. $this->assertSame('deleteMultiple', $call->name);
  115. $this->assertSame(['keys' => $arg, 'result' => true], $call->result);
  116. $this->assertSame(0, $call->hits);
  117. $this->assertSame(0, $call->misses);
  118. $this->assertNotEmpty($call->start);
  119. $this->assertNotEmpty($call->end);
  120. }
  121. public function testTraceSetTrace()
  122. {
  123. $pool = $this->createSimpleCache();
  124. $pool->set('k', 'foo');
  125. $calls = $pool->getCalls();
  126. $this->assertCount(1, $calls);
  127. $call = $calls[0];
  128. $this->assertSame('set', $call->name);
  129. $this->assertSame(['k' => true], $call->result);
  130. $this->assertSame(0, $call->hits);
  131. $this->assertSame(0, $call->misses);
  132. $this->assertNotEmpty($call->start);
  133. $this->assertNotEmpty($call->end);
  134. }
  135. public function testSetMultipleTrace()
  136. {
  137. $pool = $this->createSimpleCache();
  138. $pool->setMultiple(['k' => 'foo']);
  139. $calls = $pool->getCalls();
  140. $this->assertCount(1, $calls);
  141. $call = $calls[0];
  142. $this->assertSame('setMultiple', $call->name);
  143. $this->assertSame(['keys' => ['k'], 'result' => true], $call->result);
  144. $this->assertSame(0, $call->hits);
  145. $this->assertSame(0, $call->misses);
  146. $this->assertNotEmpty($call->start);
  147. $this->assertNotEmpty($call->end);
  148. }
  149. }