public function testHydrate_WithoutHydrator()
 {
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData(false);
     $data = $this->bookEntityProvider->getDataFromBookEntity($bookEntity);
     $this->setExpectedException('LogicException', 'Hydrator has not been injected!');
     $testedObj = new BooksRepository($this->entityManagerMock, $this->classMetadataMock);
     $testedObj->hydrate($bookEntity, $data);
 }
 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);
 }
 public function testGetRequest_WithExistingId()
 {
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $data = $this->bookEntityProvider->getDataFromBookEntity($bookEntity);
     $id = $bookEntity->getId();
     $this->serviceMock->expects($this->once())->method('getById')->with($id)->will($this->returnValue($bookEntity));
     $this->serviceMock->expects($this->once())->method('extractEntity')->with($bookEntity)->will($this->returnValue($data));
     $this->dispatch(sprintf(self::GET_URL, $id), Request::METHOD_GET);
     $expectedJson = Json::encode($data);
     $this->assertSame($expectedJson, $this->getResponse()->getContent());
     $this->assertResponseStatusCode(Response::STATUS_CODE_200);
 }
 public function testGetListRequest_WhenEntitiesExists()
 {
     $data = [];
     for ($i = 0; $i < 2; $i += 1) {
         $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
         $data[] = $this->bookEntityProvider->getDataFromBookEntity($bookEntity);
     }
     $paginatorMock = $this->getMockBuilder(Paginator::class)->disableOriginalConstructor()->setMethods(array('count', 'getIterator'))->getMock();
     $paginatorMock->expects($this->any())->method('count')->willReturn(2);
     $paginatorMock->expects($this->any())->method('getIterator')->willReturn($data);
     $this->serviceMock->expects($this->once())->method('getFilteredResults')->will($this->returnValue($paginatorMock));
     $this->dispatch(self::GET_LIST_URL, Request::METHOD_GET);
     $expectedJson = Json::encode(['data' => $data]);
     $this->assertSame($expectedJson, $this->getResponse()->getContent());
     $this->assertResponseStatusCode(Response::STATUS_CODE_200);
 }
Exemple #5
0
 public function testExtractEntity()
 {
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $data = $this->bookEntityProvider->getDataFromBookEntity($bookEntity);
     $this->bookRepositoryMock->expects($this->once())->method('extract')->with($bookEntity)->will($this->returnValue($data));
     $result = $this->testedObj->extractEntity($bookEntity);
     $this->assertSame($data, $result);
 }
 public function testUpdateRequest_WithValidData()
 {
     $this->authenticateUser();
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $id = $bookEntity->getId();
     $newBookEntity = $this->bookEntityProvider->getBookEntityWithRandomData(false);
     $postData = $this->bookEntityProvider->getDataFromBookEntity($newBookEntity);
     Bootstrap::setIdToEntity($newBookEntity, $id);
     $dataAfterSaving = $this->bookEntityProvider->getDataFromBookEntity($newBookEntity);
     $this->serviceMock->expects($this->once())->method('getById')->with($id)->will($this->returnValue($bookEntity));
     $this->filter->setData($postData);
     $this->serviceMock->expects($this->once())->method('update')->with($bookEntity, $this->filter)->will($this->returnValue($newBookEntity));
     $this->serviceMock->expects($this->once())->method('extractEntity')->with($newBookEntity)->will($this->returnValue($dataAfterSaving));
     $this->dispatch(sprintf(self::UPDATE_URL, $id), Request::METHOD_PUT, $postData);
     $expectedJson = Json::encode($dataAfterSaving);
     $this->assertSame($expectedJson, $this->getResponse()->getContent());
     $this->assertResponseStatusCode(Response::STATUS_CODE_200);
 }
 public function testIndexAction_WithValidPostRequest()
 {
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $id = $bookEntity->getId();
     $newBookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $data = $this->bookEntityProvider->getDataFromBookEntity($newBookEntity, false);
     $inputFilterMock = $this->getMock(InputFilterInterface::class);
     $this->crudServiceMock->expects($this->once())->method('getById')->with($id)->will($this->returnValue($bookEntity));
     $this->createFormMock->expects($this->once())->method('get')->with('submit')->will($this->returnSelf());
     $this->createFormMock->expects($this->once())->method('setValue')->with('Update');
     $this->createFormMock->expects($this->once())->method('setData')->with($data);
     $this->createFormMock->expects($this->once())->method('isValid')->will($this->returnValue(true));
     $this->createFormMock->expects($this->once())->method('getInputFilter')->will($this->returnValue($inputFilterMock));
     $this->crudServiceMock->expects($this->once())->method('update')->with($bookEntity, $inputFilterMock);
     $this->routeMatch->setParam('id', $id);
     $result = $this->controller->dispatch((new Request())->setMethod(Request::METHOD_POST)->setPost(new Parameters($data)));
     $this->assertResponseStatusCode(Response::STATUS_CODE_200);
     $this->assertSame(['form' => $this->createFormMock], $result);
 }
 public function testIndexAction_WithExceptionInService()
 {
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $data = $this->bookEntityProvider->getDataFromBookEntity($bookEntity, false);
     $data['csrf'] = 'csrfToken';
     $inputFilterMock = $this->getMock(InputFilterInterface::class);
     $this->createFormMock->expects($this->once())->method('get')->will($this->returnSelf());
     $this->createFormMock->expects($this->once())->method('setValue')->with('Create');
     $this->createFormMock->expects($this->once())->method('setData')->with($data);
     $this->createFormMock->expects($this->once())->method('isValid')->will($this->returnValue(true));
     $this->createFormMock->expects($this->once())->method('getInputFilter')->will($this->returnValue($inputFilterMock));
     $this->crudServiceMock->expects($this->once())->method('create')->with($inputFilterMock)->will($this->throwException(new \InvalidArgumentException('Some error')));
     $this->controller->dispatch((new Request())->setMethod(Request::METHOD_POST)->setPost(new Parameters($data)));
     $this->assertResponseStatusCode(Response::STATUS_CODE_302);
     $this->assertRedirectTo('/library/books');
 }