-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
265 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ConnectionsBundle\EventListener; | ||
|
||
use ONGR\ConnectionsBundle\Pipeline\Event\ItemPipelineEvent; | ||
|
||
/** | ||
* AbstractConsumeEventListener class. | ||
*/ | ||
abstract class AbstractConsumeEventListener | ||
{ | ||
/** | ||
* Entry point of consume event. | ||
* | ||
* @param ItemPipelineEvent $event | ||
*/ | ||
public function onConsume(ItemPipelineEvent $event) | ||
{ | ||
if ($event->getItemSkipException()) { | ||
$this->skip($event); | ||
} else { | ||
$this->consume($event); | ||
} | ||
} | ||
|
||
/** | ||
* Called when item should be skipped. | ||
* | ||
* @param ItemPipelineEvent $event | ||
*/ | ||
public function skip(ItemPipelineEvent $event) | ||
{ | ||
} | ||
|
||
/** | ||
* Called when item should be consumed. | ||
* | ||
* @param ItemPipelineEvent $event | ||
*/ | ||
abstract public function consume(ItemPipelineEvent $event); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ConnectionsBundle\Pipeline; | ||
|
||
/** | ||
* Skip Item Exception. | ||
*/ | ||
class ItemSkipException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ConnectionsBundle\Tests\Unit\Pipeline; | ||
|
||
use ONGR\ConnectionsBundle\Pipeline\Event\ItemPipelineEvent; | ||
use ONGR\ConnectionsBundle\Pipeline\Event\SourcePipelineEvent; | ||
use ONGR\ConnectionsBundle\Pipeline\ItemSkipException; | ||
use ONGR\ConnectionsBundle\Pipeline\PipelineFactory; | ||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||
|
||
/** | ||
* PipelineTest class. | ||
*/ | ||
class PipelineTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Tests pipeline. | ||
* | ||
* @param array $data | ||
* @param int $expectedConsumedItems | ||
* @param int $expectedSkippedItems | ||
* | ||
* @dataProvider PipelineData | ||
*/ | ||
public function testPipeline($data, $expectedConsumedItems, $expectedSkippedItems) | ||
{ | ||
$pipelineFactory = new PipelineFactory(); | ||
$pipelineFactory->setDispatcher(new EventDispatcher()); | ||
$pipelineFactory->setClassName('ONGR\ConnectionsBundle\Pipeline\Pipeline'); | ||
|
||
$consumer = new PipelineTestConsumer(); | ||
|
||
$source = function (SourcePipelineEvent $event) use ($data) { | ||
$event->addSource($data); | ||
}; | ||
|
||
$pipeline = $pipelineFactory->create( | ||
'test', | ||
[ | ||
'sources' => [$source], | ||
'modifiers' => [[$this, 'onModify']], | ||
'consumers' => [[$consumer, 'onConsume']], | ||
] | ||
); | ||
$pipeline->start(); | ||
|
||
$this->assertEquals($expectedConsumedItems, $consumer->getConsumeCalled()); | ||
$this->assertEquals($expectedSkippedItems, $consumer->getSkipCalled()); | ||
} | ||
|
||
/** | ||
* OnModify. | ||
* | ||
* @param ItemPipelineEvent $event | ||
* | ||
* @throws ItemSkipException | ||
*/ | ||
public function onModify(ItemPipelineEvent $event) | ||
{ | ||
if ($event->getItem() == 'skip') { | ||
throw new ItemSkipException(); | ||
} | ||
} | ||
|
||
/** | ||
* Pipeline data provider. | ||
* | ||
* @return array | ||
*/ | ||
public function pipelineData() | ||
{ | ||
return [ | ||
[[], 0, 0], | ||
[['consume'], 1, 0], | ||
[['skip'], 0, 1], | ||
[['skip', 'consume', 'skip'], 1, 2], | ||
[['consume', 'skip', 'consume'], 2, 1], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ConnectionsBundle\Tests\Unit\Pipeline; | ||
|
||
use ONGR\ConnectionsBundle\EventListener\AbstractConsumeEventListener; | ||
use ONGR\ConnectionsBundle\Pipeline\Event\ItemPipelineEvent; | ||
|
||
/** | ||
* PipelineTestConsumer class. | ||
*/ | ||
class PipelineTestConsumer extends AbstractConsumeEventListener | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $consumeCalled = 0; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $skipCalled = 0; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function consume(ItemPipelineEvent $event) | ||
{ | ||
++$this->consumeCalled; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function skip(ItemPipelineEvent $event) | ||
{ | ||
++$this->skipCalled; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getConsumeCalled() | ||
{ | ||
return $this->consumeCalled; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getSkipCalled() | ||
{ | ||
return $this->skipCalled; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters