class ReplicatorTest in Replicate 8
@coversDefaultClass \Drupal\replicate\Replicator @group replicate
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\replicate\Unit\ReplicatorTest
Expanded class hierarchy of ReplicatorTest
File
- tests/
src/ Unit/ ReplicatorTest.php, line 24
Namespace
Drupal\Tests\replicate\UnitView source
class ReplicatorTest extends UnitTestCase {
/**
* Tests the cloneEntity method.
*
* @covers ::cloneEntity
*/
public function testCloneForNonFieldableEntity() {
$entity = $this
->prophesize(EntityInterface::class);
$entity
->getEntityTypeId()
->willReturn('entity_test');
$clone = $this
->prophesize(EntityInterface::class);
$clone
->getEntityTypeId()
->willReturn('entity_test');
$clone = $clone
->reveal();
$entity
->createDuplicate()
->willReturn($clone);
$event_dispatcher = $this
->prophesize(EventDispatcherInterface::class);
$event_dispatcher
->dispatch('replicate__entity__entity_test', Argument::type(ReplicateEntityEvent::class))
->shouldBeCalled();
$event_dispatcher
->dispatch(ReplicatorEvents::REPLICATE_ALTER, Argument::type(ReplicateAlterEvent::class))
->shouldBeCalled();
$event_dispatcher
->dispatch(ReplicatorEvents::AFTER_SAVE, Argument::type(AfterSaveEvent::class))
->shouldNotBeCalled();
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$replicator = new Replicator($entity_type_manager
->reveal(), $event_dispatcher
->reveal());
$result = $replicator
->cloneEntity($entity
->reveal());
$this
->assertSame($clone, $result);
}
/**
* Tests the cloneEntity and cloneEntityFields methods.
*
* @covers ::cloneEntity
* @covers ::dispatchEventCloneEntityFields
*/
public function testCloneForFieldableEntity() {
$entity = $this
->prophesize(FieldableEntityInterface::class);
$entity
->getEntityTypeId()
->willReturn('entity_test');
$clone = $this
->prophesize(FieldableEntityInterface::class);
$clone
->getEntityTypeId()
->willReturn('entity_test');
$entity_ref_field_item_list = $this
->prophesize(FieldItemListInterface::class);
$clone
->get('entity_ref')
->willReturn($entity_ref_field_item_list
->reveal());
$textfield_field_item_list = $this
->prophesize(FieldItemListInterface::class);
$clone
->get('field_textfield')
->willReturn($textfield_field_item_list
->reveal());
$field_definitions = [];
$field_definition = $this
->prophesize(FieldDefinitionInterface::class);
$field_definition
->getType()
->willReturn('entity_reference');
$field_definitions['entity_ref'] = $field_definition
->reveal();
$field_definition = $this
->prophesize(FieldDefinitionInterface::class);
$field_definition
->getType()
->willReturn('textfield');
$field_definitions['field_textfield'] = $field_definition
->reveal();
$clone
->getFieldDefinitions()
->willReturn($field_definitions)
->shouldBeCalled();
$clone = $clone
->reveal();
$entity
->createDuplicate()
->willReturn($clone);
$event_dispatcher = $this
->prophesize(EventDispatcherInterface::class);
$event_dispatcher
->dispatch('replicate__entity__entity_test', Argument::type(ReplicateEntityEvent::class))
->shouldBeCalled();
$event_dispatcher
->dispatch('replicate__entity_field__entity_reference', Argument::that(function ($event) {
if (!$event instanceof ReplicateEntityFieldEvent) {
return FALSE;
}
if (!$event
->getFieldItemList() instanceof FieldItemListInterface) {
return FALSE;
}
return TRUE;
}))
->shouldBeCalled();
$event_dispatcher
->dispatch('replicate__entity_field__textfield', Argument::that(function ($event) {
if (!$event instanceof ReplicateEntityFieldEvent) {
return FALSE;
}
if (!$event
->getFieldItemList() instanceof FieldItemListInterface) {
return FALSE;
}
return TRUE;
}))
->shouldBeCalled();
$event_dispatcher
->dispatch(ReplicatorEvents::REPLICATE_ALTER, Argument::type(ReplicateAlterEvent::class))
->shouldBeCalled();
$event_dispatcher
->dispatch(ReplicatorEvents::AFTER_SAVE, Argument::type(AfterSaveEvent::class))
->shouldNotBeCalled();
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$replicator = new Replicator($entity_type_manager
->reveal(), $event_dispatcher
->reveal());
$result = $replicator
->cloneEntity($entity
->reveal());
$this
->assertSame($clone, $result);
}
}
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. | |
ReplicatorTest:: |
public | function | Tests the cloneEntity and cloneEntityFields methods. | |
ReplicatorTest:: |
public | function | Tests the cloneEntity method. | |
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. | |
UnitTestCase:: |
protected | function | 340 |