You are here

class CacheTagsQueuerTest in Purge 8.3

@coversDefaultClass \Drupal\purge_queuer_coretags\CacheTagsQueuer

@group purge

Hierarchy

Expanded class hierarchy of CacheTagsQueuerTest

File

modules/purge_queuer_coretags/tests/src/Unit/CacheTagsQueuerTest.php, line 21

Namespace

Drupal\Tests\purge_queuer_coretags\Unit
View source
class CacheTagsQueuerTest extends UnitTestCase {

  /**
   * The tested cache tags queuer.
   *
   * @var \Drupal\purge_queuer_coretags\CacheTagsQueuer
   */
  protected $cacheTagsQueuer;

  /**
   * The mocked config factory.
   *
   * @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

  /**
   * The mocked queue service.
   *
   * @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\purge\Plugin\Purge\Queue\QueueServiceInterface
   */
  protected $purgeQueue;

  /**
   * The mocked queuers service.
   *
   * @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\purge\Plugin\Purge\Queuers\QueuersServiceInterface
   */
  protected $purgeQueuers;

  /**
   * The mocked invalidations factory.
   *
   * @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\purge\Plugin\Purge\Invalidation\InvalidationsServiceInterface
   */
  protected $purgeInvalidationFactory;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->purgeQueue = $this
      ->getMockBuilder(QueueServiceInterface::class)
      ->setMethods([])
      ->getMock();
    $this->purgeQueuers = $this
      ->getMockBuilder(QueuersServiceInterface::class)
      ->setMethods([
      'get',
    ])
      ->getMock();
    $this->purgeInvalidationFactory = $this
      ->getMockForAbstractClass(InvalidationsServiceInterface::class);

    // Create a container with all dependent services in it.
    $this->container = new ContainerBuilder();
    $this->container
      ->set('purge.queue', $this->purgeQueue);
    $this->container
      ->set('purge.queuers', $this->purgeQueuers);
    $this->container
      ->set('purge.invalidation.factory', $this->purgeInvalidationFactory);

    // Initialize the CacheTagsQueuer object and set the container.
    $this->cacheTagsQueuer = new CacheTagsQueuer();
    $this->cacheTagsQueuer
      ->setContainer($this->container);
    $this
      ->assertInstanceOf(ContainerAwareInterface::class, $this->cacheTagsQueuer);
    $this
      ->assertInstanceOf(CacheTagsInvalidatorInterface::class, $this->cacheTagsQueuer);
  }

  /**
   * @covers ::initialize
   */
  public function testInitializeDoesntLoadWhenQueuerDisabled() : void {
    $this->purgeInvalidationFactory
      ->expects($this
      ->never())
      ->method('get');
    $this->purgeQueue
      ->expects($this
      ->never())
      ->method('add');
    $this->purgeQueuers
      ->expects($this
      ->once())
      ->method('get')
      ->with('coretags')
      ->willReturn(FALSE);
    $this
      ->assertNull($this->cacheTagsQueuer
      ->invalidateTags([
      "1",
      "2",
    ]));
  }

  /**
   * @covers ::invalidateTags
   *
   * @dataProvider providerTestInvalidateTags()
   */
  public function testInvalidateTags($config, array $sets) : void {
    $this->container
      ->set('config.factory', $this
      ->getConfigFactoryStub($config));

    // Assert that the queuer plugin is loaded exactly once.
    $this->purgeQueuers
      ->expects($this
      ->once())
      ->method('get')
      ->with('coretags')
      ->willReturn($this
      ->getMockBuilder(QueuerBase::class)
      ->disableOriginalConstructor()
      ->getMock());

    // Assert how InvalidationsServiceInterface::get() is called.
    $invs_added_total = array_sum(array_map(function ($set) {
      return $set[1];
    }, $sets));
    $this->purgeInvalidationFactory
      ->expects($this
      ->exactly($invs_added_total))
      ->method('get')
      ->with('tag')
      ->willReturn($this
      ->createMock(InvalidationInterface::class));

    // Assert the precise calls to QueueServiceInterface::add().
    $number_queue_add_calls = count(array_filter($sets, function ($set) {
      return $set[1] !== 0;
    }));
    reset($sets);
    $this->purgeQueue
      ->expects($this
      ->exactly($number_queue_add_calls))
      ->method('add')
      ->with($this
      ->callback(function ($queuer) {
      return $queuer instanceof QueuerBase;
    }), $this
      ->callback(function (array $invs) use (&$sets) {
      $invs_added = current($sets)[1];

      // Sets with 0 shouldn't call ::add() at all, so skip over them.
      if ($invs_added === 0) {
        next($sets);
        $invs_added = current($sets)[1];
      }
      next($sets);
      return is_array($invs) && count($invs) === $invs_added;
    }));

    // Trigger the entire chain by feeding the sets of tags.
    foreach ($sets as $set) {
      $this->cacheTagsQueuer
        ->invalidateTags($set[0]);
    }
  }

  /**
   * Provides test data for testInvalidateTags().
   */
  public function providerTestInvalidateTags() : array {
    $blacklist = [
      'purge_queuer_coretags.settings' => [
        'blacklist' => [
          'menu',
          'node',
        ],
      ],
    ];
    return [
      // Two calls to ::invalidateTags(), 'node:5' should get blacklisted.
      [
        $blacklist,
        [
          [
            [
              'block:1',
            ],
            1,
          ],
          [
            [
              'node:5',
            ],
            0,
          ],
          [
            [
              'extension:views',
              'baz',
            ],
            2,
          ],
        ],
      ],
      // One call to ::invalidateTags(), with 1 and 3 tags respectively.
      [
        $blacklist,
        [
          [
            [
              'menu:main',
            ],
            0,
          ],
          [
            [
              'NODE:5',
              'foo',
              'bar',
              'bar',
            ],
            3,
          ],
        ],
      ],
      // One call to ::invalidateTags() with 4 tags.
      [
        [
          'purge_queuer_coretags.settings' => [
            'blacklist' => [],
          ],
        ],
        [
          [
            [
              'node:5',
              'foo:2',
              'foo:3',
              'bar:baz',
            ],
            4,
          ],
        ],
      ],
      // Five calls to ::invalidateTags() with varying number of tags.
      [
        [
          'purge_queuer_coretags.settings' => [
            'blacklist' => [],
          ],
        ],
        [
          [
            [
              'a',
              'b',
            ],
            2,
          ],
          [
            [
              'c',
              'd',
            ],
            2,
          ],
          [
            [
              'e',
              'f',
            ],
            2,
          ],
          [
            [
              'g',
              'h',
              'i',
            ],
            3,
          ],
          [
            [
              'j',
            ],
            1,
          ],
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheTagsQueuerTest::$cacheTagsQueuer protected property The tested cache tags queuer.
CacheTagsQueuerTest::$configFactory protected property The mocked config factory.
CacheTagsQueuerTest::$container protected property The container.
CacheTagsQueuerTest::$purgeInvalidationFactory protected property The mocked invalidations factory.
CacheTagsQueuerTest::$purgeQueue protected property The mocked queue service.
CacheTagsQueuerTest::$purgeQueuers protected property The mocked queuers service.
CacheTagsQueuerTest::providerTestInvalidateTags public function Provides test data for testInvalidateTags().
CacheTagsQueuerTest::setUp protected function Overrides UnitTestCase::setUp
CacheTagsQueuerTest::testInitializeDoesntLoadWhenQueuerDisabled public function @covers ::initialize
CacheTagsQueuerTest::testInvalidateTags public function @covers ::invalidateTags
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.