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\Coupon; |
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\Coupon\Standard( $this->context, $this->view, $templatePaths, 'basket/coupon' ); |
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
|
|
|
$this->addProduct( 'CNC' ); |
38
|
|
|
|
39
|
|
|
$body = '{"data": {"type": "basket/coupon", "id": "GHIJ"}}'; |
40
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
41
|
|
|
|
42
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
43
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
44
|
|
|
$this->assertEquals( 1, count( $result['data']['attributes']['coupons'] ) ); |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
$body = '{"data": {"type": "basket/coupon", "id": "GHIJ"}}'; |
48
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
49
|
|
|
|
50
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
51
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
52
|
|
|
|
53
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
54
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
55
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
56
|
|
|
|
57
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
58
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
59
|
|
|
$this->assertArrayNotHasKey( 'coupon', $result['data']['attributes'] ); |
60
|
|
|
|
61
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
public function testDeleteById() |
66
|
|
|
{ |
67
|
|
|
$this->addProduct( 'CNC' ); |
68
|
|
|
|
69
|
|
|
$body = '{"data": {"type": "basket/coupon", "id": "GHIJ"}}'; |
70
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
71
|
|
|
|
72
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
73
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
74
|
|
|
|
75
|
|
|
$this->assertEquals( 1, count( $result['data']['attributes']['coupons'] ) ); |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
$params = array( 'id' => 'default', 'relatedid' => 'GHIJ' ); |
79
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
80
|
|
|
$this->view->addHelper( 'param', $helper ); |
81
|
|
|
|
82
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
83
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
84
|
|
|
|
85
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
86
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
87
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
88
|
|
|
|
89
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
90
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
91
|
|
|
$this->assertArrayNotHasKey( 'coupon', $result['data']['attributes'] ); |
92
|
|
|
|
93
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
public function testDeleteMShopException() |
98
|
|
|
{ |
99
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
100
|
|
|
|
101
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
102
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
106
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
public function testDeleteException() |
111
|
|
|
{ |
112
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
113
|
|
|
|
114
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
115
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
119
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
public function testPost() |
124
|
|
|
{ |
125
|
|
|
$this->addProduct( 'CNC' ); |
126
|
|
|
|
127
|
|
|
$body = '{"data": {"type": "basket/coupon", "id": "GHIJ"}}'; |
128
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
129
|
|
|
|
130
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
131
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
135
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
136
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
137
|
|
|
|
138
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
139
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
140
|
|
|
$this->assertEquals( 1, count( $result['data']['attributes']['coupons'] ) ); |
141
|
|
|
$this->assertEquals( 'GHIJ', $result['data']['attributes']['coupons'][0] ); |
142
|
|
|
|
143
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
public function testPostMultiple() |
148
|
|
|
{ |
149
|
|
|
$this->addProduct( 'CNC' ); |
150
|
|
|
|
151
|
|
|
$body = '{"data": [{ |
152
|
|
|
"type": "basket/coupon", "id": "90AB" |
153
|
|
|
}, { |
154
|
|
|
"type": "basket/coupon", "id": "GHIJ" |
155
|
|
|
}]}'; |
156
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
157
|
|
|
|
158
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
159
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
163
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
164
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
165
|
|
|
|
166
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
167
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
168
|
|
|
$this->assertEquals( 2, count( $result['data']['attributes']['coupons'] ) ); |
169
|
|
|
$this->assertEquals( '90AB', $result['data']['attributes']['coupons'][0] ); |
170
|
|
|
$this->assertEquals( 'GHIJ', $result['data']['attributes']['coupons'][1] ); |
171
|
|
|
|
172
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
public function testPostMShopException() |
177
|
|
|
{ |
178
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
179
|
|
|
|
180
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
181
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
185
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
public function testPostException() |
190
|
|
|
{ |
191
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
192
|
|
|
|
193
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
194
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
198
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
protected function addProduct( $code ) |
203
|
|
|
{ |
204
|
|
|
$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( $code )->getId(); |
|
|
|
|
205
|
|
|
|
206
|
|
|
$body = '{"data": {"type": "basket/product", "attributes": {"productid": ' . $prodId . '}}}'; |
207
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
208
|
|
|
|
209
|
|
|
$templatePaths = \TestHelperJapi::getTemplatePaths(); |
210
|
|
|
$object = new \Aimeos\Client\JsonApi\Basket\Product\Standard( $this->context, $this->view, $templatePaths, 'basket/product' ); |
211
|
|
|
|
212
|
|
|
$object->post( $request, $this->view->response() ); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Returns a test object with a mocked basket controller |
218
|
|
|
* |
219
|
|
|
* @param string $method Basket controller method name to mock |
220
|
|
|
* @param mixed $result Return value of the mocked method |
221
|
|
|
*/ |
222
|
|
|
protected function getObject( $method, $result ) |
223
|
|
|
{ |
224
|
|
|
$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
225
|
|
|
->setConstructorArgs( [$this->context] ) |
226
|
|
|
->setMethods( [$method] ) |
227
|
|
|
->getMock(); |
228
|
|
|
|
229
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
230
|
|
|
|
231
|
|
|
\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl ); |
232
|
|
|
|
233
|
|
|
$templatePaths = \TestHelperJapi::getTemplatePaths(); |
234
|
|
|
$object = new \Aimeos\Client\JsonApi\Basket\Coupon\Standard( $this->context, $this->view, $templatePaths, 'basket/coupon' ); |
235
|
|
|
|
236
|
|
|
return $object; |
237
|
|
|
} |
238
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: