|
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\Utility\Hash; |
|
20
|
|
|
|
|
21
|
|
|
class ExtendedSortExtensionTest extends IntegrationTestCase |
|
22
|
|
|
{ |
|
23
|
|
|
use ConfigTrait; |
|
24
|
|
|
use FixturesTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* setUp |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public function setUp() |
|
32
|
|
|
{ |
|
33
|
|
|
parent::setUp(); |
|
34
|
|
|
Configure::write('App.fullBaseUrl', 'http://example.com'); |
|
35
|
|
|
$this->_tokenAccess(); |
|
36
|
|
|
$this->_loadDefaultExtensions('CakeDC/Api.ExtendedSort'); |
|
37
|
|
|
$this->_loadDefaultExtensions('CakeDC/Api.Paginate'); |
|
38
|
|
|
$this->getDefaultUser(Settings::USER1); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* tearDown |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
public function tearDown() |
|
47
|
|
|
{ |
|
48
|
|
|
parent::tearDown(); |
|
49
|
|
|
Configure::write('Test.Api.Extension', null); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testDefault() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->sendRequest('/articles', 'GET'); |
|
55
|
|
|
$result = $this->getJsonResponse(); |
|
56
|
|
|
$expected = [ |
|
57
|
|
|
'page' => 1, |
|
58
|
|
|
'limit' => 20, |
|
59
|
|
|
'pages' => 1, |
|
60
|
|
|
'count' => 15 |
|
61
|
|
|
]; |
|
62
|
|
|
$this->assertSuccess($result); |
|
63
|
|
|
$this->assertEquals($expected, $result['pagination']); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testSortById() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->sendRequest('/authors', 'GET', ['limit' => 4, 'sort' => json_encode(['id' => 'asc'])]); |
|
69
|
|
|
$result = $this->getJsonResponse(); |
|
70
|
|
|
$this->assertSuccess($result); |
|
71
|
|
|
$this->assertEquals(range(1, 4), Hash::extract($result, 'data.{n}.id')); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testSortByIdDesc() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->sendRequest('/authors', 'GET', ['limit' => 4, 'sort' => json_encode(['id' => 'desc'])]); |
|
77
|
|
|
$result = $this->getJsonResponse(); |
|
78
|
|
|
$this->assertSuccess($result); |
|
79
|
|
|
$this->assertEquals(array_reverse(range(12, 15)), Hash::extract($result, 'data.{n}.id')); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testSortByName() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->sendRequest('/authors', 'GET', ['limit' => 4, 'sort' => json_encode(['first_name' => 'asc'])]); |
|
85
|
|
|
$result = $this->getJsonResponse(); |
|
86
|
|
|
$this->assertSuccess($result); |
|
87
|
|
|
$this->assertEquals([7, 5, 11, 15], Hash::extract($result, 'data.{n}.id')); |
|
88
|
|
|
|
|
89
|
|
|
$this->sendRequest('/authors', 'GET', ['limit' => 4, 'sort' => json_encode(['first_name' => 'asc', 'last_name' => 'asc'])]); |
|
90
|
|
|
$result = $this->getJsonResponse(); |
|
91
|
|
|
$this->assertSuccess($result); |
|
92
|
|
|
$this->assertEquals([7, 5, 11, 15], Hash::extract($result, 'data.{n}.id')); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testSortByFirstNameDesc() |
|
96
|
|
|
{ |
|
97
|
|
|
$this->sendRequest('/authors', 'GET', ['limit' => 4, 'sort' => json_encode(['first_name' => 'desc'])]); |
|
98
|
|
|
$result = $this->getJsonResponse(); |
|
99
|
|
|
$this->assertSuccess($result); |
|
100
|
|
|
$this->assertEquals([4, 9, 14, 10], Hash::extract($result, 'data.{n}.id')); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|