public function testValidateWithTypeRegistered() { $value = 'foobar'; $this->registry->expects($this->once())->method('has')->with($value)->willReturn(true); $this->context->expects($this->never())->method('buildViolation'); $this->subject->validate($value, new JobType()); }
/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (null === $value) { return; } if (!$this->registry->has($value)) { $this->context->buildViolation($constraint->message)->setParameter('{{string}}', $value)->addViolation(); } }
/** * {@inheritdoc} */ public function create(JobInterface $job) { $level = $this->registry->get($job->getType())->getLogLevel(); if (false === $level) { return new NullLogger(); } elseif (null === $level) { $level = $this->level; } $handlers = $this->handlerFactory->createHandlers($job, $level, $this->bubble); return new Logger($this->buildChannel($job), array_merge($handlers, $this->handlers)); }
public function testProduce() { $type = 'JobType'; $ticket = 'JobTicket'; $queue = 'QueueName'; $message = new Message($type, $ticket); $jobType = $this->createMock(JobTypeInterface::class); $jobType->expects($this->once())->method('getQueue')->willReturn($queue); $producerMessage = new DefaultMessage('ConsumeJob', ['type' => $type, 'ticket' => $ticket]); $this->registry->expects($this->once())->method('get')->with($message->getType())->willReturn($jobType); $this->producer->expects($this->once())->method('produce')->with($producerMessage, $queue); $this->subject->produce($message); }
/** * @dataProvider provideValuesWithConstraints * @param array $parameters * @param array $constraints */ public function testValidateWithValue(array $parameters, array $constraints) { $validator = $this->createMock(ValidatorInterface::class); $contextualValidator = $this->createMock(ContextualValidatorInterface::class); $provider = $this->createMock(ConstraintProviderInterface::class); $this->subject->register($provider); $this->registry->expects($this->once())->method('getTypeChoices')->willReturn(['foobar']); $provider->expects($this->any())->method('getConstraints')->with('foobar')->willReturn($constraints); $this->context->expects($this->once())->method('getValidator')->willReturn($validator); $validator->expects($this->once())->method('inContext')->with($this->context)->willReturn($contextualValidator); $contextualValidator->expects($this->exactly(count($constraints)))->method('atPath')->willReturn($contextualValidator); $contextualValidator->expects($this->exactly(count($constraints)))->method('validate'); $this->subject->validate($parameters, new ParametersConstraint(['type' => 'foobar'])); }
/** * Publishes a message to the backend. * * @param Message $message * @return void * @throws \RuntimeException If publishing fails */ public function produce(Message $message) { $type = $message->getType(); $body = array('ticket' => $message->getTicket()); try { $this->logger->debug(sprintf('Publish message for job %s to sonata backend', $message->getTicket()), ['type' => $type, 'body' => $body]); $queue = $this->registry->get($message->getType())->getQueue(); $this->backendProvider->getBackend($queue)->createAndPublish($type, $body); } catch (\Exception $e) { $this->logger->error(sprintf('Failed to publish message (Error: %s)', $e->getMessage()), ['exception' => $e]); if (!$e instanceof \RuntimeException) { $e = new \RuntimeException($e->getMessage(), $e->getCode(), $e); } throw $e; } }
public function testGetTypeChoices() { $callable = array(new TestJob(), 'log'); $jobType = new JobType('service-id', 'JobType', $callable); $this->subject->register($jobType); $this->assertEquals(['JobType'], $this->subject->getTypeChoices()); }
private function initializeConstraints() { $this->constraints = array(); $priority = []; foreach ($this->providers as $provider) { foreach ($this->registry->getTypeChoices() as $type) { $constraints = $provider->getConstraints($type); if (is_array($constraints) && count($constraints) > 0) { if (!isset($this->constraints[$type]) || $provider->getPriority() > $priority[$type]) { $this->constraints[$type] = $constraints; $priority[$type] = $provider->getPriority(); } } } } }
/** * @expectedException \Exception */ public function testAddRethrowsBackendExceptions() { $job = new Job(); $job->setTicket('ticket'); $this->jobManager->expects($this->once())->method('isManagerOf')->with($job)->willReturn(true); $this->registry->expects($this->any())->method('has')->willReturn(true); $this->producer->expects($this->once())->method('produce')->willThrowException(new \Exception()); $this->subject->add($job); }
/** * @param string $type The job type * @return array */ private function getParamTypes($type) { $jobType = $this->registry->get($type); $types = $jobType->getParameterTypes(); $indices = $jobType->getIndicesOfSerializableParameters(); $rs = array(); foreach ($indices as $index) { $rs[] = $types[$index]; } return $rs; }
public function testDeserializeReturnValue() { $jobType = $this->createMock(JobTypeInterface::class); $this->registry->expects($this->once())->method('get')->with('JobType')->willReturn($jobType); $jobType->expects($this->once())->method('getReturnType')->willReturn('ReturnType'); $jobType->expects($this->once())->method('getReturnTypeOptions')->willReturn(['groups' => ['group1', 'group2'], 'version' => '12345']); $expectedContext = new DeserializationContext(); $expectedContext->setGroups(['group1', 'group2']); $expectedContext->setVersion('12345'); $this->serializer->expects($this->once())->method('deserialize')->with('ReturnValue', 'ReturnType', 'json', $expectedContext); $this->subject->deserializeReturnValue('JobType', 'ReturnValue'); }
/** * @expectedException \Exception */ public function testProduceThrowsExceptionsThrownByBackend() { $queue = 'foobar'; $message = new Message('type', 'ticket', 'callback'); $backend = $this->createMock(BackendInterface::class); $jobType = $this->createMock(JobTypeInterface::class); $jobType->expects($this->any())->method('getQueue')->willReturn($queue); $this->registry->expects($this->any())->method('get')->with('type')->willReturn($jobType); $this->backendProvider->expects($this->once())->method('getBackend')->with($queue)->willReturn($backend); $backend->expects($this->once())->method('createAndPublish')->willThrowException(new \Exception()); $this->subject->produce($message); }
/** * Invokes the job. * * @param JobInterface $job * @param ContextInterface $context * @return mixed * @throws JobTypeNotFoundException */ public function invoke(JobInterface $job, ContextInterface $context) { $jobType = $this->registry->get($job->getType()); $callableArray = $jobType->getCallable(); $parameters = static::resolveParameters($jobType, $context, $job->getParameters()); if (is_array($callableArray) && ($callable = $callableArray[0])) { if ($callable instanceof JobAwareInterface) { $callable->setJob($job); } if ($callable instanceof ManagerAwareInterface) { $callable->setManager($this->manager); } if ($callable instanceof ControllerAwareInterface) { $callable->setController($this->controllerFactory->create($job)); } if ($callable instanceof LoggerAwareInterface && $context->has('abc.logger')) { $callable->setLogger($context->get('abc.logger')); } } return call_user_func_array($callableArray, $parameters); }
/** * @param $withLogger * @dataProvider provideTrueFalse */ public function testInvokeHandlesLoggerAwareJobs($withLogger) { $serviceId = 'serviceId'; $type = 'callable-type'; $callable = new LoggerAwareJob(); $jobType = new JobType($serviceId, $type, array($callable, 'execute')); $logger = $this->createMock(LoggerInterface::class); $context = new Context($withLogger ? ['abc.logger' => $logger] : []); $job = new Job($type); $this->registry->register($jobType); $this->assertEquals('foobar', $this->subject->invoke($job, $context)); if ($withLogger) { $this->assertSame($logger, $callable->getLogger()); } else { $this->assertNull($callable->getLogger()); } }
public function testCreatesLoggerWithAddedHandlers() { $job = new Job(); $job->setType('JobType'); $handler = $this->createMock(HandlerInterface::class); $extraHandler = $this->createMock(HandlerInterface::class); $factory = $this->getMockBuilder(BaseHandlerFactory::class)->disableOriginalConstructor()->getMock(); $jobType = $this->createMock(JobTypeInterface::class); $this->handlerFactory->register($factory); $jobType->expects($this->any())->method('getLogLevel')->willReturn(Logger::CRITICAL); $this->registry->expects($this->once())->method('get')->with($job->getType())->willReturn($jobType); $factory->expects($this->once())->method('createHandler')->willReturn($handler); $this->subject->addHandler($extraHandler); $logger = $this->subject->create($job); $this->assertContains($handler, $logger->getHandlers()); $this->assertContains($extraHandler, $logger->getHandlers()); }
/** * {@inheritdoc} */ public function produce(Message $message) { $producerMessage = new DefaultMessage('ConsumeJob', ['type' => $message->getType(), 'ticket' => $message->getTicket()]); $this->logger->debug('Publish message to bernard queue backend', ['message' => $message]); $this->producer->produce($producerMessage, $this->registry->get($message->getType())->getQueue()); }
/** * Deserializes the return of a job. * * @param string $type The job type * @param string $data * @return mixed */ public function deserializeReturnValue($type, $data) { $jobType = $this->registry->get($type); return $this->serializer->deserialize($data, $jobType->getReturnType(), 'json', $this->getResponseDeserializationContext($jobType)); }
/** * @param JobTypeInterface $jobType */ private function setUpRegistry(JobTypeInterface $jobType) { $this->registry->expects($this->any())->method('get')->with($jobType->getName())->willReturn($jobType); }