class CachePreWarmerTest

@coversDefaultClass \Drupal\Core\PreWarm\CachePreWarmer
@group PreWarm

Hierarchy

Expanded class hierarchy of CachePreWarmerTest

File

core/tests/Drupal/Tests/Core/PreWarm/CachePreWarmerTest.php, line 17

Namespace

Drupal\Tests\Core\PreWarm
View source
class CachePreWarmerTest extends UnitTestCase {
  
  /**
   * @var \Drupal\Core\DependencyInjection\ClassResolverInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected MockObject|ClassResolverInterface $classResolver;
  
  /**
   * @var \SplObjectStorage<\Drupal\Core\PreWarm\PreWarmableInterface|\PHPUnit\Framework\MockObject\MockObject>
   */
  protected \SplObjectStorage $warmedMap;
  public function testNoServices() : void {
    $classResolver = $this->createMock(ClassResolverInterface::class);
    $classResolver->expects($this->never())
      ->method('getInstanceFromDefinition');
    $prewarmer = new CachePreWarmer($classResolver, []);
    $this->assertFalse($prewarmer->preWarmOneCache());
    $this->assertFalse($prewarmer->preWarmAllCaches());
  }
  protected function setupCacheServices() : void {
    $this->classResolver = $this->createMock(ClassResolverInterface::class);
    $this->warmedMap = new \SplObjectStorage();
    for ($i = 0; $i < 4; $i++) {
      $serviceId = 'service' . $i;
      $serviceMock = $this->createMock(PrewarmableInterface::class);
      $this->warmedMap[$serviceMock] = 0;
      $serviceMock->method('preWarm')
        ->willReturnCallback(function () use ($serviceMock) {
        $this->warmedMap[$serviceMock] = 1 + $this->warmedMap[$serviceMock];
      });
      $returnMap[] = [
        $serviceId,
        $serviceMock,
      ];
    }
    $this->classResolver
      ->method('getInstanceFromDefinition')
      ->willReturnMap($returnMap);
  }
  
  /**
   * @covers ::preWarmOneCache
   */
  public function testPreWarmOnlyOne() : void {
    $this->setupCacheServices();
    $preWarmer = new CachePreWarmer($this->classResolver, [
      'service0',
      'service1',
      'service2',
      'service3',
    ]);
    $this->assertTrue($preWarmer->preWarmOneCache());
    $warmed = 0;
    foreach ($this->warmedMap as $service) {
      $warmed += $this->warmedMap[$service];
    }
    $this->assertEquals(1, $warmed);
  }
  
  /**
   * @covers ::preWarmOneCache
   */
  public function testPreWarmByOne() : void {
    $this->setupCacheServices();
    $preWarmer = new CachePreWarmer($this->classResolver, [
      'service0',
      'service1',
      'service2',
      'service3',
    ]);
    while ($preWarmer->preWarmOneCache()) {
    }
    foreach ($this->warmedMap as $service) {
      $this->assertEquals(1, $this->warmedMap[$service]);
    }
  }
  
  /**
   * @covers ::preWarmAllCaches
   */
  public function testPreWarmAll() : void {
    $this->setupCacheServices();
    $preWarmer = new CachePreWarmer($this->classResolver, [
      'service0',
      'service1',
      'service2',
      'service3',
    ]);
    $this->assertTrue($preWarmer->preWarmAllCaches());
    foreach ($this->warmedMap as $service) {
      $this->assertEquals(1, $this->warmedMap[$service]);
    }
    $this->assertFalse($preWarmer->preWarmAllCaches());
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
CachePreWarmerTest::$classResolver protected property
CachePreWarmerTest::$warmedMap protected property
CachePreWarmerTest::setupCacheServices protected function
CachePreWarmerTest::testNoServices public function
CachePreWarmerTest::testPreWarmAll public function @covers ::preWarmAllCaches[[api-linebreak]]
CachePreWarmerTest::testPreWarmByOne public function @covers ::preWarmOneCache[[api-linebreak]]
CachePreWarmerTest::testPreWarmOnlyOne public function @covers ::preWarmOneCache[[api-linebreak]]
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
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::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::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setUp protected function 374

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