Skip to content

Commit

Permalink
Merge pull request #14 from interactive-solutions/increased-bernard-s…
Browse files Browse the repository at this point in the history
…upport

Increased bernard support
  • Loading branch information
Hotas2k authored Apr 11, 2017
2 parents 9bebd03 + 645775c commit a0871ae
Showing 1 changed file with 123 additions and 18 deletions.
141 changes: 123 additions & 18 deletions src/Context/BernardContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@
*
* @copyright Interactive Solutions
*/
declare(strict_types = 1);
declare(strict_types=1);

namespace InteractiveSolutions\ZfBehat\Context;

use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Bernard\BernardEvents;
use Bernard\Consumer;
use Bernard\Envelope;
use Bernard\Event\EnvelopeEvent;
use Bernard\Event\RejectEnvelopeEvent;
use Bernard\QueueFactory;
use Closure;
use InteractiveSolutions\Bernard\BernardOptions;
use InteractiveSolutions\Bernard\EventDispatcherInterface;
use InteractiveSolutions\Bernard\Producer;
use InteractiveSolutions\ZfBehat\Assertions;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;

class BernardContext implements SnippetAcceptingContext, ServiceManagerAwareInterface
{
use ServiceLocatorAwareTrait;

/**
* @var ServiceManager
*/
Expand All @@ -30,6 +39,11 @@ class BernardContext implements SnippetAcceptingContext, ServiceManagerAwareInte
*/
private $apiContext;

/**
* @var EnvelopeEvent|RejectEnvelopeEvent
*/
private $eventOfLastExecutedTask;

/**
* Inject the entity manager
*
Expand All @@ -44,6 +58,14 @@ public function bootstrap(BeforeScenarioScope $scope)
$this->apiContext = $scope->getEnvironment()->getContext(ApiContext::class);
}

/**
* @BeforeScenario
*/
public function clearDataOfLastExecutedTask()
{
$this->eventOfLastExecutedTask = null;
}

/**
* Set service manager
*
Expand All @@ -55,35 +77,27 @@ public function setServiceManager(ServiceManager $serviceManager)
}

/**
* @return QueueFactory
* @return Consumer
*/
private function getQueueFactory()
public function getConsumer()
{
/* @var $options BernardOptions */
$options = $this->serviceManager->get(BernardOptions::class);

/* @var QueueFactory $queueFactory */
$queueFactory = $this->serviceManager->get($options->getQueueInstanceKey());

return $queueFactory;
return $this->serviceManager->get(Consumer::class);
}

/**
* @param $queueName
*
* @return \Bernard\Queue
* @return Producer
*/
private function createQueue($queueName)
public function getProducer()
{
return $this->getQueueFactory()->create($queueName);
return $this->serviceManager->get(Producer::class);
}

/**
* @param $queueName
* @return EventDispatcherInterface
*/
private function clearQueue($queueName)
public function getEventDispatcher()
{
return $this->getQueueFactory()->remove($queueName);
return $this->serviceManager->get(EventDispatcherInterface::class);
}

/**
Expand Down Expand Up @@ -111,6 +125,7 @@ public function thenQueueShouldContainTask($queueName, $taskName)
foreach ($queue->peek() as $envelope) {
if ($taskName === $envelope->getName()) {
Assertions::assertEquals($taskName, $envelope->getName());

return true;
}
}
Expand Down Expand Up @@ -163,6 +178,64 @@ public function theTaskInQueueWithIndexShouldHaveTheFieldWithValue($queueName, $
Assertions::fail(sprintf('Could not find task with index: %s in queue: %s', $index, $queueName));
}

/**
* @When the first task from queue :queueName is executed
*
* @param $queueName
*/
public function theFirstTaskFromQueueIsExecuted($queueName)
{
$eventDispatcher = $this->getEventDispatcher();
$queue = $this->createQueue($queueName);
$consumer = $this->getConsumer();

$eventDispatcher->addListener(BernardEvents::REJECT, [$this, 'onTaskExecuted']);
$eventDispatcher->addListener(BernardEvents::ACKNOWLEDGE, [$this, 'onTaskExecuted']);

$consumer->tick($queue);

$eventDispatcher->removeListener(BernardEvents::REJECT, [$this, 'onTaskExecuted']);
$eventDispatcher->removeListener(BernardEvents::ACKNOWLEDGE, [$this, 'onTaskExecuted']);
}

/**
* @Then the last executed task should be acknowledged
*/
public function theLastExecutedTaskShouldBeAcknowledged()
{
Assertions::assertInstanceOf(EnvelopeEvent::class, $this->eventOfLastExecutedTask);
}

/**
* @Then the last executed task should be rejected
*/
public function theLastExecutedTaskShouldBeRejected()
{
Assertions::assertInstanceOf(RejectEnvelopeEvent::class, $this->eventOfLastExecutedTask);
}

/**
* @Then dump the exception of the last executed task
*/
public function dumpExceptionOfLastExecutedTask()
{
if ($this->eventOfLastExecutedTask && $this->eventOfLastExecutedTask instanceof RejectEnvelopeEvent) {
var_dump($this->eventOfLastExecutedTask->getException());
}

var_dump(null);
}

/**
* Called when bernard task is completed
*
* @param EnvelopeEvent|RejectEnvelopeEvent $event
*/
public function onTaskExecuted($event)
{
$this->eventOfLastExecutedTask = $event;
}

/**
* Binds an anonymous function to an object, allowing us to access
* instance variables directly
Expand All @@ -181,4 +254,36 @@ private function getFieldOfObject($object, $field)

return $getValue($object, $field);
}

/**
* @return QueueFactory
*/
private function getQueueFactory()
{
/* @var $options BernardOptions */
$options = $this->serviceManager->get(BernardOptions::class);

/* @var QueueFactory $queueFactory */
$queueFactory = $this->serviceManager->get($options->getQueueInstanceKey());

return $queueFactory;
}

/**
* @param $queueName
*
* @return \Bernard\Queue
*/
private function createQueue($queueName)
{
return $this->getQueueFactory()->create($queueName);
}

/**
* @param $queueName
*/
private function clearQueue($queueName)
{
return $this->getQueueFactory()->remove($queueName);
}
}

0 comments on commit a0871ae

Please sign in to comment.