function VariantCollectionTraitTest::testAddVariant

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

@covers ::addVariant

File

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

Class

VariantCollectionTraitTest
Tests the methods of a variant-aware class.

Namespace

Drupal\Tests\ctools\Unit

Code

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(),
  ];
}