|
| 1 | +<?php |
| 2 | + |
| 3 | +use Elastica\Query; |
| 4 | +use Elastica\Query\Term; |
| 5 | +use Elastica\Result; |
| 6 | +use Elastica\Type; |
| 7 | +use Knp\Component\Pager\Paginator; |
| 8 | +use Knp\Component\Pager\Event\Subscriber\Paginate\ElasticaQuerySubscriber; |
| 9 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 10 | +use Test\Mock\PaginationSubscriber as MockPaginationSubscriber; |
| 11 | +use Test\Tool\BaseTestCase; |
| 12 | + |
| 13 | +class ElasticaTest extends BaseTestCase |
| 14 | +{ |
| 15 | + public function testElasticaSubscriber() |
| 16 | + { |
| 17 | + $dispatcher = new EventDispatcher; |
| 18 | + $dispatcher->addSubscriber(new ElasticaQuerySubscriber()); |
| 19 | + $dispatcher->addSubscriber(new MockPaginationSubscriber); // pagination view |
| 20 | + $p = new Paginator($dispatcher); |
| 21 | + |
| 22 | + $query = Query::create(new Term(array( |
| 23 | + 'name' => 'Fred', |
| 24 | + ))); |
| 25 | + $response = $this->getMockBuilder('Elastica\\ResultSet')->disableOriginalConstructor()->getMock(); |
| 26 | + $response->expects($this->once()) |
| 27 | + ->method('getTotalHits') |
| 28 | + ->will($this->returnValue(2)); |
| 29 | + $response->expects($this->once()) |
| 30 | + ->method('getResults') |
| 31 | + ->will($this->returnValue(array(new Result(array()), new Result(array())))); |
| 32 | + $searchable = $this->getMockBuilder('Elastica\\SearchableInterface')->getMock(); |
| 33 | + $searchable->expects($this->once()) |
| 34 | + ->method('search') |
| 35 | + ->with($query) |
| 36 | + ->will($this->returnValue($response)); |
| 37 | + |
| 38 | + $view = $p->paginate(array($searchable, $query), 1, 10); |
| 39 | + |
| 40 | + $this->assertEquals(0, $query->getParam('from'), 'Query offset set correctly'); |
| 41 | + $this->assertEquals(10, $query->getParam('size'), 'Query limit set correctly'); |
| 42 | + $this->assertSame($response, $view->getCustomParameter('resultSet'), 'Elastica ResultSet available in Paginator'); |
| 43 | + |
| 44 | + $this->assertEquals(1, $view->getCurrentPageNumber()); |
| 45 | + $this->assertEquals(10, $view->getItemNumberPerPage()); |
| 46 | + $this->assertEquals(2, count($view->getItems())); |
| 47 | + $this->assertEquals(2, $view->getTotalItemCount()); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @test |
| 52 | + */ |
| 53 | + function shouldSlicePaginateAnArray() |
| 54 | + { |
| 55 | + /*$dispatcher = new EventDispatcher; |
| 56 | + $dispatcher->addSubscriber(new ArraySubscriber); |
| 57 | + $dispatcher->addSubscriber(new MockPaginationSubscriber); // pagination view |
| 58 | + $p = new Paginator($dispatcher); |
| 59 | +
|
| 60 | + $items = range('a', 'u'); |
| 61 | + $view = $p->paginate($items, 2, 10); |
| 62 | +
|
| 63 | + $this->assertEquals(2, $view->getCurrentPageNumber()); |
| 64 | + $this->assertEquals(10, $view->getItemNumberPerPage()); |
| 65 | + $this->assertEquals(10, count($view->getItems())); |
| 66 | + $this->assertEquals(21, $view->getTotalItemCount());*/ |
| 67 | + } |
| 68 | +} |
0 commit comments