ea1936856676c33a48f96846ea15fa1af4323b93
[yaffs-website] / YamlDiscoveryDecoratorTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin\Discovery;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Core\Plugin\Discovery\YamlDiscoveryDecorator;
7
8 /**
9  * YamlDiscoveryDecorator unit tests.
10  *
11  * @group Plugin
12  */
13 class YamlDiscoveryDecoratorTest extends UnitTestCase {
14
15   /**
16    * The YamlDiscovery instance to test.
17    *
18    * @var \Drupal\Core\Plugin\Discovery\YamlDiscoveryDecorator
19    */
20   protected $discoveryDecorator;
21
22   /**
23    * Expected provider => key mappings for testing.
24    *
25    * @var array
26    */
27   protected $expectedKeys = [
28     'test_1' => 'test_1_a',
29     'another_provider_1' => 'test_1_b',
30     'another_provider_2' => 'test_2_a',
31     'test_2' => 'test_2_b',
32     'decorated_1' => 'decorated_test_1',
33     'decorated_2' => 'decorated_test_2',
34   ];
35
36   protected function setUp() {
37     parent::setUp();
38
39     $base_path = __DIR__ . '/Fixtures';
40     // Set up the directories to search.
41     $directories = [
42       'test_1' => $base_path . '/test_1',
43       'test_2' => $base_path . '/test_2',
44     ];
45
46     $definitions = [
47       'decorated_test_1' => [
48         'id' => 'decorated_test_1',
49         'name' => 'Decorated test 1',
50         'provider' => 'decorated_1',
51       ],
52       'decorated_test_2' => [
53         'id' => 'decorated_test_2',
54         'name' => 'Decorated test 1',
55         'provider' => 'decorated_2',
56       ],
57     ];
58
59     $decorated = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
60     $decorated->expects($this->once())
61       ->method('getDefinitions')
62       ->will($this->returnValue($definitions));
63
64     $this->discoveryDecorator = new YamlDiscoveryDecorator($decorated, 'test', $directories);
65   }
66
67   /**
68    * Tests the getDefinitions() method.
69    */
70   public function testGetDefinitions() {
71     $definitions = $this->discoveryDecorator->getDefinitions();
72
73     $this->assertInternalType('array', $definitions);
74     $this->assertCount(6, $definitions);
75
76     foreach ($this->expectedKeys as $expected_key) {
77       $this->assertArrayHasKey($expected_key, $definitions);
78     }
79
80     foreach ($definitions as $id => $definition) {
81       foreach (['name', 'id', 'provider'] as $key) {
82         $this->assertArrayHasKey($key, $definition);
83       }
84       $this->assertEquals($id, $definition['id']);
85       $this->assertEquals(array_search($id, $this->expectedKeys), $definition['provider']);
86     }
87   }
88
89 }