class CacheFactoryTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php \Drupal\Tests\Core\Cache\CacheFactoryTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php \Drupal\Tests\Core\Cache\CacheFactoryTest
  3. 10 core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php \Drupal\Tests\Core\Cache\CacheFactoryTest

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

Hierarchy

  • class \Drupal\Tests\Core\Cache\CacheFactoryTest extends \Drupal\Tests\UnitTestCase

Expanded class hierarchy of CacheFactoryTest

File

core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php, line 16

Namespace

Drupal\Tests\Core\Cache
View source
class CacheFactoryTest extends UnitTestCase {
    
    /**
     * Tests that the cache factory falls back to the built-in default service.
     *
     * @covers ::__construct
     * @covers ::get
     */
    public function testCacheFactoryWithDefaultSettings() : void {
        $settings = new Settings([]);
        $cache_factory = new CacheFactory($settings);
        $container = new ContainerBuilder();
        $cache_factory->setContainer($container);
        $builtin_default_backend_factory = $this->createMock('\\Drupal\\Core\\Cache\\CacheFactoryInterface');
        $container->set('cache.backend.database', $builtin_default_backend_factory);
        $render_bin = $this->createMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
        $builtin_default_backend_factory->expects($this->once())
            ->method('get')
            ->with('render')
            ->willReturn($render_bin);
        $actual_bin = $cache_factory->get('render');
        $this->assertSame($render_bin, $actual_bin);
    }
    
    /**
     * Tests that the cache factory falls back to customized default service.
     *
     * @covers ::__construct
     * @covers ::get
     */
    public function testCacheFactoryWithCustomizedDefaultBackend() : void {
        $settings = new Settings([
            'cache' => [
                'default' => 'cache.backend.custom',
            ],
        ]);
        $cache_factory = new CacheFactory($settings);
        $container = new ContainerBuilder();
        $cache_factory->setContainer($container);
        $custom_default_backend_factory = $this->createMock('\\Drupal\\Core\\Cache\\CacheFactoryInterface');
        $container->set('cache.backend.custom', $custom_default_backend_factory);
        $render_bin = $this->createMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
        $custom_default_backend_factory->expects($this->once())
            ->method('get')
            ->with('render')
            ->willReturn($render_bin);
        $actual_bin = $cache_factory->get('render');
        $this->assertSame($render_bin, $actual_bin);
    }
    
    /**
     * Tests that the cache factory uses the correct default bin backend.
     *
     * @covers ::__construct
     * @covers ::get
     */
    public function testCacheFactoryWithDefaultBinBackend() : void {
        // Ensure the default bin backends are used before the configured default.
        $settings = new Settings([
            'cache' => [
                'default' => 'cache.backend.unused',
            ],
        ]);
        $default_bin_backends = [
            'render' => 'cache.backend.custom',
        ];
        $cache_factory = new CacheFactory($settings, $default_bin_backends);
        $container = new ContainerBuilder();
        $cache_factory->setContainer($container);
        $custom_default_backend_factory = $this->createMock('\\Drupal\\Core\\Cache\\CacheFactoryInterface');
        $container->set('cache.backend.custom', $custom_default_backend_factory);
        $render_bin = $this->createMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
        $custom_default_backend_factory->expects($this->once())
            ->method('get')
            ->with('render')
            ->willReturn($render_bin);
        $actual_bin = $cache_factory->get('render');
        $this->assertSame($render_bin, $actual_bin);
    }
    
    /**
     * Tests that the cache factory picks the correct per-bin service.
     *
     * @covers ::__construct
     * @covers ::get
     */
    public function testCacheFactoryWithSpecifiedPerBinBackend() : void {
        // Ensure the per-bin configuration is used before the configured default
        // and per-bin defaults.
        $settings = new Settings([
            'cache' => [
                'default' => 'cache.backend.unused',
                'bins' => [
                    'render' => 'cache.backend.custom',
                ],
            ],
        ]);
        $default_bin_backends = [
            'render' => 'cache.backend.unused',
        ];
        $cache_factory = new CacheFactory($settings, $default_bin_backends);
        $container = new ContainerBuilder();
        $cache_factory->setContainer($container);
        $custom_render_backend_factory = $this->createMock('\\Drupal\\Core\\Cache\\CacheFactoryInterface');
        $container->set('cache.backend.custom', $custom_render_backend_factory);
        $render_bin = $this->createMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
        $custom_render_backend_factory->expects($this->once())
            ->method('get')
            ->with('render')
            ->willReturn($render_bin);
        $actual_bin = $cache_factory->get('render');
        $this->assertSame($render_bin, $actual_bin);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
CacheFactoryTest::testCacheFactoryWithCustomizedDefaultBackend public function Tests that the cache factory falls back to customized default service.
CacheFactoryTest::testCacheFactoryWithDefaultBinBackend public function Tests that the cache factory uses the correct default bin backend.
CacheFactoryTest::testCacheFactoryWithDefaultSettings public function Tests that the cache factory falls back to the built-in default service.
CacheFactoryTest::testCacheFactoryWithSpecifiedPerBinBackend public function Tests that the cache factory picks the correct per-bin service.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 367
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.