Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / Discovery / ContainerDerivativeDiscoveryDecoratorTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin\Discovery;
4
5 use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator
10  * @group Plugin
11  */
12 class ContainerDerivativeDiscoveryDecoratorTest extends UnitTestCase {
13
14   /**
15    * @covers ::getDefinitions
16    */
17   public function testGetDefinitions() {
18     $example_service = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
19     $example_container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
20       ->setMethods(['get'])
21       ->getMock();
22     $example_container->expects($this->once())
23       ->method('get')
24       ->with($this->equalTo('example_service'))
25       ->will($this->returnValue($example_service));
26
27     \Drupal::setContainer($example_container);
28
29     $definitions = [];
30     $definitions['container_aware_discovery'] = [
31       'id' => 'container_aware_discovery',
32       'deriver' => '\Drupal\Tests\Core\Plugin\Discovery\TestContainerDerivativeDiscovery',
33     ];
34     $definitions['non_container_aware_discovery'] = [
35       'id' => 'non_container_aware_discovery',
36       'deriver' => '\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery',
37     ];
38
39     $discovery_main = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
40     $discovery_main->expects($this->any())
41       ->method('getDefinitions')
42       ->will($this->returnValue($definitions));
43
44     $discovery = new ContainerDerivativeDiscoveryDecorator($discovery_main);
45     $definitions = $discovery->getDefinitions();
46
47     // Ensure that both the instances from container and non-container test derivatives got added.
48     $this->assertEquals(4, count($definitions));
49   }
50
51 }