You are here

class CacheDataCollectorTest in Devel 8.2

Same name and namespace in other branches
  1. 8.3 webprofiler/tests/src/Unit/DataCollector/CacheDataCollectorTest.php \Drupal\Tests\webprofiler\Unit\DataCollector\CacheDataCollectorTest
  2. 8 webprofiler/tests/src/Unit/DataCollector/CacheDataCollectorTest.php \Drupal\Tests\webprofiler\Unit\DataCollector\CacheDataCollectorTest
  3. 4.x webprofiler/tests/src/Unit/DataCollector/CacheDataCollectorTest.php \Drupal\Tests\webprofiler\Unit\DataCollector\CacheDataCollectorTest

@coversDefaultClass \Drupal\webprofiler\DataCollector\CacheDataCollector

@group webprofiler

Hierarchy

Expanded class hierarchy of CacheDataCollectorTest

File

webprofiler/tests/src/Unit/DataCollector/CacheDataCollectorTest.php, line 13

Namespace

Drupal\Tests\webprofiler\Unit\DataCollector
View source
class CacheDataCollectorTest extends DataCollectorBaseTest {

  /**
   * @var \Drupal\webprofiler\DataCollector\CacheDataCollector
   */
  private $cacheDataCollector;

  /**
   * @var \PHPUnit_Framework_MockObject_MockObject
   */
  private $cacheBackendInterface;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $this->cacheDataCollector = new CacheDataCollector();
    $this->cacheBackendInterface = $this
      ->getMock('Drupal\\Core\\Cache\\CacheBackendInterface');
  }

  /**
   * Tests the collection of a cache miss.
   */
  public function testCacheCollectorMiss() {
    $this->cacheBackendInterface
      ->expects($this
      ->once())
      ->method('get')
      ->will($this
      ->returnValue(FALSE));
    $cacheBackendWrapper = new CacheBackendWrapper($this->cacheDataCollector, $this->cacheBackendInterface, 'default');
    $cache = $cacheBackendWrapper
      ->get('cache_id');
    $this
      ->assertFalse($cache);
    $this
      ->assertEquals(1, $this->cacheDataCollector
      ->getCacheMissesCount());
  }

  /**
   * Tests the collection of a cache hit.
   */
  public function testCacheCollectorHit() {
    $cache = new \stdClass();
    $cache->cid = 'cache_id';
    $cache->expire = 1;
    $cache->tags = [
      'tag1',
      'tag2',
    ];
    $this->cacheBackendInterface
      ->expects($this
      ->once())
      ->method('get')
      ->will($this
      ->returnValue($cache));
    $cacheBackendWrapper = new CacheBackendWrapper($this->cacheDataCollector, $this->cacheBackendInterface, 'default');
    $cache2 = $cacheBackendWrapper
      ->get('cache_id');
    $this
      ->assertNotNull($cache2);
    $this
      ->assertEquals(1, $this->cacheDataCollector
      ->getCacheHitsCount());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheDataCollectorTest::$cacheBackendInterface private property
CacheDataCollectorTest::$cacheDataCollector private property
CacheDataCollectorTest::setUp public function Overrides DataCollectorBaseTest::setUp
CacheDataCollectorTest::testCacheCollectorHit public function Tests the collection of a cache hit.
CacheDataCollectorTest::testCacheCollectorMiss public function Tests the collection of a cache miss.
DataCollectorBaseTest::$exception protected property @var
DataCollectorBaseTest::$request protected property @var
DataCollectorBaseTest::$response protected property @var
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.