Example #1
0
 /**
  * @expectedException \Tebru\Executioner\Exception\FailedException
  */
 public function testWithLogger()
 {
     $callable = new MockeryCallableMock();
     $exception = new Exception();
     $callable->shouldBeCalled()->times(1)->withNoArgs()->andThrow($exception);
     $callable->shouldBeCalled()->times(1)->withNoArgs()->andReturn(true);
     $logger = Mockery::mock(LoggerInterface::class);
     $dispatchException = new Exception();
     $logger->shouldReceive('info')->times(1)->with('Attempting "test" with 1 attempts to go. (uq)');
     $logger->shouldReceive('info')->times(1)->with('Attempting "test" with 0 attempts to go. (uq)');
     $logger->shouldReceive('info')->times(1)->with('Completed attempt for "test" (uq)', ['result' => true])->andThrow($dispatchException);
     $logger->shouldReceive('notice')->times(1)->with('Failed attempt for "test", retrying. 0 attempts remaining (uq)', ['exception' => $exception]);
     $logger->shouldReceive('error')->times(1)->with('Could not complete "test" (uq)', ['exception' => $dispatchException]);
     $loggerSubscriber = new LoggerSubscriber('test', $logger, 'uq');
     $dispatcher = Mockery::mock(EventDispatcher::class);
     $dispatcher->makePartial();
     $dispatcher->shouldReceive('dispatch')->times(5)->passthru();
     $dispatcher->shouldReceive('addSubscriber')->times(1)->with($loggerSubscriber)->passthru();
     $executor = new Executor();
     $executor->setDispatcher($dispatcher);
     $executor->addSubscriber($loggerSubscriber);
     $executor->execute(1, $callable);
 }
Example #2
0
 /**
  * Make an Executor
  *
  * If adding a logger, both name and instance must be passed in.  If adding a wait, it must
  * be an int to represent seconds, or a WaitStrategy
  *
  * @param null $loggerName
  * @param LoggerInterface $logger
  * @param int|WaitStrategy $wait WaitStrategy or wait time in seconds
  * @param EventDispatcherInterface $eventDispatcher
  * @return Executor
  */
 public static function make($loggerName = null, LoggerInterface $logger = null, $wait = null, EventDispatcherInterface $eventDispatcher = null)
 {
     $executor = new Executor();
     if (!is_null($loggerName) xor !is_null($logger)) {
         throw new InvalidArgumentException('Logger name and logger must both be set');
     }
     if (!is_null($wait) && !is_int($wait) && !$wait instanceof WaitStrategy) {
         throw new InvalidArgumentException('Wait must be an int or an instance of WaitStrategy');
     }
     if (!is_null($loggerName) && !is_null($logger)) {
         $executor->setLogger($loggerName, $logger);
     }
     if (is_int($wait)) {
         $executor->setWait($wait);
     }
     if ($wait instanceof WaitStrategy) {
         $executor->setWaitStrategy($wait);
     }
     if (null !== $eventDispatcher) {
         $executor->setDispatcher($eventDispatcher);
     }
     return $executor;
 }