ReferenceTest.php in YAML Content 8
File
tests/src/Unit/Plugin/yaml_content/process/ReferenceTest.php
View source
<?php
namespace Drupal\Tests\yaml_content\Unit\Plugin\yaml_content\process;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\yaml_content\Traits\LoadFixturesTrait;
use Drupal\yaml_content\Plugin\ProcessingContext;
use Drupal\yaml_content\Plugin\yaml_content\process\Reference;
use Prophecy\Argument;
class ReferenceTest extends UnitTestCase {
protected $entityTypeManager;
protected $entityStorageHandler;
protected $reference;
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());
}
public function testProcessExisting() {
$data = [
'#process' => [
'callback' => 'reference',
],
];
$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());
$this->entityStorageHandler
->create(Argument::any())
->shouldNotBeCalled();
$this->reference
->process(new ProcessingContext(), $data);
$this
->assertArrayEquals([
'target_id' => 1,
], $data);
}
public function testProcessCreate() {
$data = [
'#process' => [
'callback' => 'reference',
],
];
$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());
$entity = $this
->prophesize(EntityInterface::class);
$entity
->id()
->willReturn(2);
$this->entityStorageHandler
->create(Argument::any())
->shouldBeCalled()
->willReturn($entity);
$this->reference
->process(new ProcessingContext(), $data);
$this
->assertArrayEquals([
'target_id' => 2,
], $data);
}
public function testProcessCreateFail() {
$data = [
'#process' => [
'callback' => 'reference',
],
];
$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());
$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.');
$this->entityStorageHandler
->create(Argument::any())
->shouldBeCalled()
->willReturn($entity);
$this->reference
->process(new ProcessingContext(), $data);
$this
->assertArrayEquals([
'target_id' => 2,
], $data);
}
}