PaginationExtensionTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A tearDown() 0 4 1
A testDefault() 0 13 1
A testLimitDefault() 0 8 1
A testCustomLimit() 0 14 1
A testCustomLimitAndPage() 0 14 1
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 PaginationExtensionTest extends IntegrationTestCase
22
{
23
24
    use ConfigTrait;
25
    use FixturesTrait;
26
27
    /**
28
     * setUp
29
     *
30
     * @return void
31
     */
32
    public function setUp()
33
    {
34
        parent::setUp();
35
        Configure::write('App.fullBaseUrl', 'http://example.com');
36
        $this->_tokenAccess();
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
    }
50
51
    public function testDefault()
52
    {
53
        $this->sendRequest('/articles', 'GET');
54
        $result = $this->getJsonResponse();
55
        $expected = [
56
            'page' => 1,
57
            'limit' => 20,
58
            'pages' => 1,
59
            'count' => 15
60
        ];
61
        $this->assertSuccess($result);
62
        $this->assertEquals($expected, $result['pagination']);
63
    }
64
65
    public function testLimitDefault()
66
    {
67
        $this->sendRequest('/articles', 'GET');
68
        $result = $this->getJsonResponse();
69
        $this->assertSuccess($result);
70
        $this->assertEquals(20, $result['pagination']['limit']);
71
        $this->assertEquals(range(1, 15), Hash::extract($result, 'data.{n}.id'));
72
    }
73
74
    public function testCustomLimit()
75
    {
76
        $this->sendRequest('/articles', 'GET', ['limit' => 4]);
77
        $result = $this->getJsonResponse();
78
        $expected = [
79
            'page' => 1,
80
            'limit' => 4,
81
            'pages' => 4,
82
            'count' => 15
83
        ];
84
        $this->assertSuccess($result);
85
        $this->assertEquals($expected, $result['pagination']);
86
        $this->assertEquals(range(1, 4), Hash::extract($result, 'data.{n}.id'));
87
    }
88
89
    public function testCustomLimitAndPage()
90
    {
91
        $this->sendRequest('/articles', 'GET', ['limit' => 4, 'page' => 2]);
92
        $result = $this->getJsonResponse();
93
        $expected = [
94
            'page' => 2,
95
            'limit' => 4,
96
            'pages' => 4,
97
            'count' => 15
98
        ];
99
        $this->assertSuccess($result);
100
        $this->assertEquals($expected, $result['pagination']);
101
        $this->assertEquals(range(5, 8), Hash::extract($result, 'data.{n}.id'));
102
    }
103
}
104