You are here

class TestWrapperTarget in Feeds Paragraphs 8

@group Feeds Paragraphs @coversDefaultClass \Drupal\feeds_para_mapper\Feeds\Target\WrapperTarget

Hierarchy

Expanded class hierarchy of TestWrapperTarget

File

tests/src/Unit/TestWrapperTarget.php, line 13

Namespace

Drupal\Tests\feeds_para_mapper\Unit
View source
class TestWrapperTarget extends FpmTestBase {
  use Common;

  /**
   * @var string
   */
  protected $class;

  /**
   * @var string
   */
  protected $type;
  protected function setUp() {
    $this->class = Text::class;
    $this->type = "text";
    $this
      ->getInstanceMock();
    parent::setUp();
    $this
      ->addServices($this->services);
  }

  /**
   * Mocks a form state object.
   * @return FormStateInterface
   *   The form state.
   */
  public function getFormStateMock() {
    $formState = $this
      ->createMock(FormStateInterface::class);
    $formState
      ->expects($this
      ->any())
      ->method('getTriggeringElement')
      ->willReturn(array(
      '#delta' => 0,
    ));
    $formState
      ->expects($this
      ->any())
      ->method('getValue')
      ->willReturn(array(
      'format' => 'test format',
    ));
    return $formState;
  }

  /**
   *
   * @covers ::createTargetInstance
   */
  public function testCreateTargetInstance() {
    $instance = $this->wrapperTarget
      ->createTargetInstance();
    $this
      ->assertTrue($instance instanceof Text);
  }

  /**
   *
   * @covers ::prepareTarget
   */
  public function testPrepareTarget() {
    $method = $this
      ->getMethod(Text::class, 'prepareTarget')
      ->getClosure();
    $field = $this->fieldHelper
      ->getBundleFields('bundle_two')[0]
      ->reveal();
    $info = $this
      ->getTargetInfo();
    $field
      ->set('target_info', $info);
    $textDef = $method($field);
    $textPCount = count($textDef
      ->getProperties());
    $method = $this
      ->getMethod(WrapperTarget::class, 'prepareTarget')
      ->getClosure();
    $wrapperDef = $method($field);
    $wrapperPCount = count($wrapperDef
      ->getProperties());
    $this
      ->assertSame($textPCount, $wrapperPCount, 'The wrapper has the target properties');
  }

  /**
   *
   * @covers ::defaultConfiguration
   */
  public function testDefaultConfiguration() {
    $textDefaultConfig = $this->target
      ->defaultConfiguration();
    $wrapperDefaultConfig = $this->wrapperTarget
      ->defaultConfiguration();
    $message = "Wrapper has the target's default configuration: ";
    foreach ($textDefaultConfig as $key => $configItem) {
      $this
        ->assertArrayHasKey($key, $wrapperDefaultConfig, $message . $key);
    }
    $this
      ->assertArrayHasKey('max_values', $wrapperDefaultConfig, $message . 'max_values');
  }

  /**
   *
   * @covers ::buildConfigurationForm
   */
  public function testBuildConfigurationForm() {
    $formState = $this
      ->getFormStateMock();
    $textForm = $this->target
      ->buildConfigurationForm(array(), $formState);
    $wrapperForm = $this->wrapperTarget
      ->buildConfigurationForm(array(), $formState);
    $message = "Wrapper has the target's form element: ";
    foreach ($textForm as $field => $formMarkup) {
      $this
        ->assertArrayHasKey($field, $wrapperForm, $message . $field);
    }
    $this
      ->assertArrayHasKey('max_values', $wrapperForm, $message . 'max_values');
  }

  /**
   * @covers ::getSummary
   */
  public function testGetSummary() {
    $res = $this->wrapperTarget
      ->getSummary();
    $res = $res
      ->getUntranslatedString();
    $expected = "test summary<br>Maximum values: -1";
    $this
      ->assertSame($res, $expected, "The target summary exists");
  }

  /**
   * @covers \Drupal\feeds_para_mapper\Feeds\Target\WrapperTarget::targets
   */
  public function testTargets() {
    $targets = array();
    $this->wrapperTarget
      ->targets($targets, $this->feedType, array());
    $this
      ->assertTrue(isset($targets['bundle_two_text']), 'Target added');
    $field_type = $targets['bundle_two_text']
      ->getFieldDefinition()->field_type;
    $this
      ->assertSame('entity_reference_revisions', $field_type, 'target field type is changed');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Common::getClass function Returns the target class.
Common::getInstanceMock public function @inheritdoc
Common::getTargetInfo public function @inheritdoc
Common::getType function Returns the target type.
FpmTestBase::$entityHelper protected property The entity helper instance.
FpmTestBase::$feedType protected property The mocked feed type instance.
FpmTestBase::$fieldHelper protected property The field helper instance.
FpmTestBase::$fields protected property The mocked paragraphs and node fields.
FpmTestBase::$instanceMock protected property The target mock
FpmTestBase::$messenger protected property The messenger mock
FpmTestBase::$node protected property The mocked node.
FpmTestBase::$services protected property The services to mock.
FpmTestBase::$target protected property The target object.
FpmTestBase::$wrapperTarget protected property The wrapper instance.
FpmTestBase::addServices protected function Adds services to the container.
FpmTestBase::generatePluginDefinitions protected function Generates plugin definitions array (text plugin for now).
FpmTestBase::getFeedMock protected function Returns a mocked feed entity.
FpmTestBase::getFeedTypeMock protected function Creates feed type entity.
FpmTestBase::getMapperObject protected function Generates a mapper object.
FpmTestBase::getMessengerMock private function Mocks the messenger service.
FpmTestBase::getMethod protected function Calls a protected method on an object.
FpmTestBase::getPluginManagerMock private function Creates FeedsPluginManager instance.
FpmTestBase::getProcessorMock private function Mocks an entity processor instance.
FpmTestBase::getProperty protected function Returns a protected, private property of an object.
FpmTestBase::initWrapper private function Instantiates and returns a WrapperTarget object.
FpmTestBase::updateProperty protected function Updates a protected, private property of an object.
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.
TestWrapperTarget::$class protected property
TestWrapperTarget::$type protected property
TestWrapperTarget::getFormStateMock public function Mocks a form state object.
TestWrapperTarget::setUp protected function @inheritdoc Overrides FpmTestBase::setUp
TestWrapperTarget::testBuildConfigurationForm public function @covers ::buildConfigurationForm
TestWrapperTarget::testCreateTargetInstance public function @covers ::createTargetInstance
TestWrapperTarget::testDefaultConfiguration public function @covers ::defaultConfiguration
TestWrapperTarget::testGetSummary public function @covers ::getSummary
TestWrapperTarget::testPrepareTarget public function @covers ::prepareTarget
TestWrapperTarget::testTargets public function @covers \Drupal\feeds_para_mapper\Feeds\Target\WrapperTarget::targets
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.