SortExtensionTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

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

7 Methods

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