class VariantCollectionTraitTest

Same name and namespace in other branches
  1. 8.x-3.x tests/src/Unit/VariantCollectionTraitTest.php \Drupal\Tests\ctools\Unit\VariantCollectionTraitTest

Tests the methods of a variant-aware class.

@coversDefaultClass \Drupal\ctools\Plugin\VariantCollectionTrait

@group Ctools

Hierarchy

Expanded class hierarchy of VariantCollectionTraitTest

File

tests/src/Unit/VariantCollectionTraitTest.php, line 21

Namespace

Drupal\Tests\ctools\Unit
View source
class VariantCollectionTraitTest extends UnitTestCase {
  use ProphecyTrait;
  
  /**
   * @var \Drupal\Component\Plugin\PluginManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $manager;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $container = new ContainerBuilder();
    $this->manager = $this->prophesize(PluginManagerInterface::class);
    $container->set('plugin.manager.display_variant', $this->manager
      ->reveal());
    \Drupal::setContainer($container);
  }
  
  /**
   * @covers ::getVariants
   */
  public function testGetVariantsEmpty() {
    $trait_object = new TestVariantCollectionTrait();
    $this->manager
      ->createInstance()
      ->shouldNotBeCalled();
    $variants = $trait_object->getVariants();
    $this->assertInstanceOf(VariantPluginCollection::class, $variants);
    $this->assertSame(0, count($variants));
  }
  
  /**
   * @covers ::getVariants
   */
  public function testGetVariants() {
    $trait_object = new TestVariantCollectionTrait();
    $config = [
      'foo' => [
        'id' => 'foo_plugin',
      ],
      'bar' => [
        'id' => 'bar_plugin',
      ],
    ];
    foreach ($config as $value) {
      $plugin = $this->prophesize(VariantInterface::class);
      $this->manager
        ->createInstance($value['id'], $value)
        ->willReturn($plugin->reveal());
    }
    $trait_object->setVariantConfig($config);
    $variants = $trait_object->getVariants();
    $this->assertInstanceOf(VariantPluginCollection::class, $variants);
    $this->assertSame(2, count($variants));
    return $variants;
  }
  
  /**
   * @covers ::getVariants
   *
   * @depends testGetVariants
   */
  public function testGetVariantsSort(VariantPluginCollection $variants) {
    $this->assertEquals([
      'bar' => 'bar',
      'foo' => 'foo',
    ], $variants->getInstanceIds());
  }
  
  /**
   * @covers ::addVariant
   */
  public function testAddVariant() {
    $config = [
      'id' => 'foo',
    ];
    $uuid = 'test-uuid';
    $expected_config = $config + [
      'uuid' => $uuid,
    ];
    $uuid_generator = $this->prophesize(UuidInterface::class);
    $uuid_generator->generate()
      ->willReturn($uuid)
      ->shouldBeCalledTimes(1);
    $trait_object = new TestVariantCollectionTrait();
    $trait_object->setUuidGenerator($uuid_generator->reveal());
    $plugin_prophecy = $this->prophesize(VariantInterface::class);
    $plugin_prophecy->getConfiguration()
      ->willReturn($expected_config)
      ->shouldBeCalled();
    $plugin_prophecy->setConfiguration($expected_config)
      ->willReturn($expected_config)
      ->shouldBeCalled();
    $this->manager
      ->createInstance('foo', $expected_config)
      ->willReturn($plugin_prophecy->reveal());
    $resulting_uuid = $trait_object->addVariant($config);
    $this->assertSame($uuid, $resulting_uuid);
    $variants = $trait_object->getVariants();
    $this->assertSame([
      $uuid => $uuid,
    ], $variants->getInstanceIds());
    $this->assertSame([
      $uuid => $expected_config,
    ], $variants->getConfiguration());
    $this->assertSame($plugin_prophecy->reveal(), $variants->get($uuid));
    return [
      $trait_object,
      $uuid,
      $plugin_prophecy->reveal(),
    ];
  }
  
  /**
   * @covers ::getVariant
   *
   * @depends testAddVariant
   */
  public function testGetVariant($data) {
    [
      $trait_object,
      $uuid,
      $plugin,
    ] = $data;
    $this->manager
      ->createInstance()
      ->shouldNotBeCalled();
    $this->assertSame($plugin, $trait_object->getVariant($uuid));
    return [
      $trait_object,
      $uuid,
    ];
  }
  
  /**
   * @covers ::removeVariant
   *
   * @depends testGetVariant
   */
  public function testRemoveVariant($data) {
    [
      $trait_object,
      $uuid,
    ] = $data;
    $this->assertSame($trait_object, $trait_object->removeVariant($uuid));
    $this->assertFalse($trait_object->getVariants()
      ->has($uuid));
    return [
      $trait_object,
      $uuid,
    ];
  }
  
  /**
   * @covers ::getVariant
   *
   * @depends testRemoveVariant
   */
  public function testGetVariantException($data) {
    [
      $trait_object,
      $uuid,
    ] = $data;
    // Attempt to retrieve a variant that has been removed.
    $this->expectException('\\Drupal\\Component\\Plugin\\Exception\\PluginNotFoundException');
    $this->expectExceptionMessage("Plugin ID 'test-uuid' was not found.");
    $this->assertNull($trait_object->getVariant($uuid));
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function
VariantCollectionTraitTest::$manager protected property
VariantCollectionTraitTest::setUp protected function Overrides UnitTestCase::setUp
VariantCollectionTraitTest::testAddVariant public function @covers ::addVariant[[api-linebreak]]
VariantCollectionTraitTest::testGetVariant public function @covers ::getVariant[[api-linebreak]]
VariantCollectionTraitTest::testGetVariantException public function @covers ::getVariant[[api-linebreak]]
VariantCollectionTraitTest::testGetVariants public function @covers ::getVariants[[api-linebreak]]
VariantCollectionTraitTest::testGetVariantsEmpty public function @covers ::getVariants[[api-linebreak]]
VariantCollectionTraitTest::testGetVariantsSort public function @covers ::getVariants[[api-linebreak]]
VariantCollectionTraitTest::testRemoveVariant public function @covers ::removeVariant[[api-linebreak]]