You are here

public function FeedsSubscriberTest::testAfterParseWithSkippingData in Feeds Tamper 8.2

@covers ::afterParse @covers ::alterItem

File

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

Class

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

Namespace

Drupal\Tests\feeds_tamper\Unit\EventSubscriber

Code

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');
}