class ChainedFastBackendFactoryTest

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

@coversDefaultClass \Drupal\Core\Cache\ChainedFastBackendFactory @group Cache

Hierarchy

Expanded class hierarchy of ChainedFastBackendFactoryTest

File

core/tests/Drupal/Tests/Core/Cache/ChainedFastBackendFactoryTest.php, line 18

Namespace

Drupal\Tests\Core\Cache
View source
class ChainedFastBackendFactoryTest extends UnitTestCase {
    
    /**
     * Test if the same name is provided for consistent and fast services.
     */
    public function testIdenticalService() : void {
        $container = $this->createMock(ContainerInterface::class);
        $testCacheFactory = $this->createMock(CacheFactoryInterface::class);
        $testCacheBackend = $this->createMock(CacheBackendInterface::class);
        $container->expects($this->once())
            ->method('get')
            ->with('cache.backend.test')
            ->willReturn($testCacheFactory);
        $testCacheFactory->expects($this->once())
            ->method('get')
            ->with('test_bin')
            ->willReturn($testCacheBackend);
        $cacheFactory = new ChainedFastBackendFactory(NULL, 'cache.backend.test', 'cache.backend.test');
        $cacheFactory->setContainer($container);
        $cacheBackend = $cacheFactory->get('test_bin');
        // The test backend should be returned directly.
        $this->assertSame($testCacheBackend, $cacheBackend);
    }
    
    /**
     * Test if different names are provided for consistent and fast services.
     */
    public function testDifferentServices() : void {
        $container = $this->createMock(ContainerInterface::class);
        $testConsistentCacheFactory = $this->createMock(CacheFactoryInterface::class);
        $testFastCacheFactory = $this->createMock(CacheFactoryInterface::class);
        $testConsistentCacheBackend = $this->createMock(CacheBackendInterface::class);
        $testFastCacheBackend = $this->createMock(CacheBackendInterface::class);
        $container->expects($this->exactly(2))
            ->method('get')
            ->willReturnCallback(function ($service) use ($testFastCacheFactory, $testConsistentCacheFactory) {
            return match ($service) {    'cache.backend.test_consistent' => $testConsistentCacheFactory,
                'cache.backend.test_fast' => $testFastCacheFactory,
            
            };
        });
        // The same bin should be retrieved from both backends.
        $testConsistentCacheFactory->expects($this->once())
            ->method('get')
            ->with('test_bin')
            ->willReturn($testConsistentCacheBackend);
        $testFastCacheFactory->expects($this->once())
            ->method('get')
            ->with('test_bin')
            ->willReturn($testFastCacheBackend);
        $cacheFactory = new ChainedFastBackendFactory(NULL, 'cache.backend.test_consistent', 'cache.backend.test_fast');
        $cacheFactory->setContainer($container);
        // A wrapping ChainedFastBackend should be returned.
        $cacheBackend = $cacheFactory->get('test_bin');
        $this->assertInstanceOf(ChainedFastBackend::class, $cacheBackend);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ChainedFastBackendFactoryTest::testDifferentServices public function Test if different names are provided for consistent and fast services.
ChainedFastBackendFactoryTest::testIdenticalService public function Test if the same name is provided for consistent and fast services.
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 354
UnitTestCase::setUpBeforeClass public static function

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