You are here

class PdbBlockDeriverTest in Decoupled Blocks 8

@coversDefaultClass \Drupal\pdb\Plugin\Derivative\PdbBlockDeriver @group pdb

Hierarchy

Expanded class hierarchy of PdbBlockDeriverTest

File

tests/src/Unit/Plugin/Derivative/PdbBlockDeriverTest.php, line 14

Namespace

Drupal\Tests\pdb\Unit\Plugin\Derivative
View source
class PdbBlockDeriverTest extends UnitTestCase {

  /**
   * Mocked Component Discovery.
   *
   * @var \Drupal\pdb\ComponentDiscoveryInterface
   */
  protected $componentDiscovery;

  /**
   * Instance of the Block Deriver.
   *
   * @var \Drupal\pdb\Plugin\Derivative\PdbBlockDeriver
   */
  protected $deriver;

  /**
   * Create the setup for constants.
   */
  protected function setUp() {
    parent::setUp();

    // Mock the UUID service.
    $this->componentDiscovery = $this
      ->prophesize(ComponentDiscoveryInterface::CLASS);
    $this->componentDiscovery
      ->getComponents()
      ->willReturn([
      'block_1' => (object) [
        'type' => 'pdb',
        'info' => [
          'name' => 'Block 1',
          'machine_name' => 'block_1',
          'presentation' => 'pdb',
          'contexts' => [
            'entity' => 'node',
          ],
        ],
      ],
    ]);
    $this->deriver = new PdbBlockDeriver($this->componentDiscovery
      ->reveal());
  }

  /**
   * Tests the create method.
   *
   * @see ::create()
   */
  public function testCreate() {
    $base_plugin_id = 'pdb';
    $container = $this
      ->prophesize(ContainerInterface::CLASS);
    $container
      ->get('pdb.component_discovery')
      ->willReturn($this->componentDiscovery);
    $instance = PdbBlockDeriver::create($container
      ->reveal(), $base_plugin_id);
    $this
      ->assertInstanceOf('Drupal\\pdb\\Plugin\\Derivative\\PdbBlockDeriver', $instance);
  }

  /**
   * Tests the getDerivativeDefinitions() method.
   */
  public function testGetDerivativeDefinitions() {
    $base_plugin_definition = [
      'provider' => 'pdb',
    ];

    // example_1 should not appear due to debug mode being off.
    $expected = [
      'block_1' => [
        'info' => [
          'name' => 'Block 1',
          'machine_name' => 'block_1',
          'presentation' => 'pdb',
          'contexts' => [
            'entity' => 'node',
          ],
        ],
        'provider' => 'pdb',
        'admin_label' => 'Block 1',
        'cache' => [
          'max-age' => 0,
        ],
      ],
    ];
    $return = $this->deriver
      ->getDerivativeDefinitions($base_plugin_definition);
    $this
      ->assertInstanceOf('Drupal\\Core\\Plugin\\Context\\EntityContextDefinition', $return['block_1']['context_definitions']['entity']);

    // Remove the context to compare the arrays.
    unset($return['block_1']['context_definitions']);
    $this
      ->assertEquals($expected, $return);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PdbBlockDeriverTest::$componentDiscovery protected property Mocked Component Discovery.
PdbBlockDeriverTest::$deriver protected property Instance of the Block Deriver.
PdbBlockDeriverTest::setUp protected function Create the setup for constants. Overrides UnitTestCase::setUp
PdbBlockDeriverTest::testCreate public function Tests the create method.
PdbBlockDeriverTest::testGetDerivativeDefinitions public function Tests the getDerivativeDefinitions() method.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 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::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.