示例#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_WhenServiceThrowPDOException()
 {
     $this->authenticateUser();
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData(false);
     $dataBeforeSaving = $this->bookEntityProvider->getDataFromBookEntity($bookEntity);
     $this->filter->setData($dataBeforeSaving);
     $this->serviceMock->expects($this->once())->method('create')->with($this->filter)->will($this->throwException(new \PDOException()));
     $this->dispatch(self::CREATE_URL, Request::METHOD_POST, $dataBeforeSaving);
     $expectedJson = '{"errorCode":503,"message":"PDO Service Unavailable"}';
     $this->assertSame($expectedJson, $this->getResponse()->getContent());
     $this->assertResponseStatusCode(Response::STATUS_CODE_503);
 }
 /**
  * @dataProvider dataProviderForTestPriceElementByValue
  *
  * @param array $value
  * @param bool  $expectedResult
  */
 public function testPriceElementByValue($value, $expectedResult)
 {
     $input = $this->testedObj->get('price');
     $input->setValue($value);
     $result = $input->isValid();
     $this->assertSame($expectedResult, $result);
 }
 public function testUpdateRequest_WhenServiceThrowPDOException()
 {
     $this->authenticateUser();
     $id = 154;
     $postData = [];
     $this->filter->setData($postData);
     $this->serviceMock->expects($this->once())->method('getById')->with($id)->will($this->throwException(new \PDOException()));
     $this->dispatch(sprintf(self::UPDATE_URL, $id), Request::METHOD_PUT, $postData);
     $expectedJson = '{"errorCode":503,"message":"PDO Service Unavailable"}';
     $this->assertSame($expectedJson, $this->getResponse()->getContent());
     $this->assertResponseStatusCode(Response::STATUS_CODE_503);
 }
示例#5
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();
     }
 }