|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
namespace Aimeos\Client\JsonApi\Basket\Address; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class StandardTest extends \PHPUnit\Framework\TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
private $context; |
|
15
|
|
|
private $object; |
|
16
|
|
|
private $view; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
protected function setUp() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->context = \TestHelperJapi::getContext(); |
|
22
|
|
|
$templatePaths = \TestHelperJapi::getTemplatePaths(); |
|
23
|
|
|
$this->view = $this->context->getView(); |
|
24
|
|
|
|
|
25
|
|
|
$this->object = new \Aimeos\Client\JsonApi\Basket\Address\Standard( $this->context, $this->view, $templatePaths, 'basket/address' ); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
protected function tearDown() |
|
30
|
|
|
{ |
|
31
|
|
|
\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', null ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
public function testDelete() |
|
36
|
|
|
{ |
|
37
|
|
|
$body = '{"data": {"type": "basket/address", "id": "payment", "attributes": {"order.base.address.firstname": "test"}}}'; |
|
38
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
|
39
|
|
|
|
|
40
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
|
41
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/address']['data'] ) ); |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
$body = '{"data": {"type": "basket/address", "id": "payment"}}'; |
|
47
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
|
48
|
|
|
|
|
49
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
|
50
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
|
53
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
|
54
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
|
57
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
|
58
|
|
|
$this->assertArrayNotHasKey( 'basket/address', $result['data']['relationships'] ); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
public function testDeleteById() |
|
65
|
|
|
{ |
|
66
|
|
|
$body = '{"data": {"type": "basket/address", "id": "payment", "attributes": {"order.base.address.firstname": "test"}}}'; |
|
67
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
|
68
|
|
|
|
|
69
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
|
70
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/address']['data'] ) ); |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
$params = array( 'id' => 'default', 'relatedid' => 'payment' ); |
|
76
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
|
77
|
|
|
$this->view->addHelper( 'param', $helper ); |
|
78
|
|
|
|
|
79
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
|
80
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
|
83
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
|
84
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
|
87
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
|
88
|
|
|
$this->assertArrayNotHasKey( 'basket/address', $result['data']['relationships'] ); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
public function testDeleteMShopException() |
|
95
|
|
|
{ |
|
96
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
|
97
|
|
|
|
|
98
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
|
99
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
|
103
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
public function testDeleteException() |
|
108
|
|
|
{ |
|
109
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
|
110
|
|
|
|
|
111
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
|
112
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
|
116
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
public function testPost() |
|
121
|
|
|
{ |
|
122
|
|
|
$body = '{"data": {"type": "basket/address", "id": "payment", "attributes": {"order.base.address.firstname": "test"}}}'; |
|
123
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
|
124
|
|
|
|
|
125
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
|
126
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
|
130
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
|
131
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
|
134
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
|
135
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/address']['data'] ) ); |
|
136
|
|
|
$this->assertEquals( 'test', $result['included'][0]['attributes']['order.base.address.firstname'] ); |
|
137
|
|
|
|
|
138
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
public function testPostMultiple() |
|
143
|
|
|
{ |
|
144
|
|
|
$body = '{"data": [{ |
|
145
|
|
|
"type": "basket/address", "id": "payment", "attributes": {"order.base.address.firstname": "test"} |
|
146
|
|
|
}, { |
|
147
|
|
|
"type": "basket/address", "id": "delivery", "attributes": {"order.base.address.lastname": "test"} |
|
148
|
|
|
}]}'; |
|
149
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
|
150
|
|
|
|
|
151
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
|
152
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
|
156
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
|
157
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
|
158
|
|
|
|
|
159
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
|
160
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
|
161
|
|
|
$this->assertEquals( 2, count( $result['data']['relationships']['basket/address']['data'] ) ); |
|
162
|
|
|
$this->assertEquals( 'test', $result['included'][0]['attributes']['order.base.address.firstname'] ); |
|
163
|
|
|
$this->assertEquals( 'test', $result['included'][1]['attributes']['order.base.address.lastname'] ); |
|
164
|
|
|
|
|
165
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
public function testPostMShopException() |
|
170
|
|
|
{ |
|
171
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
|
172
|
|
|
|
|
173
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
|
174
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
|
178
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
public function testPostException() |
|
183
|
|
|
{ |
|
184
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
|
185
|
|
|
|
|
186
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
|
187
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
|
191
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
public function testOptions() |
|
196
|
|
|
{ |
|
197
|
|
|
$response = $this->object->options( $this->view->request(), $this->view->response() ); |
|
198
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
199
|
|
|
|
|
200
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
|
201
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
|
202
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
|
203
|
|
|
|
|
204
|
|
|
$this->assertEquals( null, $result['meta']['prefix'] ); |
|
205
|
|
|
$this->assertEquals( 20, count( $result['meta']['attributes'] ) ); |
|
206
|
|
|
$this->assertArrayNotHasKey( 'filter', $result['meta'] ); |
|
207
|
|
|
$this->assertArrayNotHasKey( 'sort', $result['meta'] ); |
|
208
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Returns a test object with a mocked basket controller |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $method Basket controller method name to mock |
|
216
|
|
|
* @param mixed $result Return value of the mocked method |
|
217
|
|
|
*/ |
|
218
|
|
|
protected function getObject( $method, $result ) |
|
219
|
|
|
{ |
|
220
|
|
|
$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
|
221
|
|
|
->setConstructorArgs( [$this->context] ) |
|
222
|
|
|
->setMethods( [$method] ) |
|
223
|
|
|
->getMock(); |
|
224
|
|
|
|
|
225
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
|
226
|
|
|
|
|
227
|
|
|
\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl ); |
|
228
|
|
|
|
|
229
|
|
|
$templatePaths = \TestHelperJapi::getTemplatePaths(); |
|
230
|
|
|
$object = new \Aimeos\Client\JsonApi\Basket\Address\Standard( $this->context, $this->view, $templatePaths, 'basket/address' ); |
|
231
|
|
|
|
|
232
|
|
|
return $object; |
|
233
|
|
|
} |
|
234
|
|
|
} |