CrudHateoasExtensionTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A tearDown() 0 5 1
A testView() 0 33 1
A testIndex() 0 21 1
A testViewNested() 0 39 1
A testIndexNested() 0 27 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 CrudHateoasExtensionTest
23
 *
24
 * @package CakeDC\Api\Test\TestCase\Integration\Service\Extension
25
 */
26
class CrudHateoasExtensionTest extends IntegrationTestCase
27
{
28
29
    use ConfigTrait;
30
    use FixturesTrait;
31
32
    /**
33
     * setUp
34
     *
35
     * @return void
36
     */
37
    public function setUp()
38
    {
39
        parent::setUp();
40
        Configure::write('App.fullBaseUrl', 'http://example.com');
41
        $this->_tokenAccess();
42
        $this->_loadDefaultExtensions('CakeDC/Api.CrudHateoas');
43
        $this->_loadDefaultExtensions('CakeDC/Api.Paginate');
44
        $this->getDefaultUser(Settings::USER1);
45
    }
46
47
    /**
48
     * tearDown
49
     *
50
     * @return void
51
     */
52
    public function tearDown()
53
    {
54
        parent::tearDown();
55
        Configure::write('Test.Api.Extension', null);
56
    }
57
58
    public function testView()
59
    {
60
        $this->sendRequest('/articles/1', 'GET', []);
61
        $result = $this->getJsonResponse();
62
        $this->assertSuccess($result);
63
        $links = [
64
            [
65
                'name' => 'self',
66
                'href' => 'http://example.com/api/articles/1',
67
                'rel' => '/api/articles/1',
68
                'method' => 'GET'
69
            ],
70
            [
71
                'name' => 'articles:edit',
72
                'href' => 'http://example.com/api/articles/1',
73
                'rel' => '/api/articles/1',
74
                'method' => 'PUT'
75
            ],
76
            [
77
                'name' => 'articles:delete',
78
                'href' => 'http://example.com/api/articles/1',
79
                'rel' => '/api/articles/1',
80
                'method' => 'DELETE'
81
            ],
82
            [
83
                'name' => 'articles:index',
84
                'href' => 'http://example.com/api/articles',
85
                'rel' => '/api/articles',
86
                'method' => 'GET'
87
            ]
88
        ];
89
        $this->assertEquals($links, Hash::get($result, 'links'));
90
    }
91
92
    public function testIndex()
93
    {
94
        $this->sendRequest('/articles', 'GET', ['limit' => 4]);
95
        $result = $this->getJsonResponse();
96
        $this->assertSuccess($result);
97
        $links = [
98
            [
99
                'name' => 'self',
100
                'href' => 'http://example.com/api/articles',
101
                'rel' => '/api/articles',
102
                'method' => 'GET'
103
            ],
104
            [
105
                'name' => 'articles:add',
106
                'href' => 'http://example.com/api/articles',
107
                'rel' => '/api/articles',
108
                'method' => 'POST'
109
            ],
110
        ];
111
        $this->assertEquals($links, Hash::get($result, 'links'));
112
    }
113
114
    public function testViewNested()
115
    {
116
        $this->sendRequest('/authors/1/articles/1', 'GET', []);
117
        $result = $this->getJsonResponse();
118
        $this->assertSuccess($result);
119
        $links = [
120
            [
121
                'name' => 'self',
122
                'href' => 'http://example.com/api/authors/1/articles/1',
123
                'rel' => '/api/authors/1/articles/1',
124
                'method' => 'GET'
125
            ],
126
            [
127
                'name' => 'articles:edit',
128
                'href' => 'http://example.com/api/authors/1/articles/1',
129
                'rel' => '/api/authors/1/articles/1',
130
                'method' => 'PUT'
131
            ],
132
            [
133
                'name' => 'articles:delete',
134
                'href' => 'http://example.com/api/authors/1/articles/1',
135
                'rel' => '/api/authors/1/articles/1',
136
                'method' => 'DELETE'
137
            ],
138
            [
139
                'name' => 'articles:index',
140
                'href' => 'http://example.com/api/authors/1/articles',
141
                'rel' => '/api/authors/1/articles',
142
                'method' => 'GET'
143
            ],
144
            [
145
                'name' => 'authors:view',
146
                'href' => 'http://example.com/api/authors/1',
147
                'rel' => '/api/authors/1',
148
                'method' => 'GET'
149
            ]
150
        ];
151
        $this->assertEquals($links, Hash::get($result, 'links'));
152
    }
153
154
    public function testIndexNested()
155
    {
156
        $this->sendRequest('/authors/1/articles', 'GET', ['limit' => 4]);
157
        $result = $this->getJsonResponse();
158
        $this->assertSuccess($result);
159
        $links = [
160
            [
161
                'name' => 'self',
162
                'href' => 'http://example.com/api/authors/1/articles',
163
                'rel' => '/api/authors/1/articles',
164
                'method' => 'GET'
165
            ],
166
            [
167
                'name' => 'articles:add',
168
                'href' => 'http://example.com/api/authors/1/articles',
169
                'rel' => '/api/authors/1/articles',
170
                'method' => 'POST'
171
            ],
172
            [
173
                'name' => 'authors:view',
174
                'href' => 'http://example.com/api/authors/1',
175
                'rel' => '/api/authors/1',
176
                'method' => 'GET'
177
            ]
178
        ];
179
        $this->assertEquals($links, Hash::get($result, 'links'));
180
    }
181
}
182