View source
<?php
namespace Drupal\Tests\Component\Plugin\Discovery;
use PHPUnit\Framework\TestCase;
class StaticDiscoveryDecoratorTest extends TestCase {
public function getRegisterDefinitionsCallback() {
$mock_callable = $this
->getMockBuilder('\\stdClass')
->setMethods([
'registerDefinitionsCallback',
])
->getMock();
$mock_callable
->expects($this
->once())
->method('registerDefinitionsCallback');
return $mock_callable;
}
public function providerGetDefinition() {
return [
[
'is_defined',
TRUE,
FALSE,
[
'plugin-definition' => 'is_defined',
],
'plugin-definition',
],
[
'is_defined',
FALSE,
FALSE,
[
'plugin-definition' => 'is_defined',
],
'plugin-definition',
],
[
NULL,
FALSE,
FALSE,
[
'plugin-definition' => 'is_defined',
],
'BAD-plugin-definition',
],
[
NULL,
FALSE,
TRUE,
[
'plugin-definition' => 'is_defined',
],
'BAD-plugin-definition',
],
];
}
public function testGetDefinition($expected, $has_register_definitions, $exception_on_invalid, $definitions, $base_plugin_id) {
$mock_decorator = $this
->getMockBuilder('Drupal\\Component\\Plugin\\Discovery\\StaticDiscoveryDecorator')
->disableOriginalConstructor()
->setMethods([
'registeredDefinitionCallback',
])
->getMock();
$ref_register_definitions = new \ReflectionProperty($mock_decorator, 'registerDefinitions');
$ref_register_definitions
->setAccessible(TRUE);
if ($has_register_definitions) {
$ref_register_definitions
->setValue($mock_decorator, [
$this
->getRegisterDefinitionsCallback(),
'registerDefinitionsCallback',
]);
}
else {
$ref_register_definitions
->setValue($mock_decorator, NULL);
}
$ref_definitions = new \ReflectionProperty($mock_decorator, 'definitions');
$ref_definitions
->setAccessible(TRUE);
$ref_definitions
->setValue($mock_decorator, []);
$mock_decorated = $this
->getMockBuilder('Drupal\\Component\\Plugin\\Discovery\\DiscoveryInterface')
->setMethods([
'getDefinitions',
])
->getMockForAbstractClass();
$mock_decorated
->expects($this
->once())
->method('getDefinitions')
->willReturn($definitions);
$ref_decorated = new \ReflectionProperty($mock_decorator, 'decorated');
$ref_decorated
->setAccessible(TRUE);
$ref_decorated
->setValue($mock_decorator, $mock_decorated);
if ($exception_on_invalid) {
$this
->expectException('Drupal\\Component\\Plugin\\Exception\\PluginNotFoundException');
}
$this
->assertEquals($expected, $mock_decorator
->getDefinition($base_plugin_id, $exception_on_invalid));
}
public function providerGetDefinitions() {
return [
[
TRUE,
[
'definition' => 'is_fake',
],
],
[
FALSE,
[
'definition' => 'array_of_stuff',
],
],
];
}
public function testGetDefinitions($has_register_definitions, $definitions) {
$mock_decorator = $this
->getMockBuilder('Drupal\\Component\\Plugin\\Discovery\\StaticDiscoveryDecorator')
->disableOriginalConstructor()
->setMethods([
'registeredDefinitionCallback',
])
->getMock();
$ref_register_definitions = new \ReflectionProperty($mock_decorator, 'registerDefinitions');
$ref_register_definitions
->setAccessible(TRUE);
if ($has_register_definitions) {
$ref_register_definitions
->setValue($mock_decorator, [
$this
->getRegisterDefinitionsCallback(),
'registerDefinitionsCallback',
]);
}
else {
$ref_register_definitions
->setValue($mock_decorator, NULL);
}
$ref_definitions = new \ReflectionProperty($mock_decorator, 'definitions');
$ref_definitions
->setAccessible(TRUE);
$ref_definitions
->setValue($mock_decorator, []);
$mock_decorated = $this
->getMockBuilder('Drupal\\Component\\Plugin\\Discovery\\DiscoveryInterface')
->setMethods([
'getDefinitions',
])
->getMockForAbstractClass();
$mock_decorated
->expects($this
->once())
->method('getDefinitions')
->willReturn($definitions);
$ref_decorated = new \ReflectionProperty($mock_decorator, 'decorated');
$ref_decorated
->setAccessible(TRUE);
$ref_decorated
->setValue($mock_decorator, $mock_decorated);
$this
->assertEquals($definitions, $mock_decorator
->getDefinitions());
}
public function providerCall() {
return [
[
'complexArguments',
[
'1',
2.0,
3,
[
'4' => 'five',
],
],
],
[
'noArguments',
[],
],
];
}
public function testCall($method, $args) {
$mock_decorated = $this
->getMockBuilder('Drupal\\Component\\Plugin\\Discovery\\DiscoveryInterface')
->setMethods([
$method,
])
->getMockForAbstractClass();
$mock_decorated
->expects($this
->once())
->method($method)
->willReturnCallback(function () {
return \func_get_args();
});
$mock_decorator = $this
->getMockBuilder('Drupal\\Component\\Plugin\\Discovery\\StaticDiscoveryDecorator')
->disableOriginalConstructor()
->getMock();
$ref_decorated = new \ReflectionProperty($mock_decorator, 'decorated');
$ref_decorated
->setAccessible(TRUE);
$ref_decorated
->setValue($mock_decorator, $mock_decorated);
$this
->assertEquals($args, \call_user_func_array([
$mock_decorated,
$method,
], $args));
}
}