f4673eea7dc2c643f7d6d4f9c3b91a4b258cbacb
[yaffs-website] / web / modules / contrib / devel / webprofiler / tests / src / Unit / Cache / CacheDataCollectorTest.php
1 <?php
2
3 namespace Drupal\Tests\webprofiler\Unit\Cache;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\webprofiler\Cache\CacheBackendWrapper;
7 use Drupal\webprofiler\DataCollector\CacheDataCollector;
8
9 /**
10  * @coversDefaultClass \Drupal\webprofiler\DataCollector\CacheDataCollector
11  * @group webprofiler
12  */
13 class CacheDataCollectorTest extends UnitTestCase {
14
15   /**
16    * @var \Drupal\webprofiler\DataCollector\CacheDataCollector
17    */
18   private $cacheDataCollector;
19
20   /**
21    * @var \PHPUnit_Framework_MockObject_MockObject
22    */
23   private $cacheBackendInterface;
24
25   /**
26    * {@inheritdoc}
27    */
28   public function setUp() {
29     $this->cacheDataCollector = new CacheDataCollector();
30     $this->cacheBackendInterface = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
31   }
32
33   /**
34    * Tests the collection of a cache miss.
35    */
36   public function testCacheCollectorMiss() {
37     $this->cacheBackendInterface->expects($this->once())
38       ->method('get')
39       ->will($this->returnValue(FALSE));
40
41     $cacheBackendWrapper = new CacheBackendWrapper($this->cacheDataCollector, $this->cacheBackendInterface, 'default');
42     $cache = $cacheBackendWrapper->get('cache_id');
43
44     $this->assertFalse($cache);
45
46     $this->assertEquals(1, $this->cacheDataCollector->getCacheMissesCount());
47   }
48
49   /**
50    * Tests the collection of a cache hit.
51    */
52   public function testCacheCollectorHit() {
53     $cache = new \StdClass();
54     $cache->cid = 'cache_id';
55     $cache->expire = 1;
56     $cache->tags = ['tag1', 'tag2'];
57     $this->cacheBackendInterface->expects($this->once())
58       ->method('get')
59       ->will($this->returnValue($cache));
60
61     $cacheBackendWrapper = new CacheBackendWrapper($this->cacheDataCollector, $this->cacheBackendInterface, 'default');
62     $cache2 = $cacheBackendWrapper->get('cache_id');
63
64     $this->assertNotNull($cache2);
65
66     $this->assertEquals(1, $this->cacheDataCollector->getCacheHitsCount());
67   }
68
69 }