/** * @covers Imbo\Http\Response\Response::setError */ public function testUpdatesResponseWhenSettingAnErrorModel() { $message = 'You wronged'; $code = 404; $imboErrorCode = 123; $date = new DateTime('@1361614522', new DateTimeZone('UTC')); $error = $this->getMock('Imbo\\Model\\Error'); $error->expects($this->once())->method('getHttpCode')->will($this->returnValue($code)); $error->expects($this->once())->method('getImboErrorCode')->will($this->returnValue($imboErrorCode)); $error->expects($this->once())->method('getErrorMessage')->will($this->returnValue($message)); $error->expects($this->once())->method('getDate')->will($this->returnValue($date)); $this->response->headers->set('ETag', '"sometag"'); $this->response->setLastModified(new DateTime('now', new DateTimeZone('UTC'))); $this->response->setError($error); $this->assertSame($code, $this->response->getStatusCode()); $this->assertSame($message, $this->response->headers->get('X-Imbo-Error-Message')); $this->assertSame($imboErrorCode, $this->response->headers->get('X-Imbo-Error-InternalCode')); $this->assertNull($this->response->headers->get('ETag')); $this->assertNull($this->response->getLastModified()); }
/** * Construct a message body with the data we need * * @param string $eventName Event that was triggered * @param Request $request Request that triggered this event * @param Response $response Response for this request * @param Imbo\EventManager\EventInterface $eventName Current event * @return array */ public function constructMessageBody($eventName, Request $request, Response $response, EventInterface $event) { if ($response->getModel() instanceof ErrorModel) { trigger_error($response->getModel()->getErrorMessage()); return; } // Construct the basics $message = ['eventName' => $eventName, 'request' => ['date' => date('c'), 'user' => $request->getUser(), 'publicKey' => $request->getPublicKey(), 'extension' => $request->getExtension(), 'url' => $request->getRawUri(), 'clientIp' => $request->getClientIp()], 'response' => ['statusCode' => $response->getStatusCode()]]; // Include any JSON request body in the message if ($request->getContentType() === 'application/json') { $message['request']['body'] = $request->getContent(); } // See if we've got an image identifier for this request $imageIdentifier = $request->getImageIdentifier(); // The imageIdentifier was not part of the URL, see if we have it in the response model if (!$imageIdentifier) { $responseData = $response->getModel()->getData(); if (isset($responseData['imageIdentifier'])) { $imageIdentifier = $responseData['imageIdentifier']; } else { return $message; } } // Get image information $image = $this->getImageData($event, $imageIdentifier); // Construct an array of all the image information we have $message['image'] = ['identifier' => $imageIdentifier, 'user' => $image->getUser() ?: $request->getUser(), 'size' => $image->getFilesize(), 'extension' => $image->getExtension(), 'mime' => $image->getMimeType(), 'added' => $image->getAddedDate()->format(DateTime::ATOM), 'updated' => $image->getUpdatedDate()->format(DateTime::ATOM), 'width' => $image->getWidth(), 'height' => $image->getHeight(), 'metadata' => $image->getMetadata()]; return $message; }