|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace BryanCrowe\ApiPagination\Test; |
| 5 | + |
| 6 | +use BryanCrowe\ApiPagination\Controller\Component\ApiPaginationComponent; |
| 7 | +use BryanCrowe\ApiPagination\TestApp\Controller\ArticlesIndexController; |
| 8 | +use Cake\Event\Event; |
| 9 | +use Cake\Http\ServerRequest as Request; |
| 10 | +use Cake\ORM\TableRegistry; |
| 11 | +use Cake\TestSuite\TestCase; |
| 12 | + |
| 13 | +/** |
| 14 | + * ApiPaginationComponentTest class |
| 15 | + * |
| 16 | + * @property ArticlesIndexController $controller |
| 17 | + */ |
| 18 | +class ApiPaginationComponentOnNonConventionalControllerNameTest extends TestCase |
| 19 | +{ |
| 20 | + public $fixtures = ['plugin.BryanCrowe/ApiPagination.Articles']; |
| 21 | + |
| 22 | + /** |
| 23 | + * setUp method |
| 24 | + * |
| 25 | + * @return void |
| 26 | + */ |
| 27 | + public function setUp(): void |
| 28 | + { |
| 29 | + $this->request = new Request(['url' => '/articles']); |
| 30 | + $this->response = $this->createMock('Cake\Http\Response'); |
| 31 | + $this->controller = new ArticlesIndexController($this->request, $this->response); |
| 32 | + $this->Articles = TableRegistry::getTableLocator()->get('BryanCrowe/ApiPagination.Articles', ['table' => 'bryancrowe_articles']); |
| 33 | + parent::setUp(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * tearDown method |
| 38 | + * |
| 39 | + * @return void |
| 40 | + */ |
| 41 | + public function tearDown(): void |
| 42 | + { |
| 43 | + parent::tearDown(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Test that a non conventional controller name is supported using the 'model' config. |
| 48 | + * |
| 49 | + * @dataProvider dataForTestVariousModelValueOnNonConventionalController |
| 50 | + * @param array $config |
| 51 | + * @param $expected |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + public function testVariousModelValueOnNonConventionalController(array $config, $expected) |
| 55 | + { |
| 56 | + $this->controller->setRequest( |
| 57 | + $this->controller->getRequest()->withEnv('HTTP_ACCEPT', 'application/json') |
| 58 | + ); |
| 59 | + $this->controller->set('data', $this->controller->paginate($this->Articles)); |
| 60 | + $apiPaginationComponent = new ApiPaginationComponent($this->controller->components(), $config); |
| 61 | + $event = new Event('Controller.beforeRender', $this->controller); |
| 62 | + $apiPaginationComponent->beforeRender($event); |
| 63 | + |
| 64 | + $result = $apiPaginationComponent->getController()->viewBuilder()->getVar('pagination'); |
| 65 | + $this->assertSame($expected, $result); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * If the name of the paginated model is not specified, the result of the pagination |
| 70 | + * on a controller not having the same name as the model fails. |
| 71 | + * |
| 72 | + * @return array[] |
| 73 | + */ |
| 74 | + public function dataForTestVariousModelValueOnNonConventionalController(): array |
| 75 | + { |
| 76 | + return [ |
| 77 | + |
| 78 | + [[], null], |
| 79 | + [['model' => 'Articles'], $this->getDefaultPagination()], |
| 80 | + [['model' => 'articles'], $this->getDefaultPagination()], |
| 81 | + [['model' => 'NonExistingModel'], null], |
| 82 | + ]; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns the standard pagination result. |
| 87 | + * |
| 88 | + * @return array |
| 89 | + */ |
| 90 | + private function getDefaultPagination(): array |
| 91 | + { |
| 92 | + return [ |
| 93 | + 'count' => 23, |
| 94 | + 'current' => 20, |
| 95 | + 'perPage' => 20, |
| 96 | + 'page' => 1, |
| 97 | + 'requestedPage' => 1, |
| 98 | + 'pageCount' => 2, |
| 99 | + 'start' => 1, |
| 100 | + 'end' => 20, |
| 101 | + 'prevPage' => false, |
| 102 | + 'nextPage' => true, |
| 103 | + 'sort' => null, |
| 104 | + 'direction' => null, |
| 105 | + 'sortDefault' => false, |
| 106 | + 'directionDefault' => false, |
| 107 | + 'completeSort' => [], |
| 108 | + 'limit' => null, |
| 109 | + 'scope' => null, |
| 110 | + 'finder' => 'all', |
| 111 | + ]; |
| 112 | + } |
| 113 | +} |
0 commit comments