public function testCanHintToApiProblemToRenderStackTrace() { $exception = new \Exception('exception message', 500); $apiProblem = new ApiProblem(500, $exception); $model = new RestfulJsonModel(); $model->setPayload($apiProblem); $this->renderer->setDisplayExceptions(true); $test = $this->renderer->render($model); $test = json_decode($test, true); $this->assertContains($exception->getMessage() . "\n" . $exception->getTraceAsString(), $test['detail']); }
public function testChildResourceObjectIdentiferMappingInCollectionsViaControllerReturn() { $this->setUpAlternateRouter(); $resource = new Resource(); $resource->getEventManager()->attach('fetchAll', function ($e) { return array((object) array('id' => 'luke', 'name' => 'Luke Skywalker'), (object) array('id' => 'leia', 'name' => 'Leia Organa')); }); $controller = new ResourceController(); $controller->setPluginManager($this->plugins); $controller->setResource($resource); $controller->setRoute('parent/child'); $controller->setIdentifierName('child_id'); $controller->setCollectionName('children'); $r = new ReflectionObject($controller); $m = $r->getMethod('getIdentifier'); $m->setAccessible(true); $uri = 'http://localhost.localdomain/api/parent/anakin/child'; $request = new Request(); $request->setUri($uri); $matches = $this->router->match($request); $this->assertInstanceOf('Zend\\Mvc\\Router\\RouteMatch', $matches); $this->assertEquals('anakin', $matches->getParam('id')); $this->assertNull($matches->getParam('child_id')); $this->assertEquals('parent/child', $matches->getMatchedRouteName()); // Emulate url helper factory and inject route matches $this->helpers->get('url')->setRouteMatch($matches); $result = $controller->getList(); $this->assertInstanceOf('PhlyRestfully\\HalCollection', $result); // Now, what happens if we render this? $model = new RestfulJsonModel(); $model->setPayload($result); $json = $this->renderer->render($model); $test = json_decode($json); $this->assertObjectHasAttribute('_links', $test); $this->assertObjectHasAttribute('self', $test->_links); $this->assertObjectHasAttribute('href', $test->_links->self); $this->assertEquals('http://localhost.localdomain/api/parent/anakin/child', $test->_links->self->href); $this->assertObjectHasAttribute('_embedded', $test); $this->assertObjectHasAttribute('children', $test->_embedded); $this->assertInternalType('array', $test->_embedded->children); foreach ($test->_embedded->children as $child) { $this->assertObjectHasAttribute('_links', $child); $this->assertObjectHasAttribute('self', $child->_links); $this->assertObjectHasAttribute('href', $child->_links->self); $this->assertRegexp('#^http://localhost.localdomain/api/parent/anakin/child/[^/]+$#', $child->_links->self->href); } }
/** * Listen to the render event * * @param MvcEvent $e */ public static function onRender(MvcEvent $e) { // only worried about error pages if (!$e->isError()) { return; } // and then, only if we have an Accept header... $request = $e->getRequest(); if (!$request instanceof HttpRequest) { return; } $headers = $request->getHeaders(); if (!$headers->has('Accept')) { return; } // ... that matches certain criteria $accept = $headers->get('Accept'); $match = $accept->match(self::$acceptFilter); if (!$match || $match->getTypeString() == '*/*') { return; } // Next, do we have a view model in the result? // If not, nothing more to do. $model = $e->getResult(); if (!$model instanceof ModelInterface) { return; } // Marshall the information we need for the API-Problem response $httpStatus = $e->getResponse()->getStatusCode(); $exception = $model->getVariable('exception'); if ($exception instanceof \Exception) { $apiProblem = new ApiProblem($httpStatus, $exception); } else { $apiProblem = new ApiProblem($httpStatus, $model->getVariable('message')); } // Create a new model with the API-Problem payload, and reset // the result and view model in the event using it. $model = new RestfulJsonModel(array('payload' => $apiProblem)); $model->setTerminal(true); $e->setResult($model); $e->setViewModel($model); }