public function testSearch() { $result = ['list' => ['foo', 'bar'], 'total' => 123]; // exec query $select = $this->getMockBuilder('\\Doctrine\\ORM\\QueryBuilder')->disableOriginalConstructor()->getMock(); $total = $this->getMockBuilder('\\Doctrine\\ORM\\QueryBuilder')->disableOriginalConstructor()->getMock(); $select->expects($this->once())->method('getQuery')->will($this->returnValue($this->getQuery($result['list']))); $total->expects($this->once())->method('getQuery')->will($this->returnValue($this->getQuery($result['total'], true))); /* @var $entity \PHPUnit_Framework_MockObject_MockObject|Search */ $entity = $this->getMock('\\AnimeDb\\Bundle\\CatalogBundle\\Entity\\Search'); // build query selector $builder = $this->getMockBuilder('\\AnimeDb\\Bundle\\CatalogBundle\\Service\\Item\\Search\\Selector\\Builder')->disableOriginalConstructor()->getMock(); $methods = ['addCountry', 'addDateAdd', 'addDateEnd', 'addDatePremiere', 'addGenres', 'addLabels', 'addName', 'addStorage', 'addStudio', 'addType']; foreach ($methods as $method) { $builder->expects($this->once())->method($method)->with($entity)->will($this->returnSelf()); } $builder->expects($this->once())->method('sort')->with('my_column', 'my_direction')->will($this->returnSelf()); $builder->expects($this->once())->method('limit')->with(111)->will($this->returnSelf()); $builder->expects($this->once())->method('offset')->with(222)->will($this->returnSelf()); $builder->expects($this->once())->method('getQuerySelect')->will($this->returnValue($select)); $builder->expects($this->once())->method('getQueryTotal')->will($this->returnValue($total)); $this->selector->expects($this->once())->method('create')->will($this->returnValue($builder)); // TODO add mocks $this->assertEquals($result, $this->driver->search($entity, 111, 222, 'my_column', 'my_direction')); }