Commit f42cd0d0 authored by Youri van Koppen's avatar Youri van Koppen
Browse files

Issue #3322691 by MegaChriz: Refactor unit tests for targets

parent 32d0ccfb
Loading
Loading
Loading
Loading
+8 −1
Changes for tests/src/Unit/Feeds/Target/BookTest.php: 8 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -23,6 +23,13 @@ namespace Drupal\Tests\feeds\Unit\Feeds\Target {
   */
  class BookTest extends FeedsUnitTestCase {

    /**
     * The ID of the plugin.
     *
     * @var string
     */
    protected static $pluginId = 'book';

    /**
     * Database Service Object.
     *
@@ -124,7 +131,7 @@ namespace Drupal\Tests\feeds\Unit\Feeds\Target {

      $book_target = $this->getMockBuilder(Book::class)
        ->setConstructorArgs([
          $configuration, 'book',
          $configuration, static::$pluginId,
          [],
          $this->database->reveal(),
          $this->nodeStorage->reveal(),
+8 −9
Changes for tests/src/Unit/Feeds/Target/BooleanTest.php: 8 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

namespace Drupal\Tests\feeds\Unit\Feeds\Target;

use Drupal\feeds\FeedTypeInterface;
use Drupal\feeds\Feeds\Target\Boolean;

/**
@@ -11,6 +10,13 @@ use Drupal\feeds\Feeds\Target\Boolean;
 */
class BooleanTest extends FieldTargetTestBase {

  /**
   * The ID of the plugin.
   *
   * @var string
   */
  protected static $pluginId = 'boolean';

  /**
   * {@inheritdoc}
   */
@@ -22,14 +28,7 @@ class BooleanTest extends FieldTargetTestBase {
   * @covers ::prepareValue
   */
  public function testPrepareValue() {
    $method = $this->getMethod(Boolean::class, 'prepareTarget')->getClosure();

    $configuration = [
      'feed_type' => $this->createMock(FeedTypeInterface::class),
      'target_definition' => $method($this->getMockFieldDefinition()),
    ];

    $target = new Boolean($configuration, 'boolean', []);
    $target = $this->instantiatePlugin();
    $values = ['value' => 'string'];

    $method = $this->getProtectedClosure($target, 'prepareValue');
+14 −6
Changes for tests/src/Unit/Feeds/Target/ConfigEntityReferenceTest.php: 14 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\feeds\Exception\ReferenceNotFoundException;
use Drupal\feeds\Feeds\Target\ConfigEntityReference;
use Drupal\feeds\FeedTypeInterface;
use Drupal\feeds\Plugin\Type\Target\TargetInterface;

/**
 * @coversDefaultClass \Drupal\feeds\Feeds\Target\ConfigEntityReference
@@ -14,6 +15,13 @@ use Drupal\feeds\FeedTypeInterface;
 */
class ConfigEntityReferenceTest extends ConfigEntityReferenceTestBase {

  /**
   * The ID of the plugin.
   *
   * @var string
   */
  protected static $pluginId = 'config_entity_reference';

  /**
   * {@inheritdoc}
   */
@@ -26,13 +34,13 @@ class ConfigEntityReferenceTest extends ConfigEntityReferenceTestBase {
  /**
   * {@inheritdoc}
   */
  protected function createTargetPluginInstance(array $configuration = []) {
  protected function instantiatePlugin(array $configuration = []): TargetInterface {
    $configuration += [
      'feed_type' => $this->createMock(FeedTypeInterface::class),
      'target_definition' => $this->createTargetDefinitionMock(),
      'reference_by' => 'id',
    ];
    return new ConfigEntityReference($configuration, 'config_entity_reference', [], $this->entityTypeManager->reveal(), $this->entityFinder->reveal(), $this->transliteration->reveal(), $this->typedConfigManager->reveal());
    return new ConfigEntityReference($configuration, static::$pluginId, [], $this->entityTypeManager->reveal(), $this->entityFinder->reveal(), $this->transliteration->reveal(), $this->typedConfigManager->reveal());
  }

  /**
@@ -65,7 +73,7 @@ class ConfigEntityReferenceTest extends ConfigEntityReferenceTestBase {
      ->willReturn(['foo'])
      ->shouldBeCalled();

    $method = $this->getProtectedClosure($this->createTargetPluginInstance(), 'prepareValue');
    $method = $this->getProtectedClosure($this->instantiatePlugin(), 'prepareValue');
    $values = ['target_id' => 'foo'];
    $method(0, $values);
    $this->assertSame($values, ['target_id' => 'foo']);
@@ -82,7 +90,7 @@ class ConfigEntityReferenceTest extends ConfigEntityReferenceTestBase {
      ->willReturn([])
      ->shouldBeCalled();

    $method = $this->getProtectedClosure($this->createTargetPluginInstance(), 'prepareValue');
    $method = $this->getProtectedClosure($this->instantiatePlugin(), 'prepareValue');
    $values = ['target_id' => 'bar'];
    $this->expectException(ReferenceNotFoundException::class);
    $this->expectExceptionMessage("Referenced entity not found for field <em class=\"placeholder\">id</em> with value <em class=\"placeholder\">bar</em>.");
@@ -96,7 +104,7 @@ class ConfigEntityReferenceTest extends ConfigEntityReferenceTestBase {
    $expected = [
      'Reference by: <em class="placeholder">ID</em>',
    ];
    $summary = $this->createTargetPluginInstance()->getSummary();
    $summary = $this->instantiatePlugin()->getSummary();
    foreach ($summary as $key => $value) {
      $summary[$key] = (string) $value;
    }
@@ -107,7 +115,7 @@ class ConfigEntityReferenceTest extends ConfigEntityReferenceTestBase {
   * @covers ::getSummary
   */
  public function testGetSummaryNoReferenceBySet() {
    $target_plugin = $this->createTargetPluginInstance([
    $target_plugin = $this->instantiatePlugin([
      'reference_by' => NULL,
    ]);

+19 −4
Changes for tests/src/Unit/Feeds/Target/DateRangeTest.php: 19 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\Tests\feeds\Unit\Feeds\Target;

use Drupal\feeds\Feeds\Target\DateRange;
use Drupal\feeds\Plugin\Type\Target\TargetInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;

/**
@@ -11,6 +12,13 @@ use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
 */
class DateRangeTest extends FieldTargetWithContainerTestBase {

  /**
   * The ID of the plugin.
   *
   * @var string
   */
  protected static $pluginId = 'daterange';

  /**
   * The mocked feed type entity.
   *
@@ -44,14 +52,21 @@ class DateRangeTest extends FieldTargetWithContainerTestBase {
  }

  /**
   * @covers ::prepareValue
   * {@inheritdoc}
   */
  public function testPrepareValue() {
    $configuration = [
  protected function instantiatePlugin(array $configuration = []): TargetInterface {
    $configuration += [
      'feed_type' => $this->feedType,
      'target_definition' => $this->targetDefinition,
    ];
    $target = new DateRange($configuration, 'daterange', []);
    return new DateRange($configuration, static::$pluginId, []);
  }

  /**
   * @covers ::prepareValue
   */
  public function testPrepareValue() {
    $target = $this->instantiatePlugin();
    $method = $this->getProtectedClosure($target, 'prepareValue');

    $values = [
+25 −21
Changes for tests/src/Unit/Feeds/Target/DateTimeTest.php: 25 added lines, 21 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ namespace Drupal\Tests\feeds\Unit\Feeds\Target;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\feeds\Feeds\Target\DateTime;
use Drupal\feeds\Plugin\Type\Target\TargetInterface;

/**
 * @coversDefaultClass \Drupal\feeds\Feeds\Target\DateTime
@@ -12,6 +13,13 @@ use Drupal\feeds\Feeds\Target\DateTime;
 */
class DateTimeTest extends FieldTargetWithContainerTestBase {

  /**
   * The ID of the plugin.
   *
   * @var string
   */
  protected static $pluginId = 'datetime';

  /**
   * The feed type entity.
   *
@@ -44,6 +52,17 @@ class DateTimeTest extends FieldTargetWithContainerTestBase {
    return DateTime::class;
  }

  /**
   * {@inheritdoc}
   */
  protected function instantiatePlugin(array $configuration = []): TargetInterface {
    $configuration += [
      'feed_type' => $this->feedType,
      'target_definition' => $this->targetDefinition,
    ];
    return new DateTime($configuration, static::$pluginId, []);
  }

  /**
   * Tests preparing a value that succeeds.
   *
@@ -53,11 +72,7 @@ class DateTimeTest extends FieldTargetWithContainerTestBase {
    $method = $this->getMethod('Drupal\feeds\Feeds\Target\DateTime', 'prepareTarget')->getClosure();
    $this->targetDefinition = $method($this->getMockFieldDefinition(['datetime_type' => 'date']));

    $configuration = [
      'feed_type' => $this->feedType,
      'target_definition' => $this->targetDefinition,
    ];
    $target = new DateTime($configuration, 'datetime', []);
    $target = $this->instantiatePlugin();
    $method = $this->getProtectedClosure($target, 'prepareValue');

    $values = ['value' => 1411606273];
@@ -71,11 +86,7 @@ class DateTimeTest extends FieldTargetWithContainerTestBase {
   * @covers ::prepareValue
   */
  public function testWithErrors() {
    $configuration = [
      'feed_type' => $this->feedType,
      'target_definition' => $this->targetDefinition,
    ];
    $target = new DateTime($configuration, 'datetime', []);
    $target = $this->instantiatePlugin();
    $method = $this->getProtectedClosure($target, 'prepareValue');

    $values = ['value' => '2000-05-32'];
@@ -89,11 +100,7 @@ class DateTimeTest extends FieldTargetWithContainerTestBase {
   * @covers ::prepareValue
   */
  public function testYearValue() {
    $configuration = [
      'feed_type' => $this->feedType,
      'target_definition' => $this->targetDefinition,
    ];
    $target = new DateTime($configuration, 'datetime', []);
    $target = $this->instantiatePlugin();
    $method = $this->getProtectedClosure($target, 'prepareValue');

    $values = ['value' => '2000'];
@@ -116,22 +123,19 @@ class DateTimeTest extends FieldTargetWithContainerTestBase {

    // Test timezone options with one of the timezones.
    $configuration = [
      'feed_type' => $this->feedType,
      'target_definition' => $this->targetDefinition,
      'timezone' => 'Europe/Helsinki',
    ];
    $target = new DateTime($configuration, 'datetime', []);

    $target = $this->instantiatePlugin($configuration);
    $method = $this->getProtectedClosure($target, 'getTimezoneConfiguration');

    $this->assertSame('Europe/Helsinki', $method());

    // Test timezone options with site default option.
    $configuration = [
      'feed_type' => $this->feedType,
      'target_definition' => $this->targetDefinition,
      'timezone' => '__SITE__',
    ];
    $target = new DateTime($configuration, 'datetime', []);
    $target = $this->instantiatePlugin($configuration);
    $method = $this->getProtectedClosure($target, 'getTimezoneConfiguration');

    $this->assertSame('UTC', $method());
Loading