TraceableCacheTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. */
  16. class TraceableCacheTest extends CacheTestCase
  17. {
  18. protected $skippedTests = [
  19. 'testPrune' => 'TraceableCache just proxies',
  20. ];
  21. public function createSimpleCache($defaultLifetime = 0)
  22. {
  23. return new TraceableCache(new FilesystemCache('', $defaultLifetime));
  24. }
  25. public function testGetMissTrace()
  26. {
  27. $pool = $this->createSimpleCache();
  28. $pool->get('k');
  29. $calls = $pool->getCalls();
  30. $this->assertCount(1, $calls);
  31. $call = $calls[0];
  32. $this->assertSame('get', $call->name);
  33. $this->assertSame(['k' => false], $call->result);
  34. $this->assertSame(0, $call->hits);
  35. $this->assertSame(1, $call->misses);
  36. $this->assertNotEmpty($call->start);
  37. $this->assertNotEmpty($call->end);
  38. }
  39. public function testGetHitTrace()
  40. {
  41. $pool = $this->createSimpleCache();
  42. $pool->set('k', 'foo');
  43. $pool->get('k');
  44. $calls = $pool->getCalls();
  45. $this->assertCount(2, $calls);
  46. $call = $calls[1];
  47. $this->assertSame(1, $call->hits);
  48. $this->assertSame(0, $call->misses);
  49. }
  50. public function testGetMultipleMissTrace()
  51. {
  52. $pool = $this->createSimpleCache();
  53. $pool->set('k1', 123);
  54. $values = $pool->getMultiple(['k0', 'k1']);
  55. foreach ($values as $value) {
  56. }
  57. $calls = $pool->getCalls();
  58. $this->assertCount(2, $calls);
  59. $call = $calls[1];
  60. $this->assertSame('getMultiple', $call->name);
  61. $this->assertSame(['k1' => true, 'k0' => false], $call->result);
  62. $this->assertSame(1, $call->misses);
  63. $this->assertNotEmpty($call->start);
  64. $this->assertNotEmpty($call->end);
  65. }
  66. public function testHasMissTrace()
  67. {
  68. $pool = $this->createSimpleCache();
  69. $pool->has('k');
  70. $calls = $pool->getCalls();
  71. $this->assertCount(1, $calls);
  72. $call = $calls[0];
  73. $this->assertSame('has', $call->name);
  74. $this->assertSame(['k' => false], $call->result);
  75. $this->assertNotEmpty($call->start);
  76. $this->assertNotEmpty($call->end);
  77. }
  78. public function testHasHitTrace()
  79. {
  80. $pool = $this->createSimpleCache();
  81. $pool->set('k', 'foo');
  82. $pool->has('k');
  83. $calls = $pool->getCalls();
  84. $this->assertCount(2, $calls);
  85. $call = $calls[1];
  86. $this->assertSame('has', $call->name);
  87. $this->assertSame(['k' => true], $call->result);
  88. $this->assertNotEmpty($call->start);
  89. $this->assertNotEmpty($call->end);
  90. }
  91. public function testDeleteTrace()
  92. {
  93. $pool = $this->createSimpleCache();
  94. $pool->delete('k');
  95. $calls = $pool->getCalls();
  96. $this->assertCount(1, $calls);
  97. $call = $calls[0];
  98. $this->assertSame('delete', $call->name);
  99. $this->assertSame(['k' => true], $call->result);
  100. $this->assertSame(0, $call->hits);
  101. $this->assertSame(0, $call->misses);
  102. $this->assertNotEmpty($call->start);
  103. $this->assertNotEmpty($call->end);
  104. }
  105. public function testDeleteMultipleTrace()
  106. {
  107. $pool = $this->createSimpleCache();
  108. $arg = ['k0', 'k1'];
  109. $pool->deleteMultiple($arg);
  110. $calls = $pool->getCalls();
  111. $this->assertCount(1, $calls);
  112. $call = $calls[0];
  113. $this->assertSame('deleteMultiple', $call->name);
  114. $this->assertSame(['keys' => $arg, 'result' => true], $call->result);
  115. $this->assertSame(0, $call->hits);
  116. $this->assertSame(0, $call->misses);
  117. $this->assertNotEmpty($call->start);
  118. $this->assertNotEmpty($call->end);
  119. }
  120. public function testTraceSetTrace()
  121. {
  122. $pool = $this->createSimpleCache();
  123. $pool->set('k', 'foo');
  124. $calls = $pool->getCalls();
  125. $this->assertCount(1, $calls);
  126. $call = $calls[0];
  127. $this->assertSame('set', $call->name);
  128. $this->assertSame(['k' => true], $call->result);
  129. $this->assertSame(0, $call->hits);
  130. $this->assertSame(0, $call->misses);
  131. $this->assertNotEmpty($call->start);
  132. $this->assertNotEmpty($call->end);
  133. }
  134. public function testSetMultipleTrace()
  135. {
  136. $pool = $this->createSimpleCache();
  137. $pool->setMultiple(['k' => 'foo']);
  138. $calls = $pool->getCalls();
  139. $this->assertCount(1, $calls);
  140. $call = $calls[0];
  141. $this->assertSame('setMultiple', $call->name);
  142. $this->assertSame(['keys' => ['k'], 'result' => true], $call->result);
  143. $this->assertSame(0, $call->hits);
  144. $this->assertSame(0, $call->misses);
  145. $this->assertNotEmpty($call->start);
  146. $this->assertNotEmpty($call->end);
  147. }
  148. }