class ReferenceTest in YAML Content 8
Test entity reference processing.
@group yaml_content
@coversDefaultClass \Drupal\yaml_content\Plugin\yaml_content\process\Reference
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\yaml_content\Unit\Plugin\yaml_content\process\ReferenceTest
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\processView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
ReferenceTest:: |
protected | property | The entity storage handler mock. | |
ReferenceTest:: |
protected | property | The entity type manager service mock. | |
ReferenceTest:: |
protected | property | The reference process plugin being tested. | |
ReferenceTest:: |
public | function |
Setup mocks and a reference plugin for all tests. Overrides UnitTestCase:: |
|
ReferenceTest:: |
public | function | Reference processing should not create new entity if no matches are found. | |
ReferenceTest:: |
public | function | Test reference processing failure. | |
ReferenceTest:: |
public | function | Reference processing returns a reference to existing matching entities. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |