/** * @test * @runInSeparateProcess * @covers ::respond * @uses \AyeAye\Api\Request * @uses \AyeAye\Api\Response::setWriterFactory * @uses \AyeAye\Api\Response::setRequest * @uses \AyeAye\Api\Response::getBody * @uses \AyeAye\Api\Response::setBodyData * @uses \AyeAye\Api\Response::prepareResponse */ public function testRespondFull() { $response = new Response(); $writers = ['testWriter']; $data = 'data'; $expectedBody = ['data' => $data]; $response->setBodyData($data); /** @var Request|\PHPUnit_Framework_MockObject_MockObject $request */ $request = $this->getMockRequest(); $request->expects($this->once())->method('getFormats')->with()->will($this->returnValue($writers)); $writer = $this->getMockWriter(); $writer->expects($this->once())->method('format')->with($expectedBody, 'response')->will($this->returnValue(json_encode($expectedBody))); $writer->expects($this->once())->method('getContentType')->with()->will($this->returnValue('')); /** @var WriterFactory|\PHPUnit_Framework_MockObject_MockObject $writerFactory */ $writerFactory = $this->getMockWriterFactory(); $writerFactory->expects($this->once())->method('getWriterFor')->with($writers)->will($this->returnValue($writer)); /** @var Status|\PHPUnit_Framework_MockObject_MockObject $status */ $status = $this->getMockStatus(); $status->expects($this->once())->method('getHttpHeader')->with()->will($this->returnValue(null)); $response->setWriterFactory($writerFactory)->setRequest($request)->setStatus($status); ob_start(); $this->assertSame($response, $response->respond()); $this->assertJsonStringEqualsJsonString(json_encode(['data' => 'data']), ob_get_clean()); }
/** * Returns a standardised 500 error. * * To be used in the event of a catastrophic failure, this method creates * all new objects, ignoring dependency injection and returns in JSON. * * This will be problematic for users expecting a response in a format * other than JSON and should only be called if the format they are * actually expecting can not be provided when using * Response::prepareResponse. * * @return Response */ protected function createFailSafeResponse() { $status = new Status(500); $response = new Response(); $response->setRequest(new Request()); $response->setWriter(new Json()); $response->setStatus($status); $response->setBodyData($status->getMessage()); return $response; }