You are here

class ReferenceTest in YAML Content 8

Test entity reference processing.

@group yaml_content

@coversDefaultClass \Drupal\yaml_content\Plugin\yaml_content\process\Reference

Hierarchy

Expanded class hierarchy of ReferenceTest

File

tests/src/Unit/Plugin/yaml_content/process/ReferenceTest.php, line 22

Namespace

Drupal\Tests\yaml_content\Unit\Plugin\yaml_content\process
View source
class ReferenceTest extends UnitTestCase {

  /**
   * The entity type manager service mock.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $entityTypeManager;

  /**
   * The entity storage handler mock.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $entityStorageHandler;

  /**
   * The reference process plugin being tested.
   *
   * @var \Drupal\yaml_content\Plugin\yaml_content\process\Reference
   */
  protected $reference;

  /**
   * Setup mocks and a reference plugin for all tests.
   */
  public function setUp() {
    parent::setUp();
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->entityStorageHandler = $this
      ->prophesize(EntityStorageInterface::class);
    $this->entityTypeManager
      ->getStorage(Argument::type('string'))
      ->willReturn($this->entityStorageHandler
      ->reveal());
    $this->reference = new Reference([
      'my_entity',
      [
        'title' => 'My First Blog Post',
      ],
    ], 'reference', [], $this->entityTypeManager
      ->reveal());
  }

  /**
   * Reference processing returns a reference to existing matching entities.
   *
   * @covers ::process
   */
  public function testProcessExisting() {
    $data = [
      '#process' => [
        'callback' => 'reference',
      ],
    ];

    // Mock a query so we can control and assert the mapping of arguments to entity.
    $query = $this
      ->prophesize(QueryInterface::class);
    $query
      ->condition('title', 'My First Blog Post')
      ->shouldBeCalled();
    $query
      ->execute()
      ->shouldBeCalled()
      ->willReturn([
      1,
    ]);
    $this->entityStorageHandler
      ->getQuery('AND')
      ->shouldBeCalled()
      ->willReturn($query
      ->reveal());

    // Create should not be called.
    $this->entityStorageHandler
      ->create(Argument::any())
      ->shouldNotBeCalled();
    $this->reference
      ->process(new ProcessingContext(), $data);
    $this
      ->assertArrayEquals([
      'target_id' => 1,
    ], $data);
  }

  /**
   * Reference processing should not create new entity if no matches are found.
   *
   * @covers ::process
   */
  public function testProcessCreate() {
    $data = [
      '#process' => [
        'callback' => 'reference',
      ],
    ];

    // Mock a query so we can control and assert the mapping of arguments to entity.
    $query = $this
      ->prophesize(QueryInterface::class);
    $query
      ->condition('title', 'My First Blog Post')
      ->shouldBeCalled();
    $query
      ->execute()
      ->shouldBeCalled()
      ->willReturn();
    $this->entityStorageHandler
      ->getQuery('AND')
      ->shouldBeCalled()
      ->willReturn($query
      ->reveal());

    // Stub an existing entity since entities are complex and db bound.
    $entity = $this
      ->prophesize(EntityInterface::class);
    $entity
      ->id()
      ->willReturn(2);

    // Create should not be called.
    $this->entityStorageHandler
      ->create(Argument::any())
      ->shouldBeCalled()
      ->willReturn($entity);
    $this->reference
      ->process(new ProcessingContext(), $data);
    $this
      ->assertArrayEquals([
      'target_id' => 2,
    ], $data);
  }

  /**
   * Test reference processing failure.
   */
  public function testProcessCreateFail() {
    $data = [
      '#process' => [
        'callback' => 'reference',
      ],
    ];

    // Mock a query so we can control and assert the mapping of arguments to entity.
    $query = $this
      ->prophesize(QueryInterface::class);
    $query
      ->condition('title', 'My First Blog Post')
      ->shouldBeCalled();
    $query
      ->execute()
      ->shouldBeCalled()
      ->willReturn();
    $this->entityStorageHandler
      ->getQuery('AND')
      ->shouldBeCalled()
      ->willReturn($query
      ->reveal());

    // Stub an existing entity since entities are complex and db bound.
    $entity = $this
      ->prophesize(EntityInterface::class);
    $entity
      ->id()
      ->willReturn(NULL);
    $this
      ->markTestIncomplete('How do we fail? Id will always be called and the array will always have something and not be empty.');

    // Create should not be called.
    $this->entityStorageHandler
      ->create(Argument::any())
      ->shouldBeCalled()
      ->willReturn($entity);
    $this->reference
      ->process(new ProcessingContext(), $data);
    $this
      ->assertArrayEquals([
      'target_id' => 2,
    ], $data);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
ReferenceTest::$entityStorageHandler protected property The entity storage handler mock.
ReferenceTest::$entityTypeManager protected property The entity type manager service mock.
ReferenceTest::$reference protected property The reference process plugin being tested.
ReferenceTest::setUp public function Setup mocks and a reference plugin for all tests. Overrides UnitTestCase::setUp
ReferenceTest::testProcessCreate public function Reference processing should not create new entity if no matches are found.
ReferenceTest::testProcessCreateFail public function Test reference processing failure.
ReferenceTest::testProcessExisting public function Reference processing returns a reference to existing matching entities.
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.