class BlockConfigEntityUnitTest

Same name in other branches
  1. 9 core/modules/block/tests/src/Unit/BlockConfigEntityUnitTest.php \Drupal\Tests\block\Unit\BlockConfigEntityUnitTest
  2. 8.9.x core/modules/block/tests/src/Unit/BlockConfigEntityUnitTest.php \Drupal\Tests\block\Unit\BlockConfigEntityUnitTest
  3. 11.x 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

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 type manager used for testing.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityTypeManager;
    
    /**
     * 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;
    
    /**
     * The module handler.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface|\Prophecy\Prophecy\ProphecyInterface
     */
    protected $moduleHandler;
    
    /**
     * The theme handler.
     *
     * @var \Drupal\Core\Extension\ThemeHandlerInterface|\Prophecy\Prophecy\ProphecyInterface
     */
    protected $themeHandler;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->entityTypeId = $this->randomMachineName();
        $this->entityType = $this->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
        $this->entityType
            ->expects($this->any())
            ->method('getProvider')
            ->willReturn('block');
        $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
        $this->entityTypeManager
            ->expects($this->any())
            ->method('getDefinition')
            ->with($this->entityTypeId)
            ->willReturn($this->entityType);
        $this->uuid = $this->createMock('\\Drupal\\Component\\Uuid\\UuidInterface');
        $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
        $this->themeHandler = $this->prophesize(ThemeHandlerInterface::class);
        $container = new ContainerBuilder();
        $container->set('entity_type.manager', $this->entityTypeManager);
        $container->set('module_handler', $this->moduleHandler
            ->reveal());
        $container->set('theme_handler', $this->themeHandler
            ->reveal());
        $container->set('uuid', $this->uuid);
        \Drupal::setContainer($container);
    }
    
    /**
     * @covers ::calculateDependencies
     */
    public function testCalculateDependencies() : void {
        $this->themeHandler
            ->themeExists('stark')
            ->willReturn(TRUE);
        $values = [
            'theme' => 'stark',
        ];
        // Mock the entity under test so that we can mock getPluginCollections().
        $entity = $this->getMockBuilder('\\Drupal\\block\\Entity\\Block')
            ->setConstructorArgs([
            $values,
            $this->entityTypeId,
        ])
            ->onlyMethods([
            'getPluginCollections',
        ])
            ->getMock();
        // Create a configurable plugin that would add a dependency.
        $instance_id = $this->randomMachineName();
        $this->moduleHandler
            ->moduleExists('test')
            ->willReturn(TRUE);
        $instance = new TestConfigurablePlugin([], $instance_id, [
            'provider' => 'test',
        ]);
        // Create a plugin collection to contain the instance.
        $plugin_collection = $this->getMockBuilder('\\Drupal\\Core\\Plugin\\DefaultLazyPluginCollection')
            ->disableOriginalConstructor()
            ->onlyMethods([
            'get',
        ])
            ->getMock();
        $plugin_collection->expects($this->atLeastOnce())
            ->method('get')
            ->with($instance_id)
            ->willReturn($instance);
        $plugin_collection->addInstanceId($instance_id);
        // Return the mocked plugin collection.
        $entity->expects($this->once())
            ->method('getPluginCollections')
            ->willReturn([
            $plugin_collection,
        ]);
        $dependencies = $entity->calculateDependencies()
            ->getDependencies();
        $this->assertContains('test', $dependencies['module']);
        $this->assertContains('stark', $dependencies['theme']);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
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::$entityTypeManager protected property The entity type manager used for testing.
BlockConfigEntityUnitTest::$moduleHandler protected property The module handler.
BlockConfigEntityUnitTest::$themeHandler protected property The theme handler.
BlockConfigEntityUnitTest::$uuid protected property The UUID generator used for testing.
BlockConfigEntityUnitTest::setUp protected function Overrides UnitTestCase::setUp
BlockConfigEntityUnitTest::testCalculateDependencies public function @covers ::calculateDependencies
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed 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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.