Paginator = new SimplePaginator(); $this->Post = $this->getMockRepository(); } /** * test paginate() and custom find, to make sure the correct count is returned. * * @return void */ public function testPaginateCustomFind() { $this->loadFixtures('Posts'); $titleExtractor = function ($result) { $ids = []; foreach ($result as $record) { $ids[] = $record->title; } return $ids; }; $table = $this->getTableLocator()->get('PaginatorPosts'); $data = ['author_id' => 3, 'title' => 'Fourth Post', 'body' => 'Article Body, unpublished', 'published' => 'N']; $result = $table->save(new Entity($data)); $this->assertNotEmpty($result); $result = $this->Paginator->paginate($table); $this->assertCount(4, $result, '4 rows should come back'); $this->assertEquals(['First Post', 'Second Post', 'Third Post', 'Fourth Post'], $titleExtractor($result)); $pagingParams = $this->Paginator->getPagingParams(); $this->assertEquals(4, $pagingParams['PaginatorPosts']['current']); $this->assertNull($pagingParams['PaginatorPosts']['count']); $settings = ['finder' => 'published']; $result = $this->Paginator->paginate($table, [], $settings); $this->assertCount(3, $result, '3 rows should come back'); $this->assertEquals(['First Post', 'Second Post', 'Third Post'], $titleExtractor($result)); $pagingParams = $this->Paginator->getPagingParams(); $this->assertEquals(3, $pagingParams['PaginatorPosts']['current']); $this->assertNull($pagingParams['PaginatorPosts']['count']); $settings = ['finder' => 'published', 'limit' => 2, 'page' => 2]; $result = $this->Paginator->paginate($table, [], $settings); $this->assertCount(1, $result, '1 rows should come back'); $this->assertEquals(['Third Post'], $titleExtractor($result)); $pagingParams = $this->Paginator->getPagingParams(); $this->assertEquals(1, $pagingParams['PaginatorPosts']['current']); $this->assertNull($pagingParams['PaginatorPosts']['count']); $this->assertSame(0, $pagingParams['PaginatorPosts']['pageCount']); $settings = ['finder' => 'published', 'limit' => 2]; $result = $this->Paginator->paginate($table, [], $settings); $this->assertCount(2, $result, '2 rows should come back'); $this->assertEquals(['First Post', 'Second Post'], $titleExtractor($result)); $pagingParams = $this->Paginator->getPagingParams(); $this->assertEquals(2, $pagingParams['PaginatorPosts']['current']); $this->assertNull($pagingParams['PaginatorPosts']['count']); $this->assertEquals(0, $pagingParams['PaginatorPosts']['pageCount']); $this->assertTrue($pagingParams['PaginatorPosts']['nextPage']); $this->assertFalse($pagingParams['PaginatorPosts']['prevPage']); $this->assertEquals(2, $pagingParams['PaginatorPosts']['perPage']); $this->assertNull($pagingParams['PaginatorPosts']['limit']); } /** * test paginate() and custom find with fields array, to make sure the correct count is returned. * * @return void */ public function testPaginateCustomFindFieldsArray() { $this->loadFixtures('Posts'); $table = $this->getTableLocator()->get('PaginatorPosts'); $data = ['author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N']; $table->save(new Entity($data)); $settings = [ 'finder' => 'list', 'conditions' => ['PaginatorPosts.published' => 'Y'], 'limit' => 2, ]; $results = $this->Paginator->paginate($table, [], $settings); $result = $results->toArray(); $expected = [ 1 => 'First Post', 2 => 'Second Post', ]; $this->assertEquals($expected, $result); $result = $this->Paginator->getPagingParams()['PaginatorPosts']; $this->assertEquals(2, $result['current']); $this->assertNull($result['count']); $this->assertEquals(0, $result['pageCount']); $this->assertTrue($result['nextPage']); $this->assertFalse($result['prevPage']); } /** * Test that special paginate types are called and that the type param doesn't leak out into defaults or options. * * @return void */ public function testPaginateCustomFinder() { $settings = [ 'PaginatorPosts' => [ 'finder' => 'published', 'fields' => ['id', 'title'], 'maxLimit' => 10, ], ]; $this->loadFixtures('Posts'); $table = $this->getTableLocator()->get('PaginatorPosts'); $table->updateAll(['published' => 'N'], ['id' => 2]); $this->Paginator->paginate($table, [], $settings); $pagingParams = $this->Paginator->getPagingParams(); $this->assertSame('published', $pagingParams['PaginatorPosts']['finder']); $this->assertSame(1, $pagingParams['PaginatorPosts']['start']); $this->assertSame(2, $pagingParams['PaginatorPosts']['end']); // nextPage will be always true for SimplePaginator $this->assertTrue($pagingParams['PaginatorPosts']['nextPage']); } }