TraceableAdapterTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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\Adapter;
  11. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  12. use Symfony\Component\Cache\Adapter\TraceableAdapter;
  13. /**
  14. * @group time-sensitive
  15. */
  16. class TraceableAdapterTest extends AdapterTestCase
  17. {
  18. protected $skippedTests = [
  19. 'testPrune' => 'TraceableAdapter just proxies',
  20. ];
  21. public function createCachePool($defaultLifetime = 0)
  22. {
  23. return new TraceableAdapter(new FilesystemAdapter('', $defaultLifetime));
  24. }
  25. public function testGetItemMissTrace()
  26. {
  27. $pool = $this->createCachePool();
  28. $pool->getItem('k');
  29. $calls = $pool->getCalls();
  30. $this->assertCount(1, $calls);
  31. $call = $calls[0];
  32. $this->assertSame('getItem', $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 testGetItemHitTrace()
  40. {
  41. $pool = $this->createCachePool();
  42. $item = $pool->getItem('k')->set('foo');
  43. $pool->save($item);
  44. $pool->getItem('k');
  45. $calls = $pool->getCalls();
  46. $this->assertCount(3, $calls);
  47. $call = $calls[2];
  48. $this->assertSame(1, $call->hits);
  49. $this->assertSame(0, $call->misses);
  50. }
  51. public function testGetItemsMissTrace()
  52. {
  53. $pool = $this->createCachePool();
  54. $arg = ['k0', 'k1'];
  55. $items = $pool->getItems($arg);
  56. foreach ($items as $item) {
  57. }
  58. $calls = $pool->getCalls();
  59. $this->assertCount(1, $calls);
  60. $call = $calls[0];
  61. $this->assertSame('getItems', $call->name);
  62. $this->assertSame(['k0' => false, 'k1' => false], $call->result);
  63. $this->assertSame(2, $call->misses);
  64. $this->assertNotEmpty($call->start);
  65. $this->assertNotEmpty($call->end);
  66. }
  67. public function testHasItemMissTrace()
  68. {
  69. $pool = $this->createCachePool();
  70. $pool->hasItem('k');
  71. $calls = $pool->getCalls();
  72. $this->assertCount(1, $calls);
  73. $call = $calls[0];
  74. $this->assertSame('hasItem', $call->name);
  75. $this->assertSame(['k' => false], $call->result);
  76. $this->assertNotEmpty($call->start);
  77. $this->assertNotEmpty($call->end);
  78. }
  79. public function testHasItemHitTrace()
  80. {
  81. $pool = $this->createCachePool();
  82. $item = $pool->getItem('k')->set('foo');
  83. $pool->save($item);
  84. $pool->hasItem('k');
  85. $calls = $pool->getCalls();
  86. $this->assertCount(3, $calls);
  87. $call = $calls[2];
  88. $this->assertSame('hasItem', $call->name);
  89. $this->assertSame(['k' => true], $call->result);
  90. $this->assertNotEmpty($call->start);
  91. $this->assertNotEmpty($call->end);
  92. }
  93. public function testDeleteItemTrace()
  94. {
  95. $pool = $this->createCachePool();
  96. $pool->deleteItem('k');
  97. $calls = $pool->getCalls();
  98. $this->assertCount(1, $calls);
  99. $call = $calls[0];
  100. $this->assertSame('deleteItem', $call->name);
  101. $this->assertSame(['k' => true], $call->result);
  102. $this->assertSame(0, $call->hits);
  103. $this->assertSame(0, $call->misses);
  104. $this->assertNotEmpty($call->start);
  105. $this->assertNotEmpty($call->end);
  106. }
  107. public function testDeleteItemsTrace()
  108. {
  109. $pool = $this->createCachePool();
  110. $arg = ['k0', 'k1'];
  111. $pool->deleteItems($arg);
  112. $calls = $pool->getCalls();
  113. $this->assertCount(1, $calls);
  114. $call = $calls[0];
  115. $this->assertSame('deleteItems', $call->name);
  116. $this->assertSame(['keys' => $arg, 'result' => true], $call->result);
  117. $this->assertSame(0, $call->hits);
  118. $this->assertSame(0, $call->misses);
  119. $this->assertNotEmpty($call->start);
  120. $this->assertNotEmpty($call->end);
  121. }
  122. public function testSaveTrace()
  123. {
  124. $pool = $this->createCachePool();
  125. $item = $pool->getItem('k')->set('foo');
  126. $pool->save($item);
  127. $calls = $pool->getCalls();
  128. $this->assertCount(2, $calls);
  129. $call = $calls[1];
  130. $this->assertSame('save', $call->name);
  131. $this->assertSame(['k' => true], $call->result);
  132. $this->assertSame(0, $call->hits);
  133. $this->assertSame(0, $call->misses);
  134. $this->assertNotEmpty($call->start);
  135. $this->assertNotEmpty($call->end);
  136. }
  137. public function testSaveDeferredTrace()
  138. {
  139. $pool = $this->createCachePool();
  140. $item = $pool->getItem('k')->set('foo');
  141. $pool->saveDeferred($item);
  142. $calls = $pool->getCalls();
  143. $this->assertCount(2, $calls);
  144. $call = $calls[1];
  145. $this->assertSame('saveDeferred', $call->name);
  146. $this->assertSame(['k' => true], $call->result);
  147. $this->assertSame(0, $call->hits);
  148. $this->assertSame(0, $call->misses);
  149. $this->assertNotEmpty($call->start);
  150. $this->assertNotEmpty($call->end);
  151. }
  152. public function testCommitTrace()
  153. {
  154. $pool = $this->createCachePool();
  155. $pool->commit();
  156. $calls = $pool->getCalls();
  157. $this->assertCount(1, $calls);
  158. $call = $calls[0];
  159. $this->assertSame('commit', $call->name);
  160. $this->assertTrue($call->result);
  161. $this->assertSame(0, $call->hits);
  162. $this->assertSame(0, $call->misses);
  163. $this->assertNotEmpty($call->start);
  164. $this->assertNotEmpty($call->end);
  165. }
  166. }