3 namespace Drupal\Tests\block\Unit;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
7 use Drupal\Tests\UnitTestCase;
10 * @coversDefaultClass \Drupal\block\Entity\Block
13 class BlockConfigEntityUnitTest extends UnitTestCase {
16 * The entity type used for testing.
18 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
20 protected $entityType;
23 * The entity manager used for testing.
25 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
27 protected $entityManager;
30 * The ID of the type of the entity under test.
34 protected $entityTypeId;
37 * The UUID generator used for testing.
39 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
46 protected function setUp() {
47 $this->entityTypeId = $this->randomMachineName();
49 $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
50 $this->entityType->expects($this->any())
51 ->method('getProvider')
52 ->will($this->returnValue('block'));
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));
60 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
62 $container = new ContainerBuilder();
63 $container->set('entity.manager', $this->entityManager);
64 $container->set('uuid', $this->uuid);
65 \Drupal::setContainer($container);
69 * @covers ::calculateDependencies
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'])
78 // Create a configurable plugin that would add a dependency.
79 $instance_id = $this->randomMachineName();
80 $instance = new TestConfigurablePlugin([], $instance_id, ['provider' => 'test']);
82 // Create a plugin collection to contain the instance.
83 $plugin_collection = $this->getMockBuilder('\Drupal\Core\Plugin\DefaultLazyPluginCollection')
84 ->disableOriginalConstructor()
87 $plugin_collection->expects($this->atLeastOnce())
90 ->will($this->returnValue($instance));
91 $plugin_collection->addInstanceId($instance_id);
93 // Return the mocked plugin collection.
94 $entity->expects($this->once())
95 ->method('getPluginCollections')
96 ->will($this->returnValue([$plugin_collection]));
98 $dependencies = $entity->calculateDependencies()->getDependencies();
99 $this->assertContains('test', $dependencies['module']);
100 $this->assertContains('stark', $dependencies['theme']);