Skip to content

Commit d15c05d

Browse files
committed
Test default pagination response setting
1 parent bd50c23 commit d15c05d

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

phpunit.xml.dist

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
2+
<phpunit bootstrap="./tests/bootstrap.php"
33
backupGlobals="false"
44
backupStaticAttributes="false"
55
colors="true"
@@ -14,6 +14,15 @@
1414
<directory>tests</directory>
1515
</testsuite>
1616
</testsuites>
17+
<listeners>
18+
<listener
19+
class="\Cake\TestSuite\Fixture\FixtureInjector"
20+
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
21+
<arguments>
22+
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
23+
</arguments>
24+
</listener>
25+
</listeners>
1726
<filter>
1827
<whitelist>
1928
<directory suffix=".php">src/</directory>

tests/ApiPaginationComponentTest.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,36 @@
55
use Cake\Controller\ComponentRegistry;
66
use Cake\Controller\Controller;
77
use Cake\Core\Plugin;
8+
use Cake\Datasource\ConnectionManager;
89
use Cake\Event\Event;
910
use Cake\Network\Request;
1011
use Cake\Network\Response;
12+
use Cake\Routing\DispatcherFactory;
13+
use Cake\Routing\Router;
14+
use Cake\ORM\TableRegistry;
1115
use Cake\TestSuite\TestCase;
1216

17+
18+
class ArticlesController extends Controller
19+
{
20+
public $components = ['Paginator'];
21+
}
22+
1323
/**
1424
* ApiPaginationComponentTest class
1525
*/
1626
class ApiPaginationComponentTest extends TestCase
1727
{
28+
public $fixtures = ['plugin.BryanCrowe/ApiPagination.Articles'];
29+
1830
/**
1931
* setUp method
2032
*
2133
* @return void
2234
*/
2335
public function setUp()
2436
{
37+
$this->Articles = TableRegistry::get('BryanCrowe/ApiPagination.Articles', ['table' => 'bryancrowe_articles']);
2538
parent::setUp();
2639
}
2740

@@ -37,12 +50,43 @@ public function tearDown()
3750

3851
public function testNonApiPaginatedRequest()
3952
{
40-
$request = new Request('/');
53+
$request = new Request('/articles');
4154
$response = $this->getMock('Cake\Network\Response');
42-
$controller = new Controller($request, $response);
55+
$controller = new ArticlesController($request, $response);
4356
$apiPaginationComponent = new ApiPaginationComponent($controller->components());
4457
$event = new Event('Controller.beforeRender', $controller);
4558

4659
$this->assertNull($apiPaginationComponent->beforeRender($event));
4760
}
61+
62+
public function testDefaultPaginationSettings()
63+
{
64+
$request = new Request('/articles');
65+
$request->env('HTTP_ACCEPT', 'application/json');
66+
$response = $this->getMock('Cake\Network\Response');
67+
$controller = new ArticlesController($request, $response);
68+
$controller->set('data', $controller->paginate($this->Articles));
69+
$apiPaginationComponent = new ApiPaginationComponent($controller->components());
70+
$event = new Event('Controller.beforeRender', $controller);
71+
$apiPaginationComponent->beforeRender($event);
72+
73+
$result = $apiPaginationComponent->_registry->getController()->viewVars['pagination'];
74+
$expected = [
75+
'finder' => 'all',
76+
'page' => 1,
77+
'current' => 20,
78+
'count' => 23,
79+
'perPage' => 20,
80+
'prevPage' => false,
81+
'nextPage' => true,
82+
'pageCount' => 2,
83+
'sort' => null,
84+
'direction' => false,
85+
'limit' => null,
86+
'sortDefault' => false,
87+
'directionDefault' => false
88+
];
89+
90+
$this->assertSame($expected, $result);
91+
}
4892
}

tests/Fixture/ArticlesFixture.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace BryanCrowe\ApiPagination\Test\Fixture;
3+
4+
use Cake\TestSuite\Fixture\TestFixture;
5+
6+
class ArticlesFixture extends TestFixture
7+
{
8+
public $table = 'bryancrowe_articles';
9+
10+
public $fields = [
11+
'id' => ['type' => 'integer'],
12+
'title' => ['type' => 'string', 'null' => false],
13+
'body' => 'text',
14+
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
15+
];
16+
17+
public $records = [
18+
['title' => 'Post #1', 'body' => 'This is the article body.'],
19+
['title' => 'Post #2', 'body' => 'This is the article body.'],
20+
['title' => 'Post #3', 'body' => 'This is the article body.'],
21+
['title' => 'Post #4', 'body' => 'This is the article body.'],
22+
['title' => 'Post #5', 'body' => 'This is the article body.'],
23+
['title' => 'Post #6', 'body' => 'This is the article body.'],
24+
['title' => 'Post #7', 'body' => 'This is the article body.'],
25+
['title' => 'Post #8', 'body' => 'This is the article body.'],
26+
['title' => 'Post #9', 'body' => 'This is the article body.'],
27+
['title' => 'Post #10', 'body' => 'This is the article body.'],
28+
['title' => 'Post #11', 'body' => 'This is the article body.'],
29+
['title' => 'Post #12', 'body' => 'This is the article body.'],
30+
['title' => 'Post #13', 'body' => 'This is the article body.'],
31+
['title' => 'Post #14', 'body' => 'This is the article body.'],
32+
['title' => 'Post #15', 'body' => 'This is the article body.'],
33+
['title' => 'Post #16', 'body' => 'This is the article body.'],
34+
['title' => 'Post #17', 'body' => 'This is the article body.'],
35+
['title' => 'Post #18', 'body' => 'This is the article body.'],
36+
['title' => 'Post #19', 'body' => 'This is the article body.'],
37+
['title' => 'Post #20', 'body' => 'This is the article body.'],
38+
['title' => 'Post #21', 'body' => 'This is the article body.'],
39+
['title' => 'Post #22', 'body' => 'This is the article body.'],
40+
['title' => 'Post #23', 'body' => 'This is the article body.']
41+
];
42+
}

tests/bootstrap.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
$findRoot = function ($root) {
3+
do {
4+
$lastRoot = $root;
5+
$root = dirname($root);
6+
if (is_dir($root . '/vendor/cakephp/cakephp')) {
7+
return $root;
8+
}
9+
} while ($root !== $lastRoot);
10+
throw new Exception("Cannot find the root of the application, unable to run tests");
11+
};
12+
$root = $findRoot(__FILE__);
13+
unset($findRoot);
14+
chdir($root);
15+
if (file_exists($root . '/config/bootstrap.php')) {
16+
require $root . '/config/bootstrap.php';
17+
return;
18+
}
19+
require $root . '/vendor/cakephp/cakephp/tests/bootstrap.php';

0 commit comments

Comments
 (0)