public function testGetFilteredResult()
 {
     $bookEntity1 = $this->bookEntityProvider->getBookEntityWithRandomData();
     $bookEntity2 = $this->bookEntityProvider->getBookEntityWithRandomData();
     $books = [$bookEntity1, $bookEntity2];
     $this->bookRepositoryMock->expects($this->once())->method('findByQueryFilter')->will($this->returnValue($books));
     $result = $this->testedObj->getFilteredResults(new QueryFilter([], []));
     $this->assertSame($books, $result);
 }
示例#2
0
 public function testIndexAction_WithValidGetRequest()
 {
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $id = $bookEntity->getId();
     $this->crudServiceMock->expects($this->once())->method('getById')->with($id)->will($this->returnValue($bookEntity));
     $this->routeMatch->setParam('id', $id);
     $result = $this->controller->dispatch(new Request());
     $this->assertResponseStatusCode(Response::STATUS_CODE_200);
     $this->assertSame(['book' => $bookEntity], $result);
 }
 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 testDeleteRequest_WithExistingId()
 {
     $this->authenticateUser();
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $id = $bookEntity->getId();
     $this->serviceMock->expects($this->once())->method('getById')->with($id)->will($this->returnValue($bookEntity));
     $this->serviceMock->expects($this->once())->method('delete')->with($bookEntity);
     $this->dispatch(sprintf(self::DELETE_URL, $id), Request::METHOD_DELETE);
     $expectedJson = Json::encode(['data' => "Books with id {$id} has been deleted"]);
     $this->assertSame($expectedJson, $this->getResponse()->getContent());
     $this->assertResponseStatusCode(Response::STATUS_CODE_200);
 }
 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 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);
 }
示例#7
0
 public function testSettersAndGetters()
 {
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $this->testedObj->setTitle($bookEntity->getTitle())->setDescription($bookEntity->getDescription())->setIsbn($bookEntity->getIsbn())->setPublisher($bookEntity->getPublisher())->setYear($bookEntity->getYear());
     $this->assertSame($bookEntity->getTitle(), $this->testedObj->getTitle());
     $this->assertSame($bookEntity->getDescription(), $this->testedObj->getDescription());
     $this->assertSame($bookEntity->getIsbn(), $this->testedObj->getIsbn());
     $this->assertSame($bookEntity->getPublisher(), $this->testedObj->getPublisher());
     $this->assertSame($bookEntity->getYear(), $this->testedObj->getYear());
     $this->testedObj->setPrice('841,21');
     $this->assertSame(841.21, $this->testedObj->getPrice());
 }
示例#8
0
 public function testDelete_WithoutFlush()
 {
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $this->entityManagerMock->expects($this->once())->method('remove')->with($bookEntity);
     $this->entityManagerMock->expects($this->never())->method('flush');
     $this->testedObj->delete($bookEntity, false);
 }
示例#9
0
 public function testIndexAction_WithGetRequest()
 {
     $bookEntityOne = $this->bookEntityProvider->getBookEntityWithRandomData();
     $bookEntityTwo = $this->bookEntityProvider->getBookEntityWithRandomData();
     $books = [$bookEntityOne, $bookEntityTwo];
     $paginatorMock = $this->getMockBuilder(PaginatorAdapter::class)->disableOriginalConstructor()->setMethods(array('count', 'getIterator'))->getMock();
     $paginatorMock->expects($this->any())->method('count')->willReturn(2);
     $paginatorMock->expects($this->any())->method('getIterator')->willReturn($books);
     $this->filterResultsServiceMock->expects($this->once())->method('getFilteredResults')->will($this->returnValue($paginatorMock));
     $paginator = new PaginatorInfo(2, 1, 10);
     $this->paginatorInfoFactoryMock->expects($this->once())->method('create')->with(2)->willReturn($paginator);
     $result = $this->controller->dispatch(new Request());
     $this->assertResponseStatusCode(Response::STATUS_CODE_200);
     $expectedResult = array('books' => $books, 'paginator' => $paginator, 'route' => 'library/books/*', 'query' => array('$page' => 1, '$limit' => 10));
     $this->assertSame($expectedResult, $result);
 }
 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);
 }
示例#11
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);
 }
示例#12
0
 public function testIndexAction_WithExceptionInService()
 {
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $id = $bookEntity->getId();
     $data = [];
     $this->crudServiceMock->expects($this->once())->method('getById')->with($id)->will($this->throwException(new EntityNotFoundException()));
     $this->routeMatch->setParam('id', $id);
     $this->controller->dispatch((new Request())->setMethod(Request::METHOD_POST)->setPost(new Parameters($data)));
     $this->assertResponseStatusCode(Response::STATUS_CODE_302);
     $this->assertRedirectTo('/library/books');
 }
示例#13
0
 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');
 }