AfterParseBaseTest.php in Feeds 8.3
File
tests/src/Unit/EventSubscriber/AfterParseBaseTest.php
View source
<?php
namespace Drupal\Tests\feeds\Unit\EventSubscriber;
use Drupal\feeds\Event\FeedsEvents;
use Drupal\feeds\Event\ParseEvent;
use Drupal\feeds\EventSubscriber\AfterParseBase;
use Drupal\feeds\Exception\SkipItemException;
use Drupal\feeds\Feeds\Item\DynamicItem;
use Drupal\feeds\Feeds\Item\ItemInterface;
use Drupal\feeds\Result\ParserResult;
use Drupal\Tests\feeds\Unit\FeedsUnitTestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
class AfterParseBaseTest extends FeedsUnitTestCase {
protected $subscriber;
protected $parserResult;
protected $event;
public function setUp() {
parent::setUp();
$this->subscriber = $this
->getMockBuilder(AfterParseBase::class)
->setMethods([
'alterItem',
])
->getMock();
$this->parserResult = new ParserResult();
$this->event = $this
->getMockBuilder(ParseEvent::class)
->disableOriginalConstructor()
->setMethods([
'getParserResult',
])
->getMock();
$this->event
->expects($this
->any())
->method('getParserResult')
->willReturn($this->parserResult);
}
public function testAfterParse() {
$item1 = new DynamicItem();
$item1
->set('title', 'Foo');
$item2 = new DynamicItem();
$item2
->set('title', 'Bar');
$this->parserResult
->addItems([
$item1,
$item2,
]);
$this->subscriber
->expects($this
->exactly(2))
->method('alterItem')
->will($this
->returnCallback(function (ItemInterface $item, ParseEvent $event) {
$item
->set('title', $item
->get('title') . '1');
}));
$this->subscriber
->afterParse($this->event);
$this
->assertEquals('Foo1', $item1
->get('title'));
$this
->assertEquals('Bar1', $item2
->get('title'));
}
public function testSkippingItems() {
for ($i = 1; $i <= 5; $i++) {
$item = new DynamicItem();
$item
->set('id', $i);
$this->parserResult
->addItem($item);
}
$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();
}
}));
$this->subscriber
->afterParse($this->event);
$this
->assertCount(3, $this->parserResult);
$expected = [
1,
2,
4,
];
$i = 0;
foreach ($this->parserResult as $item) {
$this
->assertEquals($expected[$i], $item
->get('id'));
$i++;
}
}
public function testApplies() {
$subscriber = $this
->getMockBuilder(AfterParseBase::class)
->setMethods([
'applies',
'alterItem',
])
->getMock();
for ($i = 1; $i <= 3; $i++) {
$item = new DynamicItem();
$item
->set('id', $i);
$this->parserResult
->addItem($item);
}
$subscriber
->expects($this
->never())
->method('alterItem');
$subscriber
->expects($this
->once())
->method('applies')
->willReturn(FALSE);
$subscriber
->afterParse($this->event);
}
public function testDispatch() {
for ($i = 1; $i <= 3; $i++) {
$item = new DynamicItem();
$item
->set('id', $i);
$this->parserResult
->addItem($item);
}
$this->subscriber
->expects($this
->exactly(3))
->method('alterItem');
$dispatcher = new EventDispatcher();
$dispatcher
->addSubscriber($this->subscriber);
$dispatcher
->dispatch(FeedsEvents::PARSE, $this->event);
}
}