Issue #2076681 by damiankloip, dawehner, pwolanin: Add a YAML discovery decorator.

8.0.x
Alex Pott 2013-09-06 11:07:06 +02:00
parent db2d92e7cb
commit 36a898ebea
2 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,58 @@
<?php
/**
* @file
* Contains \Drupal\Core\Plugin\Discovery\YamlDiscoveryDecorator.
*/
namespace Drupal\Core\Plugin\Discovery;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
/**
* Enables YAML discovery for plugin definitions.
*
* You should normally extend this class to add validation for the values in the
* YAML data or to restrict use of the class or derivatives keys.
*/
class YamlDiscoveryDecorator extends YamlDiscovery {
/**
* The Discovery object being decorated.
*
* @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
*/
protected $decorated;
/**
* Constructs a YamlDiscoveryDecorator object.
*
* @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $decorated
* The discovery object that is being decorated.
* @param string $name
* The file name suffix to use for discovery. E.g. 'test' will become
* 'MODULE.test.yml'.
* @param array $directories
* An array of directories to scan.
*/
public function __construct(DiscoveryInterface $decorated, $name, array $directories) {
parent::__construct($name, $directories);
$this->decorated = $decorated;
}
/**
* {@inheritdoc}
*/
public function getDefinitions() {
return parent::getDefinitions() + $this->decorated->getDefinitions();
}
/**
* Passes through all unknown calls onto the decorated object.
*/
public function __call($method, $args) {
return call_user_func_array(array($this->decorated, $method), $args);
}
}

View File

@ -0,0 +1,100 @@
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Plugin\Discovery\YamlDiscoveryDecoratorTest.
*/
namespace Drupal\Tests\Core\Plugin\Discovery;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Plugin\Discovery\YamlDiscoveryDecorator;
/**
* Tests the YamlDiscoveryDecorator class.
*/
class YamlDiscoveryDecoratorTest extends UnitTestCase {
/**
* The YamlDiscovery instance to test.
*
* @var \Drupal\Core\Plugin\Discovery\YamlDiscoveryDecorator
*/
protected $discoveryDecorator;
/**
* Expected provider => key mappings for testing.
*
* @var array
*/
protected $expectedKeys = array(
'test_1' => 'test_1_a',
'another_provider_1' => 'test_1_b',
'another_provider_2' => 'test_2_a',
'test_2' => 'test_2_b',
'decorated_1' => 'decorated_test_1',
'decorated_2' => 'decorated_test_2',
);
public static function getInfo() {
return array(
'name' => 'YamlDiscoveryDecorator',
'description' => 'YamlDiscoveryDecorator unit tests.',
'group' => 'Plugin',
);
}
public function setUp() {
parent::setUp();
$base_path = __DIR__ . '/Fixtures';
// Set up the directories to search.
$directories = array(
'test_1' => $base_path . '/test_1',
'test_2' => $base_path . '/test_2',
);
$definitions = array(
'decorated_test_1' => array(
'id' => 'decorated_test_1',
'name' => 'Decorated test 1',
'provider' => 'decorated_1',
),
'decorated_test_2' => array(
'id' => 'decorated_test_2',
'name' => 'Decorated test 1',
'provider' => 'decorated_2',
),
);
$decorated = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
$decorated->expects($this->once())
->method('getDefinitions')
->will($this->returnValue($definitions));
$this->discoveryDecorator = new YamlDiscoveryDecorator($decorated, 'test', $directories);
}
/**
* Tests the getDefinitions() method.
*/
public function testGetDefinitions() {
$definitions = $this->discoveryDecorator->getDefinitions();
$this->assertInternalType('array', $definitions);
$this->assertCount(6, $definitions);
foreach ($this->expectedKeys as $expected_key) {
$this->assertArrayHasKey($expected_key, $definitions);
}
foreach ($definitions as $id => $definition) {
foreach (array('name', 'id', 'provider') as $key) {
$this->assertArrayHasKey($key, $definition);
}
$this->assertEquals($id, $definition['id']);
$this->assertEquals(array_search($id, $this->expectedKeys), $definition['provider']);
}
}
}