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\Service\Renderer; |
13
|
|
|
|
14
|
|
|
use CakeDC\Api\Exception\UnauthenticatedException; |
15
|
|
|
use CakeDC\Api\Service\Action\Result; |
16
|
|
|
use CakeDC\Api\Service\FallbackService; |
17
|
|
|
use CakeDC\Api\Service\Renderer\RawRenderer; |
18
|
|
|
use CakeDC\Api\Service\Service; |
19
|
|
|
use CakeDC\Api\TestSuite\TestCase; |
20
|
|
|
use CakeDC\Api\Test\ConfigTrait; |
21
|
|
|
use CakeDC\Api\Test\FixturesTrait; |
22
|
|
|
use Cake\Core\Configure; |
23
|
|
|
|
24
|
|
|
class RawRendererTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
use ConfigTrait; |
28
|
|
|
use FixturesTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Service |
32
|
|
|
*/ |
33
|
|
|
public $Service; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* setUp method |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function setUp() |
41
|
|
|
{ |
42
|
|
|
parent::setUp(); |
43
|
|
|
|
44
|
|
|
$this->_initializeRequest(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* tearDown method |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function tearDown() |
53
|
|
|
{ |
54
|
|
|
unset($this->Action); |
55
|
|
|
parent::tearDown(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Test initialize |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function testRendererInitializeByClassName() |
64
|
|
|
{ |
65
|
|
|
$response = $this |
66
|
|
|
->getMockBuilder('Cake\Http\Response') |
67
|
|
|
->setMethods(['withStatus', 'withType', 'withStringBody']) |
68
|
|
|
->getMock(); |
69
|
|
|
|
70
|
|
|
$this->_initializeRequest([], 'GET', ['response' => $response]); |
71
|
|
|
$serviceOptions = [ |
72
|
|
|
'version' => null, |
73
|
|
|
'request' => $this->request, |
|
|
|
|
74
|
|
|
'response' => $response, |
75
|
|
|
'rendererClass' => 'CakeDC/Api.Raw' |
76
|
|
|
]; |
77
|
|
|
$this->Service = new FallbackService($serviceOptions); |
78
|
|
|
$renderer = $this->Service->getRenderer(); |
79
|
|
|
$this->assertTrue($renderer instanceof RawRenderer); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Test render response |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function testRendererSuccess() |
88
|
|
|
{ |
89
|
|
|
$response = $this |
90
|
|
|
->getMockBuilder('Cake\Http\Response') |
91
|
|
|
->setMethods(['withStatus', 'withType', 'withStringBody']) |
92
|
|
|
->getMock(); |
93
|
|
|
|
94
|
|
|
$this->_initializeRequest([], 'GET', ['response' => $response]); |
95
|
|
|
$serviceOptions = [ |
96
|
|
|
'version' => null, |
97
|
|
|
'request' => $this->request, |
98
|
|
|
'response' => $response, |
99
|
|
|
'rendererClass' => 'CakeDC/Api.Raw' |
100
|
|
|
]; |
101
|
|
|
$this->Service = new FallbackService($serviceOptions); |
102
|
|
|
|
103
|
|
|
$result = new Result(); |
104
|
|
|
$statusCode = 200; |
105
|
|
|
$result->setCode($statusCode); |
106
|
|
|
$data = 'Updated!'; |
107
|
|
|
$result->setData($data); |
108
|
|
|
$renderer = $this->Service->getRenderer(); |
109
|
|
|
|
110
|
|
|
$response->expects($this->once()) |
111
|
|
|
->method('withStatus') |
112
|
|
|
->with($statusCode) |
113
|
|
|
->will($this->returnValue($response)); |
114
|
|
|
$response->expects($this->once()) |
115
|
|
|
->method('withStringBody') |
116
|
|
|
->with($data) |
117
|
|
|
->will($this->returnValue($response)); |
118
|
|
|
$response->expects($this->once()) |
119
|
|
|
->method('withType') |
120
|
|
|
->with('text/plain') |
121
|
|
|
->will($this->returnValue($response)); |
122
|
|
|
|
123
|
|
|
$renderer->response($result); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Test render error |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function testRendererError() |
132
|
|
|
{ |
133
|
|
|
$response = $this |
134
|
|
|
->getMockBuilder('Cake\Http\Response') |
135
|
|
|
->setMethods(['withStatus', 'withType', 'withStringBody']) |
136
|
|
|
->getMock(); |
137
|
|
|
|
138
|
|
|
$this->_initializeRequest([], 'GET', ['response' => $response]); |
139
|
|
|
$serviceOptions = [ |
140
|
|
|
'version' => null, |
141
|
|
|
'request' => $this->request, |
142
|
|
|
'response' => $response, |
143
|
|
|
'rendererClass' => 'CakeDC/Api.Raw' |
144
|
|
|
]; |
145
|
|
|
$this->Service = new FallbackService($serviceOptions); |
146
|
|
|
|
147
|
|
|
Configure::write('debug', 0); |
148
|
|
|
$error = new UnauthenticatedException(); |
149
|
|
|
$renderer = $this->Service->getRenderer(); |
150
|
|
|
|
151
|
|
|
$response->expects($this->once()) |
152
|
|
|
->method('withStringBody') |
153
|
|
|
->with('Unauthenticated') |
154
|
|
|
->will($this->returnValue($response)); |
155
|
|
|
$response->expects($this->once()) |
156
|
|
|
->method('withType') |
157
|
|
|
->with('text/plain') |
158
|
|
|
->will($this->returnValue($response)); |
159
|
|
|
|
160
|
|
|
$renderer->error($error); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: