You are here

class ReplicatorTest in Replicate 8

@coversDefaultClass \Drupal\replicate\Replicator @group replicate

Hierarchy

Expanded class hierarchy of ReplicatorTest

File

tests/src/Unit/ReplicatorTest.php, line 24

Namespace

Drupal\Tests\replicate\Unit
View 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

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.
ReplicatorTest::testCloneForFieldableEntity public function Tests the cloneEntity and cloneEntityFields methods.
ReplicatorTest::testCloneForNonFieldableEntity public function Tests the cloneEntity method.
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.
UnitTestCase::setUp protected function 340