You are here

class CacheFactoryTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php \Drupal\Tests\Core\Cache\CacheFactoryTest

@coversDefaultClass \Drupal\Core\Cache\CacheFactory @group Cache

Hierarchy

Expanded class hierarchy of CacheFactoryTest

File

core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php, line 19
Contains \Drupal\Tests\Core\Cache\CacheFactoryTest.

Namespace

Drupal\Tests\Core\Cache
View source
class CacheFactoryTest extends UnitTestCase {

  /**
   * Test that the cache factory falls back to the built-in default service.
   *
   * @covers ::__construct
   * @covers ::get
   */
  public function testCacheFactoryWithDefaultSettings() {
    $settings = new Settings(array());
    $cache_factory = new CacheFactory($settings);
    $container = new ContainerBuilder();
    $cache_factory
      ->setContainer($container);
    $builtin_default_backend_factory = $this
      ->getMock('\\Drupal\\Core\\Cache\\CacheFactoryInterface');
    $container
      ->set('cache.backend.database', $builtin_default_backend_factory);
    $render_bin = $this
      ->getMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
    $builtin_default_backend_factory
      ->expects($this
      ->once())
      ->method('get')
      ->with('render')
      ->will($this
      ->returnValue($render_bin));
    $actual_bin = $cache_factory
      ->get('render');
    $this
      ->assertSame($render_bin, $actual_bin);
  }

  /**
   * Test that the cache factory falls back to customized default service.
   *
   * @covers ::__construct
   * @covers ::get
   */
  public function testCacheFactoryWithCustomizedDefaultBackend() {
    $settings = new Settings(array(
      'cache' => array(
        'default' => 'cache.backend.custom',
      ),
    ));
    $cache_factory = new CacheFactory($settings);
    $container = new ContainerBuilder();
    $cache_factory
      ->setContainer($container);
    $custom_default_backend_factory = $this
      ->getMock('\\Drupal\\Core\\Cache\\CacheFactoryInterface');
    $container
      ->set('cache.backend.custom', $custom_default_backend_factory);
    $render_bin = $this
      ->getMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
    $custom_default_backend_factory
      ->expects($this
      ->once())
      ->method('get')
      ->with('render')
      ->will($this
      ->returnValue($render_bin));
    $actual_bin = $cache_factory
      ->get('render');
    $this
      ->assertSame($render_bin, $actual_bin);
  }

  /**
   * Test that the cache factory picks the correct per-bin service.
   *
   * @covers ::__construct
   * @covers ::get
   */
  public function testCacheFactoryWithSpecifiedPerBinBackend() {
    $settings = new Settings(array(
      'cache' => array(
        'bins' => array(
          'render' => 'cache.backend.custom',
        ),
      ),
    ));
    $cache_factory = new CacheFactory($settings);
    $container = new ContainerBuilder();
    $cache_factory
      ->setContainer($container);
    $custom_render_backend_factory = $this
      ->getMock('\\Drupal\\Core\\Cache\\CacheFactoryInterface');
    $container
      ->set('cache.backend.custom', $custom_render_backend_factory);
    $render_bin = $this
      ->getMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
    $custom_render_backend_factory
      ->expects($this
      ->once())
      ->method('get')
      ->with('render')
      ->will($this
      ->returnValue($render_bin));
    $actual_bin = $cache_factory
      ->get('render');
    $this
      ->assertSame($render_bin, $actual_bin);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheFactoryTest::testCacheFactoryWithCustomizedDefaultBackend public function Test that the cache factory falls back to customized default service.
CacheFactoryTest::testCacheFactoryWithDefaultSettings public function Test that the cache factory falls back to the built-in default service.
CacheFactoryTest::testCacheFactoryWithSpecifiedPerBinBackend public function Test that the cache factory picks the correct per-bin service.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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.
UnitTestCase::setUp protected function 259