| Total Complexity | 49 |
| Total Lines | 563 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BaseTest 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 BaseTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class BaseTest extends \PHPUnit\Framework\TestCase |
||
| 14 | { |
||
| 15 | private $object; |
||
| 16 | private $products; |
||
| 17 | private $addresses; |
||
| 18 | private $services; |
||
| 19 | private $coupons; |
||
| 20 | |||
| 21 | |||
| 22 | protected function setUp() : void |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | |||
| 78 | protected function tearDown() : void |
||
| 81 | } |
||
| 82 | |||
| 83 | |||
| 84 | public function testArrayMethods() |
||
| 85 | { |
||
| 86 | $this->assertFalse( isset( $this->object['test'] ) ); |
||
| 87 | $this->assertEquals( null, $this->object['test'] ); |
||
| 88 | |||
| 89 | $this->object['test'] = 'value'; |
||
| 90 | |||
| 91 | $this->assertTrue( isset( $this->object['test'] ) ); |
||
| 92 | $this->assertEquals( 'value', $this->object['test'] ); |
||
| 93 | |||
| 94 | $this->expectException( \LogicException::class ); |
||
| 95 | unset( $this->object['test'] ); |
||
| 96 | } |
||
| 97 | |||
| 98 | |||
| 99 | public function testMagicMethods() |
||
| 100 | { |
||
| 101 | $this->assertFalse( isset( $this->object->test ) ); |
||
| 102 | $this->assertEquals( null, $this->object->test ); |
||
| 103 | |||
| 104 | $this->object->test = 'value'; |
||
| 105 | |||
| 106 | $this->assertTrue( isset( $this->object->test ) ); |
||
| 107 | $this->assertEquals( 'value', $this->object->test ); |
||
| 108 | } |
||
| 109 | |||
| 110 | |||
| 111 | public function testGetSet() |
||
| 112 | { |
||
| 113 | $this->assertEquals( false, $this->object->get( 'test', false ) ); |
||
| 114 | |||
| 115 | $this->object->set( 'test', 'value' ); |
||
| 116 | |||
| 117 | $this->assertEquals( 'value', $this->object->get( 'test', false ) ); |
||
| 118 | } |
||
| 119 | |||
| 120 | |||
| 121 | public function testIsModified() |
||
| 122 | { |
||
| 123 | $this->assertFalse( $this->object->isModified() ); |
||
| 124 | } |
||
| 125 | |||
| 126 | |||
| 127 | public function testAddProductAppend() |
||
| 128 | { |
||
| 129 | $this->object->setProducts( $this->products ); |
||
| 130 | |||
| 131 | $products = $this->object->getProducts(); |
||
| 132 | $product = $this->createProduct( 'prodid3' ); |
||
| 133 | $products[] = $product; |
||
| 134 | |||
| 135 | $result = $this->object->addProduct( $product ); |
||
| 136 | |||
| 137 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 138 | $this->assertEquals( $products, $this->object->getProducts() ); |
||
| 139 | $this->assertSame( $product, $this->object->getProduct( 2 ) ); |
||
| 140 | $this->assertTrue( $this->object->isModified() ); |
||
| 141 | } |
||
| 142 | |||
| 143 | |||
| 144 | public function testAddProductInsert() |
||
| 145 | { |
||
| 146 | $this->object->setProducts( $this->products ); |
||
| 147 | |||
| 148 | $products = $this->object->getProducts(); |
||
| 149 | $products[1] = $this->createProduct( 'prodid3' ); |
||
| 150 | |||
| 151 | $result = $this->object->addProduct( $products[1], 1 ); |
||
| 152 | |||
| 153 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 154 | $this->assertSame( $products[1], $this->object->getProduct( 1 ) ); |
||
| 155 | $this->assertEquals( $products, $this->object->getProducts() ); |
||
| 156 | $this->assertTrue( $this->object->isModified() ); |
||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | public function testAddProductInsertEnd() |
||
| 161 | { |
||
| 162 | $this->object->setProducts( $this->products ); |
||
| 163 | |||
| 164 | $products = $this->object->getProducts(); |
||
| 165 | $product = $this->createProduct( 'prodid3' ); |
||
| 166 | $products[] = $product; |
||
| 167 | |||
| 168 | $result = $this->object->addProduct( $product, 2 ); |
||
| 169 | |||
| 170 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 171 | $this->assertEquals( $products, $this->object->getProducts() ); |
||
| 172 | $this->assertSame( $product, $this->object->getProduct( 2 ) ); |
||
| 173 | $this->assertTrue( $this->object->isModified() ); |
||
| 174 | } |
||
| 175 | |||
| 176 | |||
| 177 | public function testAddProductSame() |
||
| 178 | { |
||
| 179 | $product = $this->createProduct( 'prodid3' )->setQuantity( 5 ); |
||
| 180 | |||
| 181 | $this->object->addProduct( $product ); |
||
| 182 | $this->object->addProduct( $product ); |
||
| 183 | |||
| 184 | $this->assertEquals( 10, $this->object->getProduct( 0 )->getQuantity() ); |
||
| 185 | $this->assertEquals( [0 => $product], $this->object->getProducts()->toArray() ); |
||
| 186 | $this->assertTrue( $this->object->isModified() ); |
||
| 187 | } |
||
| 188 | |||
| 189 | |||
| 190 | public function testAddProductStablePosition() |
||
| 191 | { |
||
| 192 | $this->object->setProducts( $this->products ); |
||
| 193 | |||
| 194 | $product = $this->createProduct( 'prodid3' )->setQuantity( 5 ); |
||
| 195 | $this->object->addProduct( $product ); |
||
| 196 | |||
| 197 | $testProduct = $this->object->getProduct( 1 ); |
||
| 198 | $this->object->deleteProduct( 0 ); |
||
| 199 | $this->object->deleteProduct( 1 ); |
||
| 200 | $result = $this->object->addProduct( $testProduct, 1 ); |
||
| 201 | |||
| 202 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 203 | $this->assertEquals( [1 => $testProduct, 2 => $product], $this->object->getProducts()->toArray() ); |
||
| 204 | } |
||
| 205 | |||
| 206 | |||
| 207 | public function testDeleteProduct() |
||
| 208 | { |
||
| 209 | $this->object->addProduct( $this->products[0] ); |
||
| 210 | $result = $this->object->deleteProduct( 0 ); |
||
| 211 | |||
| 212 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 213 | $this->assertSame( [], $this->object->getProducts()->toArray() ); |
||
| 214 | $this->assertTrue( $this->object->isModified() ); |
||
| 215 | } |
||
| 216 | |||
| 217 | |||
| 218 | public function testGetProducts() |
||
| 219 | { |
||
| 220 | $this->object->setProducts( $this->products ); |
||
| 221 | |||
| 222 | $this->assertSame( $this->products, $this->object->getProducts()->toArray() ); |
||
| 223 | $this->assertSame( $this->products[1], $this->object->getProduct( 1 ) ); |
||
| 224 | } |
||
| 225 | |||
| 226 | |||
| 227 | public function testSetProducts() |
||
| 228 | { |
||
| 229 | $result = $this->object->setProducts( $this->products ); |
||
| 230 | |||
| 231 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 232 | $this->assertSame( $this->products, $this->object->getProducts()->toArray() ); |
||
| 233 | $this->assertTrue( $this->object->isModified() ); |
||
| 234 | } |
||
| 235 | |||
| 236 | |||
| 237 | public function testAddAddress() |
||
| 238 | { |
||
| 239 | $type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT; |
||
| 240 | $result = $this->object->addAddress( $this->addresses[$type][0], $type ); |
||
| 241 | |||
| 242 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 243 | $this->assertEquals( $this->addresses[$type], $this->object->getAddress( $type ) ); |
||
| 244 | $this->assertTrue( $this->object->isModified() ); |
||
| 245 | } |
||
| 246 | |||
| 247 | |||
| 248 | public function testAddAddressMultiple() |
||
| 249 | { |
||
| 250 | $this->object->addAddress( $this->addresses['payment'][0], 'payment' ); |
||
| 251 | $result = $this->object->addAddress( $this->addresses['payment'][0], 'payment' ); |
||
| 252 | |||
| 253 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 254 | $this->assertEquals( 2, count( $this->object->getAddress( 'payment' ) ) ); |
||
| 255 | $this->assertTrue( $this->object->isModified() ); |
||
| 256 | } |
||
| 257 | |||
| 258 | |||
| 259 | public function testAddAddressPosition() |
||
| 260 | { |
||
| 261 | $type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT; |
||
| 262 | |||
| 263 | $this->object->addAddress( $this->addresses[$type][0], $type ); |
||
| 264 | $result = $this->object->addAddress( $this->addresses[$type][0], $type, 0 ); |
||
| 265 | |||
| 266 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 267 | $this->assertEquals( $this->addresses[$type], $this->object->getAddress( $type ) ); |
||
| 268 | $this->assertTrue( $this->object->isModified() ); |
||
| 269 | } |
||
| 270 | |||
| 271 | |||
| 272 | public function testDeleteAddress() |
||
| 273 | { |
||
| 274 | $type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT; |
||
| 275 | $this->object->setAddresses( $this->addresses ); |
||
| 276 | $result = $this->object->deleteAddress( $type ); |
||
| 277 | |||
| 278 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 279 | $this->assertEquals( [], $this->object->getAddress( $type ) ); |
||
| 280 | $this->assertTrue( $this->object->isModified() ); |
||
| 281 | } |
||
| 282 | |||
| 283 | |||
| 284 | public function testDeleteAddressPosition() |
||
| 294 | } |
||
| 295 | |||
| 296 | |||
| 297 | public function testDeleteAddressPositionInvalid() |
||
| 298 | { |
||
| 299 | $type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT; |
||
| 300 | $this->object->setAddresses( $this->addresses ); |
||
| 301 | |||
| 302 | $result = $this->object->deleteAddress( $type, 1 ); |
||
| 303 | |||
| 304 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 305 | $this->assertEquals( $this->addresses[$type], $this->object->getAddress( $type ) ); |
||
| 306 | } |
||
| 307 | |||
| 308 | |||
| 309 | public function testGetAddress() |
||
| 310 | { |
||
| 311 | $this->object->setAddresses( $this->addresses ); |
||
| 312 | $type = \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT; |
||
| 313 | |||
| 314 | $this->assertEquals( $this->addresses[$type], $this->object->getAddress( $type ) ); |
||
| 315 | } |
||
| 316 | |||
| 317 | |||
| 318 | public function testGetAddressSingle() |
||
| 324 | } |
||
| 325 | |||
| 326 | |||
| 327 | public function testGetAddressException() |
||
| 328 | { |
||
| 329 | $this->expectException( \Aimeos\MShop\Order\Exception::class ); |
||
| 330 | $this->object->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, 0 ); |
||
| 331 | } |
||
| 332 | |||
| 333 | |||
| 334 | public function testSetAddresses() |
||
| 335 | { |
||
| 336 | $result = $this->object->setAddresses( $this->addresses ); |
||
| 337 | |||
| 338 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 339 | $this->assertEquals( $this->addresses, $this->object->getAddresses()->toArray() ); |
||
| 340 | $this->assertTrue( $this->object->isModified() ); |
||
| 341 | } |
||
| 342 | |||
| 343 | |||
| 344 | public function testAddService() |
||
| 345 | { |
||
| 346 | $type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT; |
||
| 347 | $result = $this->object->addService( $this->services[$type][0], $type ); |
||
| 348 | |||
| 349 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 350 | $this->assertEquals( 1, count( $this->object->getService( $type ) ) ); |
||
| 351 | $this->assertTrue( $this->object->isModified() ); |
||
| 352 | } |
||
| 353 | |||
| 354 | |||
| 355 | public function testAddServicePosition() |
||
| 356 | { |
||
| 357 | $type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT; |
||
| 358 | |||
| 359 | $this->object->addService( $this->services[$type][0], $type ); |
||
| 360 | $result = $this->object->addService( $this->services[$type][0], $type, 0 ); |
||
| 361 | |||
| 362 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 363 | $this->assertEquals( 1, count( $this->object->getService( $type ) ) ); |
||
| 364 | $this->assertTrue( $this->object->isModified() ); |
||
| 365 | } |
||
| 366 | |||
| 367 | |||
| 368 | public function testDeleteService() |
||
| 369 | { |
||
| 370 | $type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT; |
||
| 371 | $this->object->setServices( $this->services ); |
||
| 372 | |||
| 373 | $result = $this->object->deleteService( $type ); |
||
| 374 | |||
| 375 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 376 | $this->assertEquals( [], $this->object->getService( $type ) ); |
||
| 377 | $this->assertTrue( $this->object->isModified() ); |
||
| 378 | } |
||
| 379 | |||
| 380 | |||
| 381 | public function testDeleteServicePosition() |
||
| 382 | { |
||
| 383 | $type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT; |
||
| 384 | $this->object->setServices( $this->services ); |
||
| 385 | |||
| 386 | $result = $this->object->deleteService( $type, 0 ); |
||
| 387 | |||
| 388 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 389 | $this->assertEquals( [], $this->object->getService( $type ) ); |
||
| 390 | $this->assertTrue( $this->object->isModified() ); |
||
| 391 | } |
||
| 392 | |||
| 393 | |||
| 394 | public function testDeleteServicePositionInvalid() |
||
| 395 | { |
||
| 396 | $type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT; |
||
| 397 | $this->object->setServices( $this->services ); |
||
| 398 | |||
| 399 | $result = $this->object->deleteService( $type, 1 ); |
||
| 400 | |||
| 401 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 402 | $this->assertEquals( $this->services[$type], $this->object->getService( $type ) ); |
||
| 403 | } |
||
| 404 | |||
| 405 | |||
| 406 | public function testGetService() |
||
| 407 | { |
||
| 408 | $this->object->setServices( $this->services ); |
||
| 409 | |||
| 410 | $payments = $this->object->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT ); |
||
| 411 | $deliveries = $this->object->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_DELIVERY ); |
||
| 412 | |||
| 413 | $this->assertEquals( 2, count( $this->object->getServices() ) ); |
||
| 414 | $this->assertEquals( 1, count( $payments ) ); |
||
| 415 | $this->assertEquals( 1, count( $deliveries ) ); |
||
| 416 | |||
| 417 | $this->assertEquals( $this->services['payment'], $payments ); |
||
| 418 | $this->assertEquals( $this->services['delivery'], $deliveries ); |
||
| 419 | } |
||
| 420 | |||
| 421 | |||
| 422 | public function testGetServiceSingle() |
||
| 423 | { |
||
| 424 | $this->object->setServices( $this->services ); |
||
| 425 | |||
| 426 | $service = $this->object->getService( 'payment', 0 ); |
||
| 427 | $this->assertEquals( 'testpay', $service->getCode() ); |
||
| 428 | } |
||
| 429 | |||
| 430 | |||
| 431 | public function testGetServiceException() |
||
| 432 | { |
||
| 433 | $this->expectException( \Aimeos\MShop\Order\Exception::class ); |
||
| 434 | $this->object->getService( 'payment', 100 ); |
||
| 435 | } |
||
| 436 | |||
| 437 | |||
| 438 | public function testSetServices() |
||
| 439 | { |
||
| 440 | $result = $this->object->setServices( $this->services ); |
||
| 441 | |||
| 442 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 443 | $this->assertEquals( $this->services, $this->object->getServices()->toArray() ); |
||
| 444 | $this->assertTrue( $this->object->isModified() ); |
||
| 445 | } |
||
| 446 | |||
| 447 | |||
| 448 | public function testAddCoupon() |
||
| 449 | { |
||
| 450 | $result = $this->object->addCoupon( 'OPQR' ); |
||
| 451 | |||
| 452 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 453 | $this->assertEquals( ['OPQR' => []], $this->object->getCoupons()->toArray() ); |
||
| 454 | $this->assertTrue( $this->object->isModified() ); |
||
| 455 | } |
||
| 456 | |||
| 457 | |||
| 458 | public function testDeleteCoupon() |
||
| 466 | } |
||
| 467 | |||
| 468 | |||
| 469 | public function testGetCoupons() |
||
| 470 | { |
||
| 471 | $this->object->setCoupons( $this->coupons ); |
||
| 472 | $this->assertEquals( $this->coupons, $this->object->getCoupons() ); |
||
| 473 | } |
||
| 474 | |||
| 475 | |||
| 476 | public function testSetCoupon() |
||
| 477 | { |
||
| 478 | $result = $this->object->setCoupon( 'OPQR', $this->coupons['OPQR'] ); |
||
| 479 | |||
| 480 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 481 | $this->assertEquals( map( ['OPQR' => $this->coupons['OPQR']] ), $this->object->getCoupons() ); |
||
| 482 | $this->assertTrue( $this->object->isModified() ); |
||
| 483 | } |
||
| 484 | |||
| 485 | |||
| 486 | public function testSetCoupons() |
||
| 493 | } |
||
| 494 | |||
| 495 | |||
| 496 | public function testGetTaxes() |
||
| 497 | { |
||
| 498 | $this->object->setProducts( $this->products ); |
||
| 499 | $this->object->setServices( $this->services ); |
||
| 500 | |||
| 501 | $result = $this->object->getTaxes(); |
||
| 502 | |||
| 508 | } |
||
| 509 | |||
| 510 | |||
| 511 | public function testGetCosts() |
||
| 512 | { |
||
| 513 | $this->object->setProducts( $this->products ); |
||
| 514 | $this->object->setServices( $this->services ); |
||
| 515 | |||
| 516 | $this->assertEquals( '3.11', round( $this->object->getCosts(), 2 ) ); |
||
| 517 | $this->assertEquals( '0.00', $this->object->getCosts( 'payment' ) ); |
||
| 518 | } |
||
| 519 | |||
| 520 | |||
| 521 | public function testCheck() |
||
| 522 | { |
||
| 523 | foreach( $this->products as $product ) { |
||
| 524 | $this->object->addProduct( $product ); |
||
| 525 | } |
||
| 526 | |||
| 527 | foreach( $this->addresses as $type => $addresses ) |
||
| 528 | { |
||
| 529 | foreach( $addresses as $address ) { |
||
| 530 | $this->object->addAddress( $address, $type ); |
||
| 531 | } |
||
| 532 | } |
||
| 533 | |||
| 534 | foreach( $this->services as $type => $services ) |
||
| 535 | { |
||
| 536 | foreach( $services as $service ) { |
||
| 537 | $this->object->addService( $service, $type ); |
||
| 538 | } |
||
| 539 | } |
||
| 540 | |||
| 541 | $result = $this->object->check( ['order/base/address', 'order/base/coupon', 'order/base/product', 'order/base/service'] ); |
||
| 542 | $this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $result ); |
||
| 543 | } |
||
| 544 | |||
| 545 | |||
| 546 | public function testCheckAllFailure() |
||
| 550 | } |
||
| 551 | |||
| 552 | |||
| 553 | public function testCheckProductsFailure() |
||
| 554 | { |
||
| 555 | $this->expectException( \Aimeos\MShop\Order\Exception::class ); |
||
| 556 | $this->object->check( ['order/base/product'] ); |
||
| 557 | } |
||
| 558 | |||
| 559 | |||
| 560 | /** |
||
| 561 | * @param string $code |
||
| 562 | */ |
||
| 563 | protected function createProduct( $code ) |
||
| 576 | } |
||
| 577 | } |
||
| 578 |