Passed
Push — master ( e5acb2...2b13b1 )
by Evgeny
07:45
created

IntegrationTestCase::getDefaultUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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\TestSuite;
13
14
use CakeDC\Api\Service\ServiceRegistry;
15
use Cake\Core\Configure;
16
use Cake\Datasource\EntityInterface;
17
use Cake\ORM\TableRegistry;
18
use Cake\TestSuite\IntegrationTestCase as BaseTestCase;
19
use Cake\Utility\Hash;
20
21
/**
22
 * Class IntegrationTestCase
23
 *
24
 * @package CakeDC\Api\TestSuite
25
 */
26
class IntegrationTestCase extends BaseTestCase
27
{
28
29
    /**
30
     * @var string|int Current logged in user
31
     */
32
    protected $_defaultUserId;
33
34
    /**
35
     * setUp
36
     *
37
     * @return void
38
     */
39 56
    public function setUp()
40
    {
41 56
        parent::setUp();
42 56
        Configure::write('Api', []);
43 56
    }
44
45
    /**
46
     * tearDown
47
     *
48
     * @return void
49
     */
50 56
    public function tearDown()
51
    {
52 56
        parent::tearDown();
53 56
        ServiceRegistry::clear();
0 ignored issues
show
Deprecated Code introduced by
The method CakeDC\Api\Service\ServiceRegistry::clear() has been deprecated with message: 3.6.0 Use \CakeDC\Api\Service\Locator\ServiceLocator::clear() instead.

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.

Loading history...
54 56
    }
55
56
    /**
57
     * Default user api method.
58
     *
59
     * @param string $userId User id.
60
     * @return string
61
     */
62 56
    public function getDefaultUser($userId = null)
63
    {
64 56
        if ($userId === null) {
65 56
            $userId = $this->_defaultUserId;
66 56
        } else {
67 47
            $this->_defaultUserId = $userId;
68
        }
69
70 56
        return $userId;
71
    }
72
73
    /**
74
     * Returns user token.
75
     *
76
     * @param string $userId User id.
77
     * @return mixed|null
78
     */
79 56
    protected function _userToken($userId = null)
80
    {
81 56
        if ($userId === null) {
82 56
            $userId = $this->getDefaultUser();
83 56
        }
84 56
        $Users = TableRegistry::get('CakeDC/Users.Users');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\TableRegistry::get() has been deprecated with message: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead.

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.

Loading history...
85 56
        $user = $Users->find()->where(['id' => $userId])->first();
86 56
        if ($user instanceof EntityInterface) {
87 47
            return $user['api_token'];
88
        }
89
90 9
        return null;
91
    }
92
93
    /**
94
     * Send api request.
95
     *
96
     * @param string $url Url.
97
     * @param string $method HTTP method.
98
     * @param array $data Api parameters.
99
     * @param string $userId Current user id.
100
     * @return void
101
     */
102 56
    public function sendRequest($url, $method, $data = [], $userId = null)
103
    {
104 56
        ServiceRegistry::clear();
0 ignored issues
show
Deprecated Code introduced by
The method CakeDC\Api\Service\ServiceRegistry::clear() has been deprecated with message: 3.6.0 Use \CakeDC\Api\Service\Locator\ServiceLocator::clear() instead.

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.

Loading history...
105 56
        $userToken = $this->_userToken($userId);
106
107 56
        Configure::load('api');
108
109 56
        if (!is_string($url)) {
110
            $this->_sendRequest($url, $method, $data);
111
112
            return;
113
        }
114 56
        $url = '/api' . $url;
115 56
        if (is_string($url)) {
116 56
            if ($userToken !== null) {
117 47
                $url = $this->_appendGetParam($url, 'token', $userToken);
118 47
            }
119 56
        }
120 56
        if ($method == 'GET' && is_string($url)) {
121 47
            if (!empty($data)) {
122 27
                foreach ($data as $key => $value) {
123 27
                    if (!is_array($value)) {
124 27
                        $url = $this->_appendGetParam($url, $key, $value);
125 27
                    }
126 27
                }
127 27
            }
128 47
        }
129 56
        $this->useHttpServer(true);
130 56
        $this->_sendRequest($url, $method, $data);
131 56
    }
132
133
    /**
134
     * Add param to request.
135
     *
136
     * @param string $url Url.
137
     * @param string $key Param name.
138
     * @param string $value Param value.
139
     * @return string
140
     */
141 48
    protected function _appendGetParam($url, $key, $value)
142
    {
143 48
        if (strpos($url, '?') !== false) {
144 26
            $appendChar = '&';
145 26
        } else {
146 48
            $appendChar = '?';
147
        }
148
149 48
        return $url . $appendChar . urlencode($key) . '=' . urlencode($value);
150
    }
151
152
    /**
153
     * Assert result is success.
154
     *
155
     * @param array $result Response.
156
     * @return void
157
     */
158 52
    public function assertSuccess($result)
159
    {
160 52
        $this->assertTrue(is_array($result));
161 52
        $this->assertEquals($result['status'], 'success');
162 52
        $this->assertEquals(200, $this->_response->getStatusCode());
163 52
    }
164
165
    /**
166
     * @return mixed
167
     */
168 55
    public function getJsonResponse()
169
    {
170 55
        return json_decode((string)$this->_response->getBody(), true);
171
    }
172
173
    /**
174
     * Assert result is error.
175
     *
176
     * @param array $result Response.
177
     * @param int $code Result code.
178
     * @return void
179
     */
180 7
    public function assertError($result, $code = null)
181
    {
182 7
        $this->assertTrue(is_array($result));
183 7
        $this->assertEquals($result['status'], 'error');
184 7
        $this->assertEquals(200, $this->_response->getStatusCode());
185 7
        if (!empty($code)) {
186 7
            $this->assertEquals($code, $result['code']);
187 7
        }
188 7
    }
189
190
    /**
191
     * Helper method for status assertions.
192
     *
193
     * @param int $code Status code.
194
     * @param string $message The error message.
195
     * @return void
196
     */
197
    public function assertStatus($code, $message = null)
198
    {
199
        if ($message === null) {
200
            $message = "Status code $code does not match";
201
        }
202
        $this->_assertStatus($code, $code, $message);
203
    }
204
205
    /**
206
     * Assert error message.
207
     *
208
     * @param array $result Response.
209
     * @param string $expectedMessage Message.
210
     * @return void
211
     */
212 3
    public function assertErrorMessage($result, $expectedMessage)
213
    {
214 3
        $message = Hash::get($result, 'message');
215 3
        $this->assertTrue(is_string($message) && strpos($message, $expectedMessage) === 0);
216 3
    }
217
}
218