comparison core/modules/block/tests/src/Unit/BlockConfigEntityUnitTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\block\Unit;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10 * @coversDefaultClass \Drupal\block\Entity\Block
11 * @group block
12 */
13 class BlockConfigEntityUnitTest extends UnitTestCase {
14
15 /**
16 * The entity type used for testing.
17 *
18 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
19 */
20 protected $entityType;
21
22 /**
23 * The entity manager used for testing.
24 *
25 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
26 */
27 protected $entityManager;
28
29 /**
30 * The ID of the type of the entity under test.
31 *
32 * @var string
33 */
34 protected $entityTypeId;
35
36 /**
37 * The UUID generator used for testing.
38 *
39 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
40 */
41 protected $uuid;
42
43 /**
44 * {@inheritdoc}
45 */
46 protected function setUp() {
47 $this->entityTypeId = $this->randomMachineName();
48
49 $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
50 $this->entityType->expects($this->any())
51 ->method('getProvider')
52 ->will($this->returnValue('block'));
53
54 $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
55 $this->entityManager->expects($this->any())
56 ->method('getDefinition')
57 ->with($this->entityTypeId)
58 ->will($this->returnValue($this->entityType));
59
60 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
61
62 $container = new ContainerBuilder();
63 $container->set('entity.manager', $this->entityManager);
64 $container->set('uuid', $this->uuid);
65 \Drupal::setContainer($container);
66 }
67
68 /**
69 * @covers ::calculateDependencies
70 */
71 public function testCalculateDependencies() {
72 $values = ['theme' => 'stark'];
73 // Mock the entity under test so that we can mock getPluginCollections().
74 $entity = $this->getMockBuilder('\Drupal\block\Entity\Block')
75 ->setConstructorArgs([$values, $this->entityTypeId])
76 ->setMethods(['getPluginCollections'])
77 ->getMock();
78 // Create a configurable plugin that would add a dependency.
79 $instance_id = $this->randomMachineName();
80 $instance = new TestConfigurablePlugin([], $instance_id, ['provider' => 'test']);
81
82 // Create a plugin collection to contain the instance.
83 $plugin_collection = $this->getMockBuilder('\Drupal\Core\Plugin\DefaultLazyPluginCollection')
84 ->disableOriginalConstructor()
85 ->setMethods(['get'])
86 ->getMock();
87 $plugin_collection->expects($this->atLeastOnce())
88 ->method('get')
89 ->with($instance_id)
90 ->will($this->returnValue($instance));
91 $plugin_collection->addInstanceId($instance_id);
92
93 // Return the mocked plugin collection.
94 $entity->expects($this->once())
95 ->method('getPluginCollections')
96 ->will($this->returnValue([$plugin_collection]));
97
98 $dependencies = $entity->calculateDependencies()->getDependencies();
99 $this->assertContains('test', $dependencies['module']);
100 $this->assertContains('stark', $dependencies['theme']);
101 }
102
103 }