protected function createPagingConfig()
 {
     $pagingConfig = [];
     foreach ($this->pagingProperties as $propertyName) {
         $pagingConfig[$propertyName] = $this->command->{$propertyName};
     }
     $this->pagingConfig = FilterPagingProperties::fromArray($pagingConfig);
 }
 /**
  * @test
  */
 public function testSearch()
 {
     $a = new EntityStub();
     $b = new EntityStub();
     $a->name = "DUMMY_NAME";
     $a->status = "new";
     $a->id = 1;
     $b->name = "NOT_DUMMY_NAME";
     $b->status = "closed";
     $b->id = 2;
     $entities = array($a, $b);
     $pagingConfig = new FilterPagingProperties();
     $querySearch = 'NAM';
     $conditions = array(array('status', 'eq', 'new'));
     $this->em->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($this->queryBuilder));
     $this->queryBuilder->expects($this->once())->method('select')->with($this->equalTo('e'))->will($this->returnValue($this->queryBuilder));
     $this->queryBuilder->expects($this->once())->method('from')->with($this->equalTo($this->classMetadata->name), $this->equalTo('e'))->will($this->returnValue($this->queryBuilder));
     $whereExpr = new \Doctrine\ORM\Query\Expr\Comparison('e.status', 'eq', 'new');
     $likeExprSubject = new \Doctrine\ORM\Query\Expr\Comparison('e.subject', 'like', '%NAM%');
     $likeExprDescription = new \Doctrine\ORM\Query\Expr\Comparison('e.description', 'like', '%NAM%');
     $whereExprOrx = new \Doctrine\ORM\Query\Expr\Orx($likeExprSubject, $likeExprDescription);
     $this->queryBuilder->expects($this->atLeastOnce())->method('andWhere')->with($this->logicalOr($this->equalTo($whereExpr), $this->equalTo($whereExprOrx)))->will($this->returnValue($this->queryBuilder));
     $this->queryBuilder->expects($this->once())->method('addOrderBy')->with($this->equalTo('e.id'), $this->equalTo($pagingConfig->getOrder()))->will($this->returnValue($this->queryBuilder));
     $this->queryBuilder->expects($this->once())->method('setFirstResult')->with($this->equalTo(0))->will($this->returnValue($this->queryBuilder));
     $this->queryBuilder->expects($this->once())->method('setMaxResults')->with($this->equalTo($pagingConfig->getLimit()))->will($this->returnValue($this->queryBuilder));
     $this->queryBuilder->expects($this->once())->method('getQuery')->will($this->returnValue($this->searchQuery));
     $this->queryBuilder->expects($this->any())->method('expr')->will($this->returnValue($this->expressionBuilder));
     $this->em->expects($this->any())->method('getExpressionBuilder')->will($this->returnValue($this->expressionBuilder));
     $this->expressionBuilder->expects($this->atLeast(2))->method('literal')->with($this->logicalOr($this->equalTo('new'), $this->equalTo('%' . $querySearch . '%')))->will($this->returnCallback(array($this, 'literalCallback')));
     $this->expressionBuilder->expects($this->atLeastOnce())->method('eq')->with($this->equalTo('e.status'), $this->equalTo('new'))->will($this->returnValue($whereExpr));
     $this->expressionBuilder->expects($this->atLeastOnce())->method('Orx')->with($this->anything(), $this->anything())->will($this->returnValue($whereExprOrx));
     $this->em->expects($this->atLeastOnce())->method('newHydrator')->with($this->equalTo(Query::HYDRATE_OBJECT))->will($this->returnValue($this->objectHydrator));
     $this->objectHydrator->expects($this->atLeastOnce())->method('hydrateAll')->will($this->returnValue(array($a)));
     $searchedEntities = $this->repository->search($querySearch, $conditions, $pagingConfig);
     $this->assertNotNull($searchedEntities);
     $this->assertTrue(is_array($searchedEntities));
     $this->assertNotEmpty($searchedEntities);
     $this->assertEquals($a, $searchedEntities[0]);
 }
 /**
  * @test
  */
 public function thatFilters()
 {
     $a = new EntityStub();
     $b = new EntityStub();
     $a->name = "DUMMY_NAME";
     $a->id = 1;
     $b->name = "NOT_DUMMY_NAME";
     $b->id = 2;
     $entities = array($a, $b);
     $pagingConfig = new FilterPagingProperties();
     $conditions = array(array('name', 'eq', 'DUMMY_NAME'));
     $whereExpr = new \Doctrine\ORM\Query\Expr\Comparison('e.name', 'eq', "'DUMMY_NAME'");
     $this->em->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($this->queryBuilder));
     $this->queryBuilder->expects($this->once())->method('select')->with($this->equalTo('e'))->will($this->returnValue($this->queryBuilder));
     $this->queryBuilder->expects($this->once())->method('from')->with($this->equalTo($this->classMetadata->name), $this->equalTo('e'))->will($this->returnValue($this->queryBuilder));
     $this->queryBuilder->expects($this->atLeastOnce())->method('andWhere')->with($this->equalTo($whereExpr))->will($this->returnValue($this->queryBuilder));
     $this->queryBuilder->expects($this->once())->method('addOrderBy')->with($this->equalTo('e.id'), $this->equalTo($pagingConfig->getOrder()))->will($this->returnValue($this->queryBuilder));
     $this->queryBuilder->expects($this->once())->method('setFirstResult')->with($this->equalTo(0))->will($this->returnValue($this->queryBuilder));
     $this->queryBuilder->expects($this->once())->method('setMaxResults')->with($this->equalTo($pagingConfig->getLimit()))->will($this->returnValue($this->queryBuilder));
     $this->queryBuilder->expects($this->once())->method('getQuery')->will($this->returnValue($this->filterQuery));
     $this->queryBuilder->expects($this->any())->method('expr')->will($this->returnValue($this->expressionBuilder));
     $this->em->expects($this->any())->method('getExpressionBuilder')->will($this->returnValue($this->expressionBuilder));
     $this->expressionBuilder->expects($this->atLeastOnce())->method('literal')->with($this->equalTo('DUMMY_NAME'))->will($this->returnValue("'DUMMY_NAME'"));
     $this->expressionBuilder->expects($this->atLeastOnce())->method('eq')->with($this->equalTo('e.name'), $this->equalTo("'DUMMY_NAME'"))->will($this->returnValue($whereExpr));
     $this->em->expects($this->atLeastOnce())->method('newHydrator')->with($this->equalTo(Query::HYDRATE_OBJECT))->will($this->returnValue($this->objectHydrator));
     $this->objectHydrator->expects($this->atLeastOnce())->method('hydrateAll')->will($this->returnValue(array($a)));
     $filteredEntities = $this->repository->filter($conditions, $pagingConfig);
     $this->assertNotNull($filteredEntities);
     $this->assertTrue(is_array($filteredEntities));
     $this->assertNotEmpty($filteredEntities);
     $this->assertEquals($a, $filteredEntities[0]);
 }