Example #1
0
 public function indexAction()
 {
     /**
      * @var Request  $request
      * @var Response $response
      */
     $request = $this->getRequest();
     $response = $this->getResponse();
     $id = $this->params()->fromRoute('id', null);
     try {
         $bookEntity = $this->service->getById($id);
         $this->filter->setData($request->getPost()->toArray());
         if ($this->filter->isValid()) {
             $bookEntity = $this->service->update($bookEntity, $this->filter);
             return new JsonModel($this->service->extractEntity($bookEntity));
         } else {
             $messages = $this->filter->getMessages();
             $response->setStatusCode(Response::STATUS_CODE_400);
             return new JsonModel(['error' => ['messages' => $messages]]);
         }
     } catch (EntityNotFoundException $e) {
         throw new Exception\NotFoundException();
     } catch (\PDOException $e) {
         throw new Exception\PDOServiceUnavailableException();
     }
 }
 public function testCreateRequest_WithInvalidData()
 {
     $this->authenticateUser();
     $data = [];
     $this->filter->setData($data);
     $this->dispatch(self::CREATE_URL, Request::METHOD_POST, $data);
     $expectedJson = Json::encode(['error' => ['messages' => $this->filter->getMessages()]]);
     $this->assertSame($expectedJson, $this->getResponse()->getContent());
     $this->assertResponseStatusCode(Response::STATUS_CODE_400);
 }
 public function testUpdateRequest_WithInvalidData()
 {
     $this->authenticateUser();
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $id = $bookEntity->getId();
     $postData = [];
     $this->filter->setData($postData);
     $this->dispatch(sprintf(self::UPDATE_URL, $id), Request::METHOD_PUT, $postData);
     $expectedJson = Json::encode(['error' => ['messages' => $this->filter->getMessages()]]);
     $this->assertSame($expectedJson, $this->getResponse()->getContent());
     $this->assertResponseStatusCode(Response::STATUS_CODE_400);
 }
Example #4
0
 public function indexAction()
 {
     /**
      * @var Request  $request
      * @var Response $response
      */
     $request = $this->getRequest();
     $response = $this->getResponse();
     $this->filter->setData($request->getPost()->toArray());
     if (!$this->filter->isValid()) {
         $messages = $this->filter->getMessages();
         $response->setStatusCode(Response::STATUS_CODE_400);
         return new JsonModel(['error' => ['messages' => $messages]]);
     }
     try {
         $bookEntity = $this->service->create($this->filter);
         $response->setStatusCode(Response::STATUS_CODE_201);
         return new JsonModel($this->service->extractEntity($bookEntity));
     } catch (\PDOException $e) {
         throw new Exception\PDOServiceUnavailableException();
     }
 }