Total Complexity | 40 |
Total Lines | 522 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like StandardTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StandardTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class StandardTest extends \PHPUnit\Framework\TestCase |
||
13 | { |
||
14 | private $object; |
||
15 | private $context; |
||
16 | private $testItem; |
||
17 | |||
18 | |||
19 | protected function setUp() : void |
||
20 | { |
||
21 | \Aimeos\MShop::cache( true ); |
||
22 | |||
23 | $this->context = \TestHelper::context(); |
||
24 | |||
25 | $manager = \Aimeos\MShop::create( $this->context, 'product' ); |
||
26 | $this->testItem = $manager->find( 'U:TESTP', ['attribute', 'media', 'price', 'product', 'text'] ); |
||
27 | |||
28 | $this->object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context ); |
||
29 | } |
||
30 | |||
31 | |||
32 | protected function tearDown() : void |
||
40 | } |
||
41 | |||
42 | |||
43 | public function testAdd() |
||
49 | } |
||
50 | |||
51 | |||
52 | public function testClear() |
||
53 | { |
||
54 | $this->object->addProduct( $this->testItem ); |
||
55 | |||
56 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $this->object->clear() ); |
||
57 | $this->assertEquals( 0, count( $this->object->get()->getProducts() ) ); |
||
58 | } |
||
59 | |||
60 | |||
61 | public function testGet() |
||
62 | { |
||
63 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $this->object->get() ); |
||
64 | } |
||
65 | |||
66 | |||
67 | public function testSave() |
||
68 | { |
||
69 | $stub = $this->getMockBuilder( \Aimeos\MShop\Order\Manager\Base\Standard::class ) |
||
70 | ->setConstructorArgs( [$this->context] ) |
||
71 | ->setMethods( ['setSession'] ) |
||
72 | ->getMock(); |
||
73 | |||
74 | \Aimeos\MShop::inject( 'order/base', $stub ); |
||
75 | |||
76 | $stub->expects( $this->exactly( 2 ) )->method( 'setSession' ); |
||
77 | |||
78 | $object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context ); |
||
79 | $object->addProduct( $this->testItem ); |
||
80 | |||
81 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $object->save() ); |
||
82 | } |
||
83 | |||
84 | |||
85 | public function testSetType() |
||
86 | { |
||
87 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $this->object->setType( 'test' ) ); |
||
88 | } |
||
89 | |||
90 | |||
91 | public function testLoad() |
||
106 | } |
||
107 | |||
108 | |||
109 | public function testStore() |
||
110 | { |
||
135 | } |
||
136 | |||
137 | |||
138 | public function testStoreLimit() |
||
149 | } |
||
150 | |||
151 | |||
152 | public function testAddDeleteProduct() |
||
153 | { |
||
154 | $basket = $this->object->get(); |
||
155 | $manager = \Aimeos\MShop::create( $this->context, 'product' ); |
||
156 | $item = $manager->find( 'CNC', ['attribute', 'media', 'price', 'product', 'text', 'locale/site'] ); |
||
157 | |||
158 | $result1 = $this->object->addProduct( $item, 2 ); |
||
159 | $item2 = $this->object->get()->getProduct( 0 ); |
||
160 | $result2 = $this->object->deleteProduct( 0 ); |
||
161 | |||
162 | $this->assertEquals( 0, count( $basket->getProducts() ) ); |
||
163 | $this->assertEquals( 'CNC', $item2->getProductCode() ); |
||
164 | $this->assertEquals( 'default', $item2->getStockType() ); |
||
165 | $this->assertEquals( 'Unit test site', $item2->getVendor() ); |
||
166 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result1 ); |
||
167 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result2 ); |
||
168 | } |
||
169 | |||
170 | |||
171 | public function testAddProductFractionalQuantity() |
||
172 | { |
||
173 | $manager = \Aimeos\MShop::create( $this->context, 'product' ); |
||
174 | $item = $manager->find( 'CNC', ['attribute', 'media', 'price', 'product', 'text'] ); |
||
175 | $item->setConfig( ['quantity-step' => '0.1'] ); |
||
176 | |||
177 | $this->object->addProduct( $item, 2.31 ); |
||
178 | $this->assertEquals( 2.4, $this->object->get()->getProduct( 0 )->getQuantity() ); |
||
179 | } |
||
180 | |||
181 | |||
182 | public function testAddProductCustomAttribute() |
||
183 | { |
||
184 | $attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'custom', [], 'product', 'date' ); |
||
185 | $attrValues = [$attrItem->getId() => '2000-01-01']; |
||
186 | |||
187 | $result = $this->object->addProduct( $this->testItem, 1, [], [], $attrValues ); |
||
188 | $basket = $this->object->get(); |
||
189 | |||
190 | $this->assertEquals( 1, count( $basket->getProducts() ) ); |
||
191 | $this->assertEquals( '2000-01-01', $basket->getProduct( 0 )->getAttribute( 'date', 'custom' ) ); |
||
192 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
193 | } |
||
194 | |||
195 | |||
196 | public function testAddProductCustomPrice() |
||
197 | { |
||
198 | $attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'custom', [], 'product', 'price' ); |
||
199 | $attrValues = [$attrItem->getId() => '0.01']; |
||
200 | |||
201 | $result = $this->object->addProduct( $this->testItem, 1, [], [], $attrValues ); |
||
202 | $basket = $this->object->get(); |
||
203 | |||
204 | $this->assertEquals( 1, count( $basket->getProducts() ) ); |
||
205 | $this->assertEquals( '0.01', $basket->getProduct( 0 )->getPrice()->getValue() ); |
||
206 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
207 | } |
||
208 | |||
209 | |||
210 | public function testAddProductCustomPriceException() |
||
211 | { |
||
212 | $attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'custom', [], 'product', 'price' ); |
||
213 | $attrValues = [$attrItem->getId() => ',']; |
||
214 | |||
215 | $this->expectException( \Aimeos\Controller\Frontend\Basket\Exception::class ); |
||
216 | $this->object->addProduct( $this->testItem, 1, [], [], $attrValues ); |
||
217 | } |
||
218 | |||
219 | |||
220 | public function testAddProductAttributePrice() |
||
221 | { |
||
222 | $attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'xs', [], 'product', 'size' ); |
||
223 | |||
224 | $result = $this->object->addProduct( $this->testItem, 1, [], [$attrItem->getId() => 2] ); |
||
225 | |||
226 | $this->assertEquals( '43.90', $this->object->get()->getPrice()->getValue() ); |
||
227 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
228 | } |
||
229 | |||
230 | |||
231 | public function testAddProductAttributeNotAssigned() |
||
232 | { |
||
233 | $attrItem = \Aimeos\MShop::create( $this->context, 'attribute' )->find( '30', [], 'product', 'width' ); |
||
234 | $ids = [$attrItem->getId()]; |
||
235 | |||
236 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
237 | $this->object->addProduct( $this->testItem, 1, [], $ids, $ids ); |
||
238 | } |
||
239 | |||
240 | |||
241 | public function testAddProductNegativeQuantityException() |
||
242 | { |
||
243 | $this->expectException( '\\Aimeos\\MShop\\Order\\Exception' ); |
||
244 | $this->object->addProduct( $this->testItem, -1 ); |
||
245 | } |
||
246 | |||
247 | |||
248 | public function testAddProductNoPriceException() |
||
249 | { |
||
250 | $item = \Aimeos\MShop::create( $this->context, 'product' )->find( 'MNOP' ); |
||
251 | |||
252 | $this->expectException( '\\Aimeos\\MShop\\Price\\Exception' ); |
||
253 | $this->object->addProduct( $item ); |
||
254 | } |
||
255 | |||
256 | |||
257 | public function testAddProductConfigAttributeException() |
||
258 | { |
||
259 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
260 | $this->object->addProduct( $this->testItem, 1, [], [-1] ); |
||
261 | } |
||
262 | |||
263 | |||
264 | public function testAddProductLowQuantityPriceException() |
||
265 | { |
||
266 | $item = \Aimeos\MShop::create( $this->context, 'product' )->find( 'IJKL', ['attribute', 'price'] ); |
||
267 | |||
268 | $this->expectException( '\\Aimeos\\MShop\\Price\\Exception' ); |
||
269 | $this->object->addProduct( $item ); |
||
270 | } |
||
271 | |||
272 | |||
273 | public function testAddProductHigherQuantities() |
||
274 | { |
||
275 | $item = \Aimeos\MShop::create( $this->context, 'product' )->find( 'IJKL', ['price'] ); |
||
276 | |||
277 | $result = $this->object->addProduct( $item, 2, [], [], [], 'unitstock' ); |
||
278 | |||
279 | $this->assertEquals( 2, $this->object->get()->getProduct( 0 )->getQuantity() ); |
||
280 | $this->assertEquals( 'IJKL', $this->object->get()->getProduct( 0 )->getProductCode() ); |
||
281 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
282 | } |
||
283 | |||
284 | |||
285 | public function testDeleteProductFlagError() |
||
286 | { |
||
287 | $this->object->addProduct( $this->testItem, 2 ); |
||
288 | |||
289 | $item = $this->object->get()->getProduct( 0 ); |
||
290 | $item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE ); |
||
291 | |||
292 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
293 | $this->object->deleteProduct( 0 ); |
||
294 | } |
||
295 | |||
296 | |||
297 | public function testUpdateProduct() |
||
298 | { |
||
299 | $this->object->addProduct( $this->testItem ); |
||
300 | |||
301 | $item = $this->object->get()->getProduct( 0 ); |
||
302 | $this->assertEquals( 1, $item->getQuantity() ); |
||
303 | |||
304 | $result = $this->object->updateProduct( 0, 4 ); |
||
305 | $item = $this->object->get()->getProduct( 0 ); |
||
306 | |||
307 | $this->assertEquals( 4, $item->getQuantity() ); |
||
308 | $this->assertEquals( 'U:TESTP', $item->getProductCode() ); |
||
309 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
310 | } |
||
311 | |||
312 | |||
313 | public function testUpdateProductFlagError() |
||
314 | { |
||
315 | $this->object->addProduct( $this->testItem, 2 ); |
||
316 | |||
317 | $item = $this->object->get()->getProduct( 0 ); |
||
318 | $item->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE ); |
||
319 | |||
320 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
321 | $this->object->updateProduct( 0, 4 ); |
||
322 | } |
||
323 | |||
324 | |||
325 | public function testAddCoupon() |
||
326 | { |
||
327 | $this->object->addProduct( $this->testItem, 2 ); |
||
328 | |||
329 | $result = $this->object->addCoupon( 'GHIJ' ); |
||
330 | $basket = $this->object->get(); |
||
331 | |||
332 | $this->assertEquals( 1, count( $basket->getCoupons() ) ); |
||
333 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
334 | } |
||
335 | |||
336 | |||
337 | public function testAddCouponExceedCount() |
||
338 | { |
||
339 | $this->object->addProduct( $this->testItem, 2 ); |
||
340 | $this->object->addCoupon( 'GHIJ' ); |
||
341 | |||
342 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
343 | $this->object->addCoupon( 'GHIJ' ); |
||
344 | } |
||
345 | |||
346 | |||
347 | public function testAddCouponInvalidCode() |
||
348 | { |
||
349 | $this->expectException( \Aimeos\MShop\Plugin\Provider\Exception::class ); |
||
350 | $this->object->addCoupon( 'invalid' ); |
||
351 | } |
||
352 | |||
353 | |||
354 | public function testDeleteCoupon() |
||
355 | { |
||
356 | $this->object->addProduct( $this->testItem, 2 ); |
||
357 | $this->object->addCoupon( '90AB' ); |
||
358 | |||
359 | $result = $this->object->deleteCoupon( '90AB' ); |
||
360 | $basket = $this->object->get(); |
||
361 | |||
362 | $this->assertEquals( 0, count( $basket->getCoupons() ) ); |
||
363 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
364 | } |
||
365 | |||
366 | |||
367 | public function testAddAddress() |
||
368 | { |
||
369 | $values = array( |
||
370 | 'order.base.address.company' => '<p onclick="javascript: alert(\'gotcha\');">Example company</p>', |
||
371 | 'order.base.address.vatid' => 'DE999999999', |
||
372 | 'order.base.address.title' => '<br/>Dr.', |
||
373 | 'order.base.address.salutation' => \Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR, |
||
374 | 'order.base.address.firstname' => 'firstunit', |
||
375 | 'order.base.address.lastname' => 'lastunit', |
||
376 | 'order.base.address.address1' => 'unit str.', |
||
377 | 'order.base.address.address2' => ' 166', |
||
378 | 'order.base.address.address3' => '4.OG', |
||
379 | 'order.base.address.postal' => '22769', |
||
380 | 'order.base.address.city' => 'Hamburg', |
||
381 | 'order.base.address.state' => 'Hamburg', |
||
382 | 'order.base.address.countryid' => 'de', |
||
383 | 'order.base.address.languageid' => 'de', |
||
384 | 'order.base.address.telephone' => '05554433221', |
||
385 | 'order.base.address.email' => '[email protected]', |
||
386 | 'order.base.address.telefax' => '05554433222', |
||
387 | 'order.base.address.website' => 'www.example.com', |
||
388 | 'random key from a random manager' => [], // ups, not a string |
||
389 | ); |
||
390 | |||
391 | $result = $this->object->addAddress( 'payment', $values ); |
||
392 | $address = $this->object->get()->getAddress( 'payment', 0 ); |
||
393 | |||
394 | $this->assertEquals( 'Example company', $address->getCompany() ); |
||
395 | $this->assertEquals( 'Dr.', $address->getTitle() ); |
||
396 | $this->assertEquals( 'firstunit', $address->getFirstname() ); |
||
397 | $this->assertInstanceOf( \Aimeos\Controller\Frontend\Basket\Iface::class, $result ); |
||
398 | } |
||
399 | |||
400 | |||
401 | public function testDeleteAddress() |
||
410 | } |
||
411 | |||
412 | |||
413 | |||
414 | public function testAddServicePayment() |
||
415 | { |
||
416 | $manager = \Aimeos\MShop::create( $this->context, 'service' ); |
||
417 | $service = $manager->find( 'unitpaymentcode', [], 'service', 'payment' ); |
||
418 | |||
419 | $this->object->addService( $service ); |
||
420 | $item = $this->object->get()->getService( 'payment', 0 )->getCode(); |
||
421 | $this->assertEquals( 'unitpaymentcode', $item ); |
||
422 | |||
423 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
424 | $this->object->addService( $service, ['prepay' => true] ); |
||
425 | } |
||
426 | |||
427 | |||
428 | public function testAddServiceDelivery() |
||
429 | { |
||
430 | $manager = \Aimeos\MShop::create( $this->context, 'service' ); |
||
431 | $service = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' ); |
||
432 | |||
433 | $this->object->addService( $service ); |
||
434 | $item = $this->object->get()->getService( 'delivery', 0 ); |
||
435 | $this->assertEquals( 'unitdeliverycode', $item->getCode() ); |
||
436 | |||
437 | $this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' ); |
||
438 | $this->object->addService( $service, ['fast shipping' => true, 'air shipping' => false] ); |
||
439 | } |
||
440 | |||
441 | |||
442 | public function testDeleteServices() |
||
452 | } |
||
453 | |||
454 | |||
455 | public function testDeleteServicePosition() |
||
456 | { |
||
457 | $manager = \Aimeos\MShop::create( $this->context, 'service' ); |
||
458 | $service = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' ); |
||
459 | |||
460 | $this->assertSame( $this->object, $this->object->addService( $service ) ); |
||
461 | $this->assertEquals( 'unitdeliverycode', $this->object->get()->getService( 'delivery', 0 )->getCode() ); |
||
462 | |||
463 | $this->assertSame( $this->object, $this->object->deleteService( 'delivery', 0 ) ); |
||
464 | $this->assertEquals( 0, count( $this->object->get()->getService( 'delivery' ) ) ); |
||
465 | } |
||
466 | |||
467 | |||
468 | public function testCheckLocale() |
||
515 | } |
||
516 | |||
517 | |||
518 | /** |
||
519 | * @param string $company |
||
520 | */ |
||
521 | protected function getAddress( $company ) |
||
522 | { |
||
523 | $customer = \Aimeos\MShop\Customer\Manager\Factory::create( \TestHelper::context(), 'Standard' ); |
||
524 | $addressManager = $customer->getSubManager( 'address', 'Standard' ); |
||
525 | |||
534 | } |
||
535 | } |
||
536 |