/** * {@inheritdoc} */ public function handle(ApiCallInterface $apiCall) { if ($apiCall->getId() === null) { $id = $this->identityGenerator->getIdentity(); $apiCall->setId($id); } $serializedApiCall = call_user_func($this->serializer, $apiCall); fwrite($this->stream, $serializedApiCall); }
function it_can_handle_existing_apicall(IdentityGeneratorInterface $identityGenerator, SerializerInterface $serializer, ApiCallInterface $apiCall) { $stream = fopen('php://temp', 'a+'); $this->beConstructedWith($stream, $identityGenerator, $serializer); $identity = 'my id'; $serializedApiCall = 'my api call'; $apiCall->getId()->willReturn($identity); $serializer->__invoke($apiCall)->willReturn($serializedApiCall); $this->handle($apiCall); $writtenData = stream_get_contents($stream, -1, 0); if ($writtenData !== $serializedApiCall) { throw new RuntimeException(sprintf("Written data did not match expected data:\nWritten: %s\nExpected: %s", $writtenData, $serializedApiCall)); } }
function it_can_serialize(ApiCallInterface $apiCall, DateTime $requestDateTime) { $duration = 1.23; $request = '<request />'; $response = '<response />'; $dateTimeFormatted = '1970-01-01T00:00:01+00:00'; $identity = 'my id'; $method = 'My::method()'; $endpoint = 'https://my.endpoint'; $reference = 'my ref'; $apiCall->getDuration()->willReturn($duration); $apiCall->getRequest()->willReturn($request); $apiCall->getResponse()->willReturn($response); $requestDateTime->format('c')->willReturn($dateTimeFormatted); $apiCall->getRequestDateTime()->willReturn($requestDateTime); $apiCall->getId()->willReturn($identity); $apiCall->getMethod()->willReturn($method); $apiCall->getEndpoint()->willReturn($endpoint); $apiCall->getReference()->willReturn($reference); $data = json_encode(array('duration' => $duration, 'request' => $request, 'response' => $response)); $expectedString = sprintf('[%s] (%s) %s (%s) | %s - %s%s', $dateTimeFormatted, $identity, $method, $endpoint, $reference, $data, "\n"); $this->__invoke($apiCall)->shouldReturn($expectedString); }
/** * {@inheritdoc} */ public function __invoke(ApiCallInterface $apiCall) { $data = json_encode(array('duration' => $apiCall->getDuration(), 'request' => $apiCall->getRequest(), 'response' => $apiCall->getResponse())); return sprintf('[%s] (%s) %s (%s) | %s - %s%s', $apiCall->getRequestDateTime()->format('c'), $apiCall->getId(), $apiCall->getMethod(), $apiCall->getEndpoint(), $apiCall->getReference(), $data, $this->eol); }
function it_wont_process_null_response(ApiCallInterface $apiCall, HandlerInterface $handler, ProcessorInterface $processor) { $response = null; $requestTime = 1.2; $apiCall->getRequestTime()->willReturn($requestTime); $apiCall->setResponse($response)->willReturn($apiCall); $apiCall->setDuration(Argument::type('float'))->willReturn($apiCall); $handler->handle($apiCall)->shouldBeCalled(); $processor->__invoke($response)->shouldNotBeCalled(); $this->logResponse($apiCall, $response, $processor); }
/** * @param Exception $exception * @param ApiCallInterface $apiCall * @return void */ protected function logHandlerException(Exception $exception, ApiCallInterface $apiCall) { if ($this->psrLogger === null) { return; } $this->psrLogger->warning($exception->getMessage(), array('exception' => $exception, 'endpoint' => $apiCall->getEndpoint(), 'method' => $apiCall->getMethod(), 'reference' => $apiCall->getReference(), 'request' => $apiCall->getRequest(), 'response' => $apiCall->getResponse())); }