You are here

class FeedsSubscriberTest in Feeds Tamper 8.2

@coversDefaultClass \Drupal\feeds_tamper\EventSubscriber\FeedsSubscriber @group feeds_tamper

Hierarchy

Expanded class hierarchy of FeedsSubscriberTest

File

tests/src/Unit/EventSubscriber/FeedsSubscriberTest.php, line 23

Namespace

Drupal\Tests\feeds_tamper\Unit\EventSubscriber
View source
class FeedsSubscriberTest extends FeedsTamperTestCase {

  /**
   * The subscriber under test.
   *
   * @var \Drupal\feeds_tamper\EventSubscriber\FeedsSubscriber
   */
  protected $subscriber;

  /**
   * The parse event.
   *
   * @var \Drupal\feeds\Event\ParseEvent
   */
  protected $event;

  /**
   * The tamper meta.
   *
   * @var \Drupal\feeds_tamper\FeedTypeTamperMetaInterface
   */
  protected $tamperMeta;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    // Create parse event.
    $this->event = new ParseEvent($this
      ->getMockFeed(), $this
      ->createMock(FetcherResultInterface::class));
    $this->event
      ->setParserResult(new ParserResult());

    // Create tamper meta.
    $this->tamperMeta = $this
      ->createMock(FeedTypeTamperMetaInterface::class);

    // Create feed type tamper manager.
    $tamper_manager = $this
      ->createMock(FeedTypeTamperManagerInterface::class);
    $tamper_manager
      ->expects($this
      ->any())
      ->method('getTamperMeta')
      ->will($this
      ->returnValue($this->tamperMeta));

    // And finally, create the subscriber to test.
    $this->subscriber = new FeedsSubscriber($tamper_manager);
  }

  /**
   * Creates a tamper mock with a return value for the tamper() method.
   *
   * @param mixed $return_value
   *   (optional) The value that the tamper plugin must return when tamper()
   *   gets called on it.
   *
   * @return \Drupal\tamper\TamperInterface
   *   A mocked tamper plugin.
   */
  protected function createTamperMock($return_value = NULL) {
    $tamper = $this
      ->createMock(TamperInterface::class);
    $tamper
      ->expects($this
      ->any())
      ->method('tamper')
      ->will($this
      ->returnValue($return_value));
    return $tamper;
  }

  /**
   * @covers ::afterParse
   * @covers ::alterItem
   */
  public function testAfterParse() {
    $tamper = $this
      ->createMock(TamperInterface::class);
    $tamper
      ->expects($this
      ->any())
      ->method('tamper')
      ->will($this
      ->returnValue('Foo'));
    $this->tamperMeta
      ->expects($this
      ->once())
      ->method('getTampersGroupedBySource')
      ->will($this
      ->returnValue([
      'alpha' => [
        $this
          ->createTamperMock('Foo'),
      ],
    ]));

    // Add an item to the parser result.
    $item = new DynamicItem();
    $item
      ->set('alpha', 'Bar');
    $this->event
      ->getParserResult()
      ->addItem($item);
    $this->subscriber
      ->afterParse($this->event);
    $this
      ->assertEquals('Foo', $item
      ->get('alpha'));
  }

  /**
   * @covers ::afterParse
   */
  public function testAfterParseWithNoItems() {
    $this->tamperMeta
      ->expects($this
      ->once())
      ->method('getTampersGroupedBySource')
      ->will($this
      ->returnValue([
      'alpha' => [
        $this
          ->createTamperMock('Foo'),
      ],
    ]));
    $this->subscriber
      ->afterParse($this->event);
  }

  /**
   * @covers ::afterParse
   */
  public function testAfterParseWithEmptyArray() {
    $tamper = $this
      ->createMock(TamperInterface::class);
    $tamper
      ->expects($this
      ->any())
      ->method('tamper')
      ->will($this
      ->returnValue('Foo'));
    $this->tamperMeta
      ->expects($this
      ->once())
      ->method('getTampersGroupedBySource')
      ->will($this
      ->returnValue([
      'alpha' => [
        $this
          ->createTamperMock('Foo'),
      ],
    ]));

    // Add an item to the parser result.
    $item = new DynamicItem();
    $item
      ->set('alpha', []);
    $this->event
      ->getParserResult()
      ->addItem($item);
    $this->subscriber
      ->afterParse($this->event);
    $this
      ->assertEquals('Foo', $item
      ->get('alpha'));
  }

  /**
   * @covers ::afterParse
   * @covers ::alterItem
   */
  public function testAfterParseWithNoTampers() {
    $this->tamperMeta
      ->expects($this
      ->once())
      ->method('getTampersGroupedBySource')
      ->will($this
      ->returnValue([]));

    // Add an item to the parser result.
    $item = new DynamicItem();
    $item
      ->set('alpha', 'Bar');
    $this->event
      ->getParserResult()
      ->addItem($item);

    // Run event callback.
    $this->subscriber
      ->afterParse($this->event);
    $this
      ->assertEquals('Bar', $item
      ->get('alpha'));
  }

  /**
   * @covers ::afterParse
   * @covers ::alterItem
   */
  public function testAfterParseWithMultiValueTampers() {

    // Create a tamper that turns an input value into an array.
    $tamper1 = $this
      ->prophesize(TamperInterface::class);
    $tamper1
      ->tamper('Bar', Argument::type(TamperableFeedItemAdapter::class))
      ->willReturn([
      'Bar',
      'Bar',
    ]);
    $tamper1
      ->getPluginDefinition()
      ->willReturn([
      'handle_multiples' => FALSE,
    ]);
    $tamper1
      ->multiple()
      ->willReturn(TRUE);
    $tamper1 = $tamper1
      ->reveal();

    // Create a tamper that returns 'Foo'.
    $tamper2 = $this
      ->prophesize(TamperInterface::class);
    $tamper2
      ->tamper('Bar', Argument::type(TamperableFeedItemAdapter::class))
      ->willReturn('Foo');
    $tamper2
      ->getPluginDefinition()
      ->willReturn([
      'handle_multiples' => FALSE,
    ]);
    $tamper2
      ->multiple()
      ->willReturn(FALSE);
    $tamper2 = $tamper2
      ->reveal();

    // Create a tamper that returns 'FooFoo'.
    $tamper3 = $this
      ->prophesize(TamperInterface::class);
    $tamper3
      ->tamper('Foo', Argument::type(TamperableFeedItemAdapter::class))
      ->willReturn('FooFoo');
    $tamper3
      ->getPluginDefinition()
      ->willReturn([
      'handle_multiples' => FALSE,
    ]);
    $tamper3
      ->multiple()
      ->willReturn(FALSE);
    $tamper3 = $tamper3
      ->reveal();
    $this->tamperMeta
      ->expects($this
      ->once())
      ->method('getTampersGroupedBySource')
      ->will($this
      ->returnValue([
      'alpha' => [
        $tamper1,
        $tamper2,
        $tamper3,
      ],
    ]));

    // Add an item to the parser result.
    $item = new DynamicItem();
    $item
      ->set('alpha', 'Bar');
    $this->event
      ->getParserResult()
      ->addItem($item);

    // Run event callback.
    $this->subscriber
      ->afterParse($this->event);
    $this
      ->assertEquals([
      'FooFoo',
      'FooFoo',
    ], $item
      ->get('alpha'));
  }

  /**
   * @covers ::afterParse
   * @covers ::alterItem
   */
  public function testAfterParseWithTamperItem() {

    // Create a tamper plugin that manipulates the whole item.
    $tamper = $this
      ->createMock(TamperInterface::class);
    $tamper
      ->expects($this
      ->once())
      ->method('tamper')
      ->will($this
      ->returnCallback([
      $this,
      'callbackWithTamperItem',
    ]));
    $this->tamperMeta
      ->expects($this
      ->once())
      ->method('getTampersGroupedBySource')
      ->will($this
      ->returnValue([
      'alpha' => [
        $tamper,
      ],
    ]));

    // Add an item to the parser result.
    $item = new DynamicItem();
    $item
      ->set('alpha', 'Foo');
    $item
      ->set('beta', 'Bar');
    $item
      ->set('gamma', 'Qux');
    $this->event
      ->getParserResult()
      ->addItem($item);

    // Run event callback.
    $this->subscriber
      ->afterParse($this->event);
    $this
      ->assertEquals('Fooing', $item
      ->get('alpha'));
    $this
      ->assertEquals('Baring', $item
      ->get('beta'));
    $this
      ->assertEquals('Quxing', $item
      ->get('gamma'));
  }

  /**
   * Callback for testAfterParseWithTamperItem().
   */
  public function callbackWithTamperItem($data, TamperableFeedItemAdapter $item) {

    // Add "ing" to each property.
    foreach ($item
      ->getSource() as $key => $value) {
      $item
        ->setSourceProperty($key, $value . 'ing');
    }

    // Make sure that "ing" is also added to the field that is being tampered.
    return $data . 'ing';
  }

  /**
   * @covers ::afterParse
   * @covers ::alterItem
   */
  public function testAfterParseWithSkippingItem() {

    // Create a tamper plugin that will throw a SkipTamperItemException for some
    // values.
    $tamper = $this
      ->createMock(TamperInterface::class);
    $tamper
      ->expects($this
      ->exactly(2))
      ->method('tamper')
      ->will($this
      ->returnCallback([
      $this,
      'callbackSkipItem',
    ]));
    $this->tamperMeta
      ->expects($this
      ->once())
      ->method('getTampersGroupedBySource')
      ->will($this
      ->returnValue([
      'alpha' => [
        $tamper,
      ],
    ]));

    // Create three items. The first item should get removed.
    $item1 = new DynamicItem();
    $item1
      ->set('alpha', 'Foo');
    $this->event
      ->getParserResult()
      ->addItem($item1);
    $item2 = new DynamicItem();
    $item2
      ->set('alpha', 'Bar');
    $this->event
      ->getParserResult()
      ->addItem($item2);
    $this->subscriber
      ->afterParse($this->event);

    // Assert that only item 2 still exists.
    $this
      ->assertEquals(1, $this->event
      ->getParserResult()
      ->count());
    $this
      ->assertSame($item2, $this->event
      ->getParserResult()
      ->offsetGet(0));
  }

  /**
   * Callback for testAfterParseWithSkippingItem().
   */
  public function callbackSkipItem($data, TamperableFeedItemAdapter $item) {
    if ($data == 'Foo') {
      throw new SkipTamperItemException();
    }
  }

  /**
   * @covers ::afterParse
   * @covers ::alterItem
   */
  public function testAfterParseWithSkippingData() {

    // Create a tamper plugin that will throw a SkipTamperDataException for some
    // values.
    $tamper1 = $this
      ->createMock(TamperInterface::class);
    $tamper1
      ->expects($this
      ->exactly(2))
      ->method('tamper')
      ->will($this
      ->returnCallback([
      $this,
      'callbackSkipData',
    ]));

    // Create a second tamper plugin that will just set the value to 'Qux'.
    $tamper2 = $this
      ->createMock(TamperInterface::class);
    $tamper2
      ->expects($this
      ->once())
      ->method('tamper')
      ->will($this
      ->returnValue('Qux'));

    // Create a third tamper plugin that operates on the 'beta' field, to ensure
    // skipping on the 'alpha' field does not skip the 'beta' field.
    $tamper3 = $this
      ->createMock(TamperInterface::class);
    $tamper3
      ->expects($this
      ->exactly(2))
      ->method('tamper')
      ->will($this
      ->returnValue('Baz'));
    $this->tamperMeta
      ->expects($this
      ->once())
      ->method('getTampersGroupedBySource')
      ->will($this
      ->returnValue([
      'alpha' => [
        $tamper1,
        $tamper2,
      ],
      'beta' => [
        $tamper3,
      ],
    ]));

    // Create two items. The first item should get the value unset.
    $item1 = new DynamicItem();
    $item1
      ->set('alpha', 'Foo');
    $item1
      ->set('beta', 'Foo');
    $this->event
      ->getParserResult()
      ->addItem($item1);
    $item2 = new DynamicItem();
    $item2
      ->set('alpha', 'Bar');
    $item2
      ->set('beta', 'Bar');
    $this->event
      ->getParserResult()
      ->addItem($item2);
    $this->subscriber
      ->afterParse($this->event);

    // Assert that 2 items still exist.
    $this
      ->assertEquals(2, $this->event
      ->getParserResult()
      ->count());

    // And assert that item 1 no longer has an alpha value.
    $this
      ->assertNull($item1
      ->get('alpha'));

    // Assert other values.
    $this
      ->assertEquals($item1
      ->get('beta'), 'Baz');
    $this
      ->assertEquals($item2
      ->get('alpha'), 'Qux');
    $this
      ->assertEquals($item2
      ->get('beta'), 'Baz');
  }

  /**
   * Callback for testAfterParseWithSkippingData().
   */
  public function callbackSkipData($data, TamperableFeedItemAdapter $item) {
    if ($data == 'Foo') {
      throw new SkipTamperDataException();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsMockingTrait::getMockAccount protected function Mocks an account object.
FeedsMockingTrait::getMockedAccountSwitcher protected function Returns a mocked AccountSwitcher object.
FeedsMockingTrait::getMockFeed protected function Returns a mocked feed entity.
FeedsMockingTrait::getMockFeedType protected function Returns a mocked feed type entity.
FeedsMockingTrait::getMockFieldDefinition protected function Mocks a field definition. 1
FeedsMockingTrait::getMockFileSystem protected function Mocks the file system.
FeedsReflectionTrait::callProtectedMethod protected function Calls a protected method on the given object.
FeedsReflectionTrait::getMethod protected function Gets a ReflectionMethod for a class method.
FeedsReflectionTrait::getProtectedClosure protected function Returns a dynamically created closure for the object's method.
FeedsReflectionTrait::setProtectedProperty protected function Sets a protected property.
FeedsSubscriberTest::$event protected property The parse event.
FeedsSubscriberTest::$subscriber protected property The subscriber under test.
FeedsSubscriberTest::$tamperMeta protected property The tamper meta.
FeedsSubscriberTest::callbackSkipData public function Callback for testAfterParseWithSkippingData().
FeedsSubscriberTest::callbackSkipItem public function Callback for testAfterParseWithSkippingItem().
FeedsSubscriberTest::callbackWithTamperItem public function Callback for testAfterParseWithTamperItem().
FeedsSubscriberTest::createTamperMock protected function Creates a tamper mock with a return value for the tamper() method.
FeedsSubscriberTest::setUp public function Overrides FeedsUnitTestCase::setUp
FeedsSubscriberTest::testAfterParse public function @covers ::afterParse @covers ::alterItem
FeedsSubscriberTest::testAfterParseWithEmptyArray public function @covers ::afterParse
FeedsSubscriberTest::testAfterParseWithMultiValueTampers public function @covers ::afterParse @covers ::alterItem
FeedsSubscriberTest::testAfterParseWithNoItems public function @covers ::afterParse
FeedsSubscriberTest::testAfterParseWithNoTampers public function @covers ::afterParse @covers ::alterItem
FeedsSubscriberTest::testAfterParseWithSkippingData public function @covers ::afterParse @covers ::alterItem
FeedsSubscriberTest::testAfterParseWithSkippingItem public function @covers ::afterParse @covers ::alterItem
FeedsSubscriberTest::testAfterParseWithTamperItem public function @covers ::afterParse @covers ::alterItem
FeedsUnitTestCase::absolutePath protected function Returns the absolute directory path of the Feeds module.
FeedsUnitTestCase::defineConstants protected function Defines stub constants.
FeedsUnitTestCase::getMockStreamWrapperManager protected function Returns a mock stream wrapper manager.
FeedsUnitTestCase::resourcesPath protected function Returns the absolute directory path of the resources folder.
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.
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.