View source
<?php
namespace Drupal\Tests\plugin\Unit\PluginDefinition;
use Drupal\Component\Plugin\Derivative\DeriverInterface;
use Drupal\Core\Plugin\Context\ContextDefinitionInterface;
use Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator;
use Drupal\plugin\PluginDefinition\PluginDefinitionInterface;
use Drupal\plugin\PluginOperationsProviderInterface;
use Drupal\Tests\UnitTestCase;
use InvalidArgumentException;
class ArrayPluginDefinitionDecoratorTest extends UnitTestCase {
protected $arrayDefinition = [];
protected $sut;
protected function setUp() : void {
parent::setUp();
$this->arrayDefinition = [
'id' => $this
->randomMachineName(),
'label' => $this
->randomMachineName(),
'description' => $this
->randomMachineName(),
'class' => $this
->getMockClass(DeriverInterface::class),
'category' => $this
->randomMachineName(),
'provider' => $this
->randomMachineName(),
'deriver' => $this
->getMockClass(DeriverInterface::class),
'operations_provider' => $this
->getMockClass(PluginOperationsProviderInterface::class),
'context' => [
$this
->randomMachineName() => $this
->createMock(ContextDefinitionInterface::class),
],
'config_dependencies' => [
'module' => [
$this
->randomMachineName(),
],
],
'parent_id' => $this
->randomMachineName(),
];
$this->sut = new ArrayPluginDefinitionDecorator($this->arrayDefinition);
}
public function testConstruct() {
$this->sut = new ArrayPluginDefinitionDecorator($this->arrayDefinition);
$this
->assertInstanceOf(ArrayPluginDefinitionDecorator::class, $this->sut);
}
public function testGetArrayDefiniton() {
$this
->assertSame($this->arrayDefinition, $this->sut
->getArrayDefinition());
}
public function testGetId() {
$this
->assertSame($this->arrayDefinition['id'], $this->sut
->getId());
$this
->assertSame($this->arrayDefinition['id'], $this->sut
->getArrayDefinition()['id']);
$this
->assertSame($this->arrayDefinition['id'], $this->sut['id']);
$value = $this
->randomMachineName();
$this
->assertSame($this->sut, $this->sut
->setId($value));
$this
->assertSame($value, $this->sut
->getId());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['id']);
$this
->assertSame($value, $this->sut['id']);
$value = $this
->randomMachineName();
$this->sut['id'] = $value;
$this
->assertSame($value, $this->sut
->getId());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['id']);
$this
->assertSame($value, $this->sut['id']);
unset($this->sut['id']);
$this
->assertFalse(isset($this->sut['id']));
$this
->assertNull($this->sut
->getId());
$this
->assertFalse(isset($this->sut
->getArrayDefinition()['id']));
}
public function testGetLabel() {
$this
->assertSame($this->arrayDefinition['label'], $this->sut
->getLabel());
$this
->assertSame($this->arrayDefinition['label'], $this->sut
->getArrayDefinition()['label']);
$this
->assertSame($this->arrayDefinition['label'], $this->sut['label']);
$value = $this
->randomMachineName();
$this
->assertSame($this->sut, $this->sut
->setLabel($value));
$this
->assertSame($value, $this->sut
->getLabel());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['label']);
$this
->assertSame($value, $this->sut['label']);
$value = $this
->randomMachineName();
$this->sut['label'] = $value;
$this
->assertSame($value, $this->sut
->getLabel());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['label']);
$this
->assertSame($value, $this->sut['label']);
unset($this->sut['label']);
$this
->assertFalse(isset($this->sut['label']));
$this
->assertNull($this->sut
->getLabel());
$this
->assertFalse(isset($this->sut
->getArrayDefinition()['label']));
}
public function testGetDescription() {
$this
->assertSame($this->arrayDefinition['description'], $this->sut
->getDescription());
$this
->assertSame($this->arrayDefinition['description'], $this->sut
->getArrayDefinition()['description']);
$this
->assertSame($this->arrayDefinition['description'], $this->sut['description']);
$value = $this
->randomMachineName();
$this
->assertSame($this->sut, $this->sut
->setDescription($value));
$this
->assertSame($value, $this->sut
->getDescription());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['description']);
$this
->assertSame($value, $this->sut['description']);
$value = $this
->randomMachineName();
$this->sut['description'] = $value;
$this
->assertSame($value, $this->sut
->getDescription());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['description']);
$this
->assertSame($value, $this->sut['description']);
unset($this->sut['description']);
$this
->assertFalse(isset($this->sut['description']));
$this
->assertNull($this->sut
->getDescription());
$this
->assertFalse(isset($this->sut
->getArrayDefinition()['description']));
}
public function testGetClass() {
$this
->assertSame($this->arrayDefinition['class'], $this->sut
->getClass());
$this
->assertSame($this->arrayDefinition['class'], $this->sut
->getArrayDefinition()['class']);
$this
->assertSame($this->arrayDefinition['class'], $this->sut['class']);
$value = '\\stdClass';
$this
->assertSame($this->sut, $this->sut
->setClass($value));
$this
->assertSame($value, $this->sut
->getClass());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['class']);
$this
->assertSame($value, $this->sut['class']);
$value = '\\stdClass';
$this->sut['class'] = $value;
$this
->assertSame($value, $this->sut
->getClass());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['class']);
$this
->assertSame($value, $this->sut['class']);
unset($this->sut['class']);
$this
->assertFalse(isset($this->sut['class']));
$this
->assertNull($this->sut
->getClass());
$this
->assertFalse(isset($this->sut
->getArrayDefinition()['class']));
}
public function testGetDeriverClass() {
$this
->assertSame($this->arrayDefinition['deriver'], $this->sut
->getDeriverClass());
$this
->assertSame($this->arrayDefinition['deriver'], $this->sut
->getArrayDefinition()['deriver']);
$this
->assertSame($this->arrayDefinition['deriver'], $this->sut['deriver']);
$value = $this
->getMockClass(DeriverInterface::class);
$this
->assertSame($this->sut, $this->sut
->setDeriverClass($value));
$this
->assertSame($value, $this->sut
->getDeriverClass());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['deriver']);
$this
->assertSame($value, $this->sut['deriver']);
$value = $this
->getMockClass(DeriverInterface::class);
$this->sut['deriver'] = $value;
$this
->assertSame($value, $this->sut
->getDeriverClass());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['deriver']);
$this
->assertSame($value, $this->sut['deriver']);
unset($this->sut['deriver']);
$this
->assertFalse(isset($this->sut['deriver']));
$this
->assertNull($this->sut
->getDeriverClass());
$this
->assertFalse(isset($this->sut
->getArrayDefinition()['deriver']));
}
public function testGetContextDefinitions() {
$this
->assertSame($this->arrayDefinition['context'], $this->sut
->getContextDefinitions());
$this
->assertSame($this->arrayDefinition['context'], $this->sut
->getArrayDefinition()['context']);
$this
->assertSame($this->arrayDefinition['context'], $this->sut['context']);
$context_definition_name_a = $this
->randomMachineName();
$context_definition_a = $this
->createMock(ContextDefinitionInterface::class);
$context_definition_name_b = $this
->randomMachineName();
$context_definition_b = $this
->createMock(ContextDefinitionInterface::class);
$value = [
$context_definition_name_a => $context_definition_a,
$context_definition_name_b => $context_definition_b,
];
$this
->assertSame($this->sut, $this->sut
->setContextDefinitions($value));
$this
->assertSame($value, $this->sut
->getContextDefinitions());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['context']);
$this
->assertSame($value, $this->sut['context']);
$context_definition_name_a = $this
->randomMachineName();
$context_definition_a = $this
->createMock(ContextDefinitionInterface::class);
$context_definition_name_b = $this
->randomMachineName();
$context_definition_b = $this
->createMock(ContextDefinitionInterface::class);
$value = [
$context_definition_name_a => $context_definition_a,
$context_definition_name_b => $context_definition_b,
];
$this->sut['context'] = $value;
$this
->assertSame($value, $this->sut
->getContextDefinitions());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['context']);
$this
->assertSame($value, $this->sut['context']);
unset($this->sut['context']);
$this
->assertFalse(isset($this->sut['context']));
$this
->assertSame([], $this->sut
->getContextDefinitions());
$this
->assertFalse(isset($this->sut
->getArrayDefinition()['context']));
}
public function testSetContextDefinitionsWithInvalidDefinition() {
$this
->expectException(InvalidArgumentException::class);
$context_definitions = [
$this
->randomMachineName() => new \stdClass(),
];
$this->sut['context'] = $context_definitions;
}
public function testGetContextDefinition() {
$name = $this
->randomMachineName();
$context_definition = $this
->createMock(ContextDefinitionInterface::class);
$this
->assertSame($this->sut, $this->sut
->setContextDefinition($name, $context_definition));
$this
->assertSame($context_definition, $this->sut
->getContextDefinition($name));
$this
->assertTrue($this->sut
->hasContextDefinition($name));
}
public function testGetContextDefinitionWithNonExistentDefinition() {
$this
->expectException(InvalidArgumentException::class);
$name = $this
->randomMachineName();
$this
->assertFalse($this->sut
->hasContextDefinition($name));
$this->sut
->getContextDefinition($name);
}
public function testMergeDefaultArrayDefinition() {
$other_definition = [
'foo' => $this
->randomMachineName(),
'label' => $this
->randomMachineName(),
];
$this
->assertSame($this->sut, $this->sut
->mergeDefaultArrayDefinition($other_definition));
$this
->assertSame($other_definition['foo'], $this->sut
->getArrayDefinition()['foo']);
$this
->assertNotSame($other_definition['label'], $this->sut
->getArrayDefinition()['label']);
}
public function testMergeDefaultDefinition() {
$other_definition = new ArrayPluginDefinitionDecorator();
$other_definition['foo'] = $this
->randomMachineName();
$other_definition['label'] = $this
->randomMachineName();
$this
->assertSame($this->sut, $this->sut
->mergeDefaultDefinition($other_definition));
$this
->assertSame($other_definition['foo'], $this->sut
->getArrayDefinition()['foo']);
$this
->assertNotSame($other_definition['label'], $this->sut
->getArrayDefinition()['label']);
}
public function testMergeDefaultDefinitionWithInvalidOtherDefinition() {
$this
->expectException(InvalidArgumentException::class);
$other_definition = $this
->createMock(PluginDefinitionInterface::class);
$this->sut
->mergeDefaultDefinition($other_definition);
}
public function testMergeOverrideArrayDefinition() {
$other_definition = [
'foo' => $this
->randomMachineName(),
'label' => $this
->randomMachineName(),
];
$this
->assertSame($this->sut, $this->sut
->mergeOverrideArrayDefinition($other_definition));
$this
->assertSame($other_definition['foo'], $this->sut
->getArrayDefinition()['foo']);
$this
->assertSame($other_definition['label'], $this->sut
->getArrayDefinition()['label']);
}
public function testMergeOverrideDefinition() {
$other_definition = new ArrayPluginDefinitionDecorator();
$other_definition['foo'] = $this
->randomMachineName();
$other_definition['label'] = $this
->randomMachineName();
$this
->assertSame($this->sut, $this->sut
->mergeOverrideDefinition($other_definition));
$this
->assertSame($other_definition['foo'], $this->sut
->getArrayDefinition()['foo']);
$this
->assertSame($other_definition['label'], $this->sut
->getArrayDefinition()['label']);
}
public function testMergeOverrideDefinitionWithInvalidOtherDefinition() {
$this
->expectException(InvalidArgumentException::class);
$other_definition = $this
->createMock(PluginDefinitionInterface::class);
$this->sut
->mergeOverrideDefinition($other_definition);
}
public function testGetCategory() {
$this
->assertSame($this->arrayDefinition['category'], $this->sut
->getCategory());
$this
->assertSame($this->arrayDefinition['category'], $this->sut
->getArrayDefinition()['category']);
$this
->assertSame($this->arrayDefinition['category'], $this->sut['category']);
$value = $this
->randomMachineName();
$this
->assertSame($this->sut, $this->sut
->setCategory($value));
$this
->assertSame($value, $this->sut
->getCategory());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['category']);
$this
->assertSame($value, $this->sut['category']);
$value = $this
->randomMachineName();
$this->sut['category'] = $value;
$this
->assertSame($value, $this->sut
->getCategory());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['category']);
$this
->assertSame($value, $this->sut['category']);
unset($this->sut['category']);
$this
->assertFalse(isset($this->sut['category']));
$this
->assertNull($this->sut
->getCategory());
$this
->assertFalse(isset($this->sut
->getArrayDefinition()['category']));
}
public function testGetProvider() {
$this
->assertSame($this->arrayDefinition['provider'], $this->sut
->getProvider());
$this
->assertSame($this->arrayDefinition['provider'], $this->sut
->getArrayDefinition()['provider']);
$this
->assertSame($this->arrayDefinition['provider'], $this->sut['provider']);
$value = $this
->randomMachineName();
$this
->assertSame($this->sut, $this->sut
->setProvider($value));
$this
->assertSame($value, $this->sut
->getProvider());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['provider']);
$this
->assertSame($value, $this->sut['provider']);
$value = $this
->randomMachineName();
$this->sut['provider'] = $value;
$this
->assertSame($value, $this->sut
->getProvider());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['provider']);
$this
->assertSame($value, $this->sut['provider']);
unset($this->sut['provider']);
$this
->assertFalse(isset($this->sut['provider']));
$this
->assertNull($this->sut
->getProvider());
$this
->assertFalse(isset($this->sut
->getArrayDefinition()['provider']));
}
public function testGetParentId() {
$this
->assertSame($this->arrayDefinition['parent_id'], $this->sut
->getParentId());
$this
->assertSame($this->arrayDefinition['parent_id'], $this->sut
->getArrayDefinition()['parent_id']);
$this
->assertSame($this->arrayDefinition['parent_id'], $this->sut['parent_id']);
$value = $this
->randomMachineName();
$this
->assertSame($this->sut, $this->sut
->setParentId($value));
$this
->assertSame($value, $this->sut
->getParentId());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['parent_id']);
$this
->assertSame($value, $this->sut['parent_id']);
$value = $this
->randomMachineName();
$this->sut['parent_id'] = $value;
$this
->assertSame($value, $this->sut
->getParentId());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['parent_id']);
$this
->assertSame($value, $this->sut['parent_id']);
unset($this->sut['parent_id']);
$this
->assertFalse(isset($this->sut['parent_id']));
$this
->assertNull($this->sut
->getParentId());
$this
->assertFalse(isset($this->sut
->getArrayDefinition()['parent_id']));
}
public function testGetConfigDependencies() {
$this
->assertSame($this->arrayDefinition['config_dependencies'], $this->sut
->getConfigDependencies());
$this
->assertSame($this->arrayDefinition['config_dependencies'], $this->sut
->getArrayDefinition()['config_dependencies']);
$this
->assertSame($this->arrayDefinition['config_dependencies'], $this->sut['config_dependencies']);
$value = [
'module' => [
$this
->randomMachineName(),
],
];
$this
->assertSame($this->sut, $this->sut
->setConfigDependencies($value));
$this
->assertSame($value, $this->sut
->getConfigDependencies());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['config_dependencies']);
$this
->assertSame($value, $this->sut['config_dependencies']);
$value = [
'module' => [
$this
->randomMachineName(),
],
];
$this->sut['config_dependencies'] = $value;
$this
->assertSame($value, $this->sut
->getConfigDependencies());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['config_dependencies']);
$this
->assertSame($value, $this->sut['config_dependencies']);
unset($this->sut['config_dependencies']);
$this
->assertFalse(isset($this->sut['config_dependencies']));
$this
->assertSame([], $this->sut
->getConfigDependencies());
$this
->assertFalse(isset($this->sut
->getArrayDefinition()['config_dependencies']));
}
public function testCount() {
$this
->assertSame(count($this->arrayDefinition), count($this->sut));
}
public function testGetIterator() {
foreach ($this->sut as $key => $value) {
$this
->assertSame($this->arrayDefinition[$key], $value);
}
}
public function testCreateFromDecoratedDefinition() {
$this
->assertInstanceOf(ArrayPluginDefinitionDecorator::class, ArrayPluginDefinitionDecorator::createFromDecoratedDefinition($this->arrayDefinition));
}
public function testCreateFromDecoratedDefinitionWithInvalidDecoratedDefinition($decorated_definition) {
$this
->expectException(InvalidArgumentException::class);
$this
->assertInstanceOf(ArrayPluginDefinitionDecorator::class, ArrayPluginDefinitionDecorator::createFromDecoratedDefinition($decorated_definition));
}
public function providerCreateFromDecoratedDefinitionWithInvalidDecoratedDefinition() {
return [
[
$this
->randomMachineName(),
],
[
new \stdClass(),
],
[
new ArrayPluginDefinitionDecorator(),
],
];
}
public function testGetOperationsProviderClass() {
$this
->assertSame($this->arrayDefinition['operations_provider'], $this->sut
->getOperationsProviderClass());
$this
->assertSame($this->arrayDefinition['operations_provider'], $this->sut
->getArrayDefinition()['operations_provider']);
$this
->assertSame($this->arrayDefinition['operations_provider'], $this->sut['operations_provider']);
$value = $this
->getMockClass(PluginOperationsProviderInterface::class);
$this
->assertSame($this->sut, $this->sut
->setOperationsProviderClass($value));
$this
->assertSame($value, $this->sut
->getOperationsProviderClass());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['operations_provider']);
$this
->assertSame($value, $this->sut['operations_provider']);
$value = $this
->getMockClass(PluginOperationsProviderInterface::class);
$this->sut['operations_provider'] = $value;
$this
->assertSame($value, $this->sut
->getOperationsProviderClass());
$this
->assertSame($value, $this->sut
->getArrayDefinition()['operations_provider']);
$this
->assertSame($value, $this->sut['operations_provider']);
unset($this->sut['operations_provider']);
$this
->assertFalse(isset($this->sut['operations_provider']));
$this
->assertNull($this->sut
->getOperationsProviderClass());
$this
->assertFalse(isset($this->sut
->getArrayDefinition()['operations_provider']));
}
}