public function AfterParseBaseTest::testSkippingItems in Feeds 8.3
Tests removing items by throwing a SkipItemException.
@covers ::afterParse
File
- tests/
src/ Unit/ EventSubscriber/ AfterParseBaseTest.php, line 99
Class
- AfterParseBaseTest
- @coversDefaultClass \Drupal\feeds\EventSubscriber\AfterParseBase @group feeds
Namespace
Drupal\Tests\feeds\Unit\EventSubscriberCode
public function testSkippingItems() {
// Create a few items.
for ($i = 1; $i <= 5; $i++) {
$item = new DynamicItem();
$item
->set('id', $i);
$this->parserResult
->addItem($item);
}
// Implement AfterParseBase::alterItem() and throw an exception on items 3
// and 5.
$this->subscriber
->expects($this
->exactly(5))
->method('alterItem')
->will($this
->returnCallback(function (ItemInterface $item, ParseEvent $event) {
switch ($item
->get('id')) {
case 3:
case 5:
throw new SkipItemException();
}
}));
// Run subscriber.
$this->subscriber
->afterParse($this->event);
// Check which items are still on the parser result.
$this
->assertCount(3, $this->parserResult);
$expected = [
1,
2,
4,
];
$i = 0;
foreach ($this->parserResult as $item) {
$this
->assertEquals($expected[$i], $item
->get('id'));
$i++;
}
}