You are here

class BlockConfigEntityUnitTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/block/tests/src/Unit/BlockConfigEntityUnitTest.php \Drupal\Tests\block\Unit\BlockConfigEntityUnitTest

@coversDefaultClass \Drupal\block\Entity\Block @group block

Hierarchy

Expanded class hierarchy of BlockConfigEntityUnitTest

File

core/modules/block/tests/src/Unit/BlockConfigEntityUnitTest.php, line 18
Contains \Drupal\Tests\block\Unit\BlockConfigEntityUnitTest.

Namespace

Drupal\Tests\block\Unit
View source
class BlockConfigEntityUnitTest extends UnitTestCase {

  /**
   * The entity type used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityType;

  /**
   * The entity manager used for testing.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityManager;

  /**
   * The ID of the type of the entity under test.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * The UUID generator used for testing.
   *
   * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $uuid;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->entityTypeId = $this
      ->randomMachineName();
    $this->entityType = $this
      ->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
    $this->entityType
      ->expects($this
      ->any())
      ->method('getProvider')
      ->will($this
      ->returnValue('block'));
    $this->entityManager = $this
      ->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface');
    $this->entityManager
      ->expects($this
      ->any())
      ->method('getDefinition')
      ->with($this->entityTypeId)
      ->will($this
      ->returnValue($this->entityType));
    $this->uuid = $this
      ->getMock('\\Drupal\\Component\\Uuid\\UuidInterface');
    $container = new ContainerBuilder();
    $container
      ->set('entity.manager', $this->entityManager);
    $container
      ->set('uuid', $this->uuid);
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependencies() {
    $values = array(
      'theme' => 'stark',
    );

    // Mock the entity under test so that we can mock getPluginCollections().
    $entity = $this
      ->getMockBuilder('\\Drupal\\block\\Entity\\Block')
      ->setConstructorArgs(array(
      $values,
      $this->entityTypeId,
    ))
      ->setMethods(array(
      'getPluginCollections',
    ))
      ->getMock();

    // Create a configurable plugin that would add a dependency.
    $instance_id = $this
      ->randomMachineName();
    $instance = new TestConfigurablePlugin(array(), $instance_id, array(
      'provider' => 'test',
    ));

    // Create a plugin collection to contain the instance.
    $plugin_collection = $this
      ->getMockBuilder('\\Drupal\\Core\\Plugin\\DefaultLazyPluginCollection')
      ->disableOriginalConstructor()
      ->setMethods(array(
      'get',
    ))
      ->getMock();
    $plugin_collection
      ->expects($this
      ->atLeastOnce())
      ->method('get')
      ->with($instance_id)
      ->will($this
      ->returnValue($instance));
    $plugin_collection
      ->addInstanceId($instance_id);

    // Return the mocked plugin collection.
    $entity
      ->expects($this
      ->once())
      ->method('getPluginCollections')
      ->will($this
      ->returnValue(array(
      $plugin_collection,
    )));
    $dependencies = $entity
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertContains('test', $dependencies['module']);
    $this
      ->assertContains('stark', $dependencies['theme']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockConfigEntityUnitTest::$entityManager protected property The entity manager used for testing.
BlockConfigEntityUnitTest::$entityType protected property The entity type used for testing.
BlockConfigEntityUnitTest::$entityTypeId protected property The ID of the type of the entity under test.
BlockConfigEntityUnitTest::$uuid protected property The UUID generator used for testing.
BlockConfigEntityUnitTest::setUp protected function Overrides UnitTestCase::setUp
BlockConfigEntityUnitTest::testCalculateDependencies public function @covers ::calculateDependencies
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.