|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Test\TestCase\Integration\Service\Action\Extension; |
|
13
|
|
|
|
|
14
|
|
|
use CakeDC\Api\TestSuite\IntegrationTestCase; |
|
15
|
|
|
use CakeDC\Api\Test\ConfigTrait; |
|
16
|
|
|
use CakeDC\Api\Test\FixturesTrait; |
|
17
|
|
|
use CakeDC\Api\Test\Settings; |
|
18
|
|
|
use Cake\Core\Configure; |
|
19
|
|
|
use Cake\ORM\TableRegistry; |
|
20
|
|
|
use Cake\Utility\Hash; |
|
21
|
|
|
|
|
22
|
|
|
class CursorPaginationExtensionTest extends IntegrationTestCase |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
use ConfigTrait; |
|
26
|
|
|
use FixturesTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* setUp |
|
30
|
|
|
* |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
|
|
public function setUp() |
|
34
|
|
|
{ |
|
35
|
|
|
parent::setUp(); |
|
36
|
|
|
Configure::write('App.fullBaseUrl', 'http://example.com'); |
|
37
|
|
|
$this->_tokenAccess(); |
|
38
|
|
|
$this->_loadDefaultExtensions('CakeDC/Api.CursorPaginate'); |
|
39
|
|
|
$this->getDefaultUser(Settings::USER1); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* tearDown |
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
46
|
|
|
*/ |
|
47
|
|
|
public function tearDown() |
|
48
|
|
|
{ |
|
49
|
|
|
parent::tearDown(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testListDefault() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->sendRequest('/articles', 'GET'); |
|
55
|
|
|
$result = $this->getJsonResponse(); |
|
56
|
|
|
$this->assertSuccess($result); |
|
57
|
|
|
$this->assertEquals(20, Hash::get($result, 'pagination.count')); |
|
58
|
|
|
$this->assertEquals(null, Hash::get($result, 'pagination.since_id')); |
|
59
|
|
|
$this->assertEquals(null, Hash::get($result, 'pagination.max_id')); |
|
60
|
|
|
$this->assertEquals('http://example.com/api/articles?since_id=15', Hash::get($result, 'pagination.links.prev')); |
|
61
|
|
|
$this->assertEquals('http://example.com/api/articles?max_id=1', Hash::get($result, 'pagination.links.next')); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testListWithConfigCount() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->_loadDefaultExtensions([ |
|
67
|
|
|
'CakeDC/Api.CursorPaginate' => [ |
|
68
|
|
|
'defaultCount' => 5 |
|
69
|
|
|
] |
|
70
|
|
|
], true); |
|
71
|
|
|
// Configure::write('Test.Api.Extension', [ |
|
72
|
|
|
// 'default' => [ |
|
73
|
|
|
// 'CakeDC/Api.CursorPaginate' => [ |
|
74
|
|
|
// 'defaultCount' => 5 |
|
75
|
|
|
// ] |
|
76
|
|
|
// ] |
|
77
|
|
|
// ]); |
|
78
|
|
|
|
|
79
|
|
|
$this->sendRequest('/articles', 'GET'); |
|
80
|
|
|
$result = $this->getJsonResponse(); |
|
81
|
|
|
$this->assertSuccess($result); |
|
82
|
|
|
$this->assertEquals(5, Hash::get($result, 'pagination.count')); |
|
83
|
|
|
$this->assertEquals(null, Hash::get($result, 'pagination.since_id')); |
|
84
|
|
|
$this->assertEquals(null, Hash::get($result, 'pagination.max_id')); |
|
85
|
|
|
$this->assertEquals('http://example.com/api/articles?since_id=15', Hash::get($result, 'pagination.links.prev')); |
|
86
|
|
|
$this->assertEquals('http://example.com/api/articles?max_id=11', Hash::get($result, 'pagination.links.next')); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testListWithCountAsGetParam() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->sendRequest('/articles', 'GET', ['count' => 5]); |
|
92
|
|
|
$result = $this->getJsonResponse(); |
|
93
|
|
|
$this->assertSuccess($result); |
|
94
|
|
|
$this->assertEquals(5, Hash::get($result, 'pagination.count')); |
|
95
|
|
|
$this->assertEquals(null, Hash::get($result, 'pagination.since_id')); |
|
96
|
|
|
$this->assertEquals(null, Hash::get($result, 'pagination.max_id')); |
|
97
|
|
|
$this->assertEquals('http://example.com/api/articles?since_id=15', Hash::get($result, 'pagination.links.prev')); |
|
98
|
|
|
$this->assertEquals('http://example.com/api/articles?max_id=11', Hash::get($result, 'pagination.links.next')); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testListWithMaxId() |
|
102
|
|
|
{ |
|
103
|
|
|
$this->sendRequest('/articles', 'GET', ['count' => 5, 'max_id' => 11]); |
|
104
|
|
|
$result = $this->getJsonResponse(); |
|
105
|
|
|
$this->assertSuccess($result); |
|
106
|
|
|
$this->assertEquals(5, Hash::get($result, 'pagination.count')); |
|
107
|
|
|
$this->assertEquals(null, Hash::get($result, 'pagination.since_id')); |
|
108
|
|
|
$this->assertEquals(11, Hash::get($result, 'pagination.max_id')); |
|
109
|
|
|
$this->assertEquals('http://example.com/api/articles?since_id=10', Hash::get($result, 'pagination.links.prev')); |
|
110
|
|
|
$this->assertEquals('http://example.com/api/articles?max_id=6', Hash::get($result, 'pagination.links.next')); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testListWithMaxIdWithNewDataAdded() |
|
114
|
|
|
{ |
|
115
|
|
|
$this->_addData(10); |
|
116
|
|
|
$this->sendRequest('/articles', 'GET', ['count' => 5, 'max_id' => 11]); |
|
117
|
|
|
$result = $this->getJsonResponse(); |
|
118
|
|
|
$this->assertSuccess($result); |
|
119
|
|
|
$this->assertEquals(5, Hash::get($result, 'pagination.count')); |
|
120
|
|
|
$this->assertEquals(null, Hash::get($result, 'pagination.since_id')); |
|
121
|
|
|
$this->assertEquals(11, Hash::get($result, 'pagination.max_id')); |
|
122
|
|
|
$this->assertEquals('http://example.com/api/articles?since_id=10', Hash::get($result, 'pagination.links.prev')); |
|
123
|
|
|
$this->assertEquals('http://example.com/api/articles?max_id=6', Hash::get($result, 'pagination.links.next')); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testListWithCountAsGetParamWithNewDataAdded() |
|
127
|
|
|
{ |
|
128
|
|
|
$this->_addData(10); |
|
129
|
|
|
$this->sendRequest('/articles', 'GET', ['count' => 5]); |
|
130
|
|
|
$result = $this->getJsonResponse(); |
|
131
|
|
|
$this->assertSuccess($result); |
|
132
|
|
|
$this->assertEquals(5, Hash::get($result, 'pagination.count')); |
|
133
|
|
|
$this->assertEquals(null, Hash::get($result, 'pagination.since_id')); |
|
134
|
|
|
$this->assertEquals(null, Hash::get($result, 'pagination.max_id')); |
|
135
|
|
|
$this->assertEquals('http://example.com/api/articles?since_id=25', Hash::get($result, 'pagination.links.prev')); |
|
136
|
|
|
$this->assertEquals('http://example.com/api/articles?max_id=21', Hash::get($result, 'pagination.links.next')); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function testListWithSinceId() |
|
140
|
|
|
{ |
|
141
|
|
|
$this->sendRequest('/articles', 'GET', ['count' => 5, 'since_id' => 15]); |
|
142
|
|
|
$result = $this->getJsonResponse(); |
|
143
|
|
|
$this->assertSuccess($result); |
|
144
|
|
|
$this->assertEquals(5, Hash::get($result, 'pagination.count')); |
|
145
|
|
|
$this->assertEquals(15, Hash::get($result, 'pagination.since_id')); |
|
146
|
|
|
$this->assertEquals(null, Hash::get($result, 'pagination.max_id')); |
|
147
|
|
|
$this->assertEquals('http://example.com/api/articles?since_id=15', Hash::get($result, 'pagination.links.prev')); |
|
148
|
|
|
$this->assertEquals('http://example.com/api/articles', Hash::get($result, 'pagination.links.next')); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function testListWithSinceIdWithNewDataAdded() |
|
152
|
|
|
{ |
|
153
|
|
|
$this->_addData(10); |
|
154
|
|
|
$this->sendRequest('/articles', 'GET', ['count' => 5, 'since_id' => 15]); |
|
155
|
|
|
$result = $this->getJsonResponse(); |
|
156
|
|
|
$this->assertSuccess($result); |
|
157
|
|
|
$this->assertEquals(5, Hash::get($result, 'pagination.count')); |
|
158
|
|
|
$this->assertEquals(15, Hash::get($result, 'pagination.since_id')); |
|
159
|
|
|
$this->assertEquals(null, Hash::get($result, 'pagination.max_id')); |
|
160
|
|
|
$this->assertEquals('http://example.com/api/articles?since_id=20', Hash::get($result, 'pagination.links.prev')); |
|
161
|
|
|
$this->assertEquals('http://example.com/api/articles?max_id=16', Hash::get($result, 'pagination.links.next')); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param $count |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function _addData($count) |
|
168
|
|
|
{ |
|
169
|
|
|
$Article = TableRegistry::get('Articles'); |
|
|
|
|
|
|
170
|
|
|
$Article->createRecords($count, 1); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.