public function testGetEntity() { $controller = new JsonApiController(); $meta = $this->getMockBuilder("Doctrine\\ORM\\Mapping\\ClassMetadata")->setConstructorArgs(["NwApi\\Entities\\User"])->getMock(); $di = Di::getInstance(); $di->em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock(); $di->em->expects($this->any())->method('find')->willReturn(new NwApi\Entities\User()); $meta->identifier = ['id']; $controller->getEntity($meta, [$this->faker->randomDigit()]); }
private function addRoutesFromMeta(Slim $application, ClassMetadata $meta, Controller $controller) { $entitiesRoute = $this->getEntitiesRoute($meta); // Fetch entities route $application->get($entitiesRoute, function () use($meta, $controller) { $controller->getEntities($meta); }); // Create entity $application->post($entitiesRoute, function () use($meta, $controller) { $controller->createEntity($meta); }); $entityRoute = $this->getEntityRoute($meta, $entitiesRoute); // Get entity $application->get($entityRoute, function () use($meta, $controller) { $controller->getEntity($meta, func_get_args()); }); // Update entity $application->put($entityRoute, function () use($meta, $controller) { $controller->updateEntity($meta, func_get_args()); }); // Patch entity $application->patch($entityRoute, function () use($meta, $controller) { $controller->patchEntity($meta, func_get_args()); }); // Delete entity $application->delete($entityRoute, function () use($meta, $controller) { $controller->deleteEntity($meta, func_get_args()); }); // Handling associated entities foreach ($meta->getAssociationMappings() as $aName => $aData) { $aTargetClass = $meta->getAssociationTargetClass($aName); $aMeta = $this->getEntityMeta($aTargetClass); $aEntitiesRoute = $entityRoute . '/' . $aName; // Create associated entity // allow to create entity and link source together // POST /articles/1/tags will fetch article 1, create tag entity and // associate it to article 1 $application->post($aEntitiesRoute, function () use($meta, $aMeta, $controller, $aData) { $controller->createEntity($aMeta, $aData['fieldName'], $meta, func_get_args()); }); // List associated entities $application->get($aEntitiesRoute, function () use($meta, $controller, $aData) { $controller->getAssociatedEntities($aData['fieldName'], $meta, func_get_args()); }); // Associate two entities // POST /articles/1/tags/2 will associate article 1 to tag 2 $aEntityRoute = $this->getEntityRoute($aMeta, $aEntitiesRoute); $application->post($aEntityRoute, function () use($meta, $aMeta, $controller, $aData) { $controller->associateEntities($aMeta, $aData['fieldName'], $meta, func_get_args()); }); } return $application; }
public function call() { try { try { // Append route to the app $di = Di::getInstance(); $this->app = $di->jsonApiRouter->addRoutes($this->app, JsonApiController::getInstance()); $this->next->call(); if ($this->app->response->headers->get('Content-Type') === 'application/json') { $this->handleJsonResponse(); } } catch (HttpException $ex) { $this->app->response->setStatus($ex->getCode()); throw $ex; } } catch (Exception $ex) { $acceptContentType = $this->app->request->headers->get('Accept'); $responseStatus = $this->app->response->getStatus(); // If response is not marked as error if ($responseStatus < 400) { if ($ex instanceof ServerException) { if ($responseStatus < 500) { $this->app->response->setStatus(500); } } else { $this->app->response->setStatus(503); } } $di = Di::getInstance(); // We display message only if it is a valid client exception if ($ex instanceof ClientException || $di->env === ENV_DEVELOPMENT) { $exceptionMessage = $ex->getMessage(); } else { $exceptionMessage = 'Internal server error'; } // If client does not support html, we send json if (strpos($acceptContentType, 'text/html') === false) { $this->app->response->headers->set('Content-Type', 'application/json'); $data = ['error' => $exceptionMessage]; if ($di->env === ENV_DEVELOPMENT) { $data['file'] = $ex->getFile(); $data['line'] = $ex->getLine(); } $this->app->response->setBody(json_encode($data)); } else { // If we are in dev, pass to PrettyException if ($di->env === ENV_DEVELOPMENT) { throw $ex; } $this->app->response->headers->set('Content-Type', 'text/html'); $this->app->response->setBody('<!DOCTYPE html><html><head><title></title></head><body><p>' . htmlspecialchars($exceptionMessage) . '</p></body></html>'); } } }