3 namespace Drupal\Tests\webprofiler\Unit\Cache;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\webprofiler\Cache\CacheBackendWrapper;
7 use Drupal\webprofiler\DataCollector\CacheDataCollector;
10 * @coversDefaultClass \Drupal\webprofiler\DataCollector\CacheDataCollector
13 class CacheDataCollectorTest extends UnitTestCase {
16 * @var \Drupal\webprofiler\DataCollector\CacheDataCollector
18 private $cacheDataCollector;
21 * @var \PHPUnit_Framework_MockObject_MockObject
23 private $cacheBackendInterface;
28 public function setUp() {
29 $this->cacheDataCollector = new CacheDataCollector();
30 $this->cacheBackendInterface = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
34 * Tests the collection of a cache miss.
36 public function testCacheCollectorMiss() {
37 $this->cacheBackendInterface->expects($this->once())
39 ->will($this->returnValue(FALSE));
41 $cacheBackendWrapper = new CacheBackendWrapper($this->cacheDataCollector, $this->cacheBackendInterface, 'default');
42 $cache = $cacheBackendWrapper->get('cache_id');
44 $this->assertFalse($cache);
46 $this->assertEquals(1, $this->cacheDataCollector->getCacheMissesCount());
50 * Tests the collection of a cache hit.
52 public function testCacheCollectorHit() {
53 $cache = new \StdClass();
54 $cache->cid = 'cache_id';
56 $cache->tags = ['tag1', 'tag2'];
57 $this->cacheBackendInterface->expects($this->once())
59 ->will($this->returnValue($cache));
61 $cacheBackendWrapper = new CacheBackendWrapper($this->cacheDataCollector, $this->cacheBackendInterface, 'default');
62 $cache2 = $cacheBackendWrapper->get('cache_id');
64 $this->assertNotNull($cache2);
66 $this->assertEquals(1, $this->cacheDataCollector->getCacheHitsCount());