|
15 | 15 | use Magento\Quote\Model\Quote\Address\RateRequest;
|
16 | 16 | use Magento\Shipping\Model\Shipment\Request;
|
17 | 17 | use Magento\Shipping\Model\Tracking\Result\Status;
|
| 18 | +use Magento\Store\Model\ScopeInterface; |
18 | 19 | use Magento\TestFramework\Helper\Bootstrap;
|
19 | 20 | use Magento\TestFramework\HTTP\AsyncClientInterfaceMock;
|
20 | 21 | use Magento\Shipping\Model\Simplexml\Element as ShippingElement;
|
21 | 22 |
|
22 | 23 | /**
|
23 | 24 | * Test for DHL integration.
|
| 25 | + * |
| 26 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
24 | 27 | */
|
25 | 28 | class CarrierTest extends \PHPUnit\Framework\TestCase
|
26 | 29 | {
|
@@ -411,7 +414,108 @@ private function getExpectedLabelRequestXml(
|
411 | 414 | */
|
412 | 415 | public function testCollectRates()
|
413 | 416 | {
|
414 |
| - $requestData = [ |
| 417 | + $requestData = $this->getRequestData(); |
| 418 | + //phpcs:disable Magento2.Functions.DiscouragedFunction |
| 419 | + $response = new Response( |
| 420 | + 200, |
| 421 | + [], |
| 422 | + file_get_contents(__DIR__ . '/../_files/dhl_quote_response.xml') |
| 423 | + ); |
| 424 | + //phpcs:enable Magento2.Functions.DiscouragedFunction |
| 425 | + $this->httpClient->nextResponses(array_fill(0, Carrier::UNAVAILABLE_DATE_LOOK_FORWARD + 1, $response)); |
| 426 | + /** @var RateRequest $request */ |
| 427 | + $request = Bootstrap::getObjectManager()->create(RateRequest::class, $requestData); |
| 428 | + $expectedRates = [ |
| 429 | + ['carrier' => 'dhl', 'carrier_title' => 'DHL Title', 'cost' => 45.85, 'method' => 'E', 'price' => 45.85], |
| 430 | + ['carrier' => 'dhl', 'carrier_title' => 'DHL Title', 'cost' => 35.26, 'method' => 'Q', 'price' => 35.26], |
| 431 | + ['carrier' => 'dhl', 'carrier_title' => 'DHL Title', 'cost' => 37.38, 'method' => 'Y', 'price' => 37.38], |
| 432 | + ['carrier' => 'dhl', 'carrier_title' => 'DHL Title', 'cost' => 35.26, 'method' => 'P', 'price' => 35.26] |
| 433 | + ]; |
| 434 | + |
| 435 | + $actualRates = $this->dhlCarrier->collectRates($request)->getAllRates(); |
| 436 | + |
| 437 | + self::assertEquals(count($expectedRates), count($actualRates)); |
| 438 | + foreach ($actualRates as $i => $actualRate) { |
| 439 | + $actualRate = $actualRate->getData(); |
| 440 | + unset($actualRate['method_title']); |
| 441 | + self::assertEquals($expectedRates[$i], $actualRate); |
| 442 | + } |
| 443 | + $requestXml = $this->httpClient->getLastRequest()->getBody(); |
| 444 | + self::assertContains('<Weight>18.223</Weight>', $requestXml); |
| 445 | + self::assertContains('<Height>0.63</Height>', $requestXml); |
| 446 | + self::assertContains('<Width>0.63</Width>', $requestXml); |
| 447 | + self::assertContains('<Depth>0.63</Depth>', $requestXml); |
| 448 | + } |
| 449 | + |
| 450 | + /** |
| 451 | + * Tests that quotes request doesn't contain dimensions when it shouldn't. |
| 452 | + * |
| 453 | + * @param string|null $size |
| 454 | + * @param string|null $height |
| 455 | + * @param string|null $width |
| 456 | + * @param string|null $depth |
| 457 | + * @magentoConfigFixture default_store carriers/dhl/active 1 |
| 458 | + * @dataProvider collectRatesWithoutDimensionsDataProvider |
| 459 | + */ |
| 460 | + public function testCollectRatesWithoutDimensions(?string $size, ?string $height, ?string $width, ?string $depth) |
| 461 | + { |
| 462 | + $requestData = $this->getRequestData(); |
| 463 | + $this->setDhlConfig(['size' => $size, 'height' => $height, 'width' => $width, 'depth' => $depth]); |
| 464 | + |
| 465 | + /** @var RateRequest $request */ |
| 466 | + $request = Bootstrap::getObjectManager()->create(RateRequest::class, $requestData); |
| 467 | + $this->dhlCarrier = Bootstrap::getObjectManager()->create(Carrier::class); |
| 468 | + $this->dhlCarrier->collectRates($request)->getAllRates(); |
| 469 | + |
| 470 | + $requestXml = $this->httpClient->getLastRequest()->getBody(); |
| 471 | + $this->assertNotContains('<Width>', $requestXml); |
| 472 | + $this->assertNotContains('<Height>', $requestXml); |
| 473 | + $this->assertNotContains('<Depth>', $requestXml); |
| 474 | + |
| 475 | + $this->config->reinit(); |
| 476 | + } |
| 477 | + |
| 478 | + /** |
| 479 | + * @return array |
| 480 | + */ |
| 481 | + public function collectRatesWithoutDimensionsDataProvider() |
| 482 | + { |
| 483 | + return [ |
| 484 | + ['size' => '0', 'height' => '1.1', 'width' => '0.6', 'depth' => '0.7'], |
| 485 | + ['size' => '1', 'height' => '', 'width' => '', 'depth' => ''], |
| 486 | + ['size' => null, 'height' => '1.1', 'width' => '0.6', 'depth' => '0.7'], |
| 487 | + ['size' => '1', 'height' => '1', 'width' => '', 'depth' => ''], |
| 488 | + ['size' => null, 'height' => null, 'width' => null, 'depth' => null], |
| 489 | + ]; |
| 490 | + } |
| 491 | + |
| 492 | + /** |
| 493 | + * Sets DHL config value. |
| 494 | + * |
| 495 | + * @param array $params |
| 496 | + * @return void |
| 497 | + */ |
| 498 | + private function setDhlConfig(array $params) |
| 499 | + { |
| 500 | + foreach ($params as $name => $val) { |
| 501 | + if ($val !== null) { |
| 502 | + $this->config->setValue( |
| 503 | + 'carriers/dhl/' . $name, |
| 504 | + $val, |
| 505 | + ScopeInterface::SCOPE_STORE |
| 506 | + ); |
| 507 | + } |
| 508 | + } |
| 509 | + } |
| 510 | + |
| 511 | + /** |
| 512 | + * Returns request data. |
| 513 | + * |
| 514 | + * @return array |
| 515 | + */ |
| 516 | + private function getRequestData(): array |
| 517 | + { |
| 518 | + return [ |
415 | 519 | 'data' => [
|
416 | 520 | 'dest_country_id' => 'DE',
|
417 | 521 | 'dest_region_id' => '82',
|
@@ -454,35 +558,5 @@ public function testCollectRates()
|
454 | 558 | 'all_items' => [],
|
455 | 559 | ]
|
456 | 560 | ];
|
457 |
| - //phpcs:disable Magento2.Functions.DiscouragedFunction |
458 |
| - $response = new Response( |
459 |
| - 200, |
460 |
| - [], |
461 |
| - file_get_contents(__DIR__ . '/../_files/dhl_quote_response.xml') |
462 |
| - ); |
463 |
| - //phpcs:enable Magento2.Functions.DiscouragedFunction |
464 |
| - $this->httpClient->nextResponses(array_fill(0, Carrier::UNAVAILABLE_DATE_LOOK_FORWARD + 1, $response)); |
465 |
| - /** @var RateRequest $request */ |
466 |
| - $request = Bootstrap::getObjectManager()->create(RateRequest::class, $requestData); |
467 |
| - $expectedRates = [ |
468 |
| - ['carrier' => 'dhl', 'carrier_title' => 'DHL Title', 'cost' => 45.85, 'method' => 'E', 'price' => 45.85], |
469 |
| - ['carrier' => 'dhl', 'carrier_title' => 'DHL Title', 'cost' => 35.26, 'method' => 'Q', 'price' => 35.26], |
470 |
| - ['carrier' => 'dhl', 'carrier_title' => 'DHL Title', 'cost' => 37.38, 'method' => 'Y', 'price' => 37.38], |
471 |
| - ['carrier' => 'dhl', 'carrier_title' => 'DHL Title', 'cost' => 35.26, 'method' => 'P', 'price' => 35.26] |
472 |
| - ]; |
473 |
| - |
474 |
| - $actualRates = $this->dhlCarrier->collectRates($request)->getAllRates(); |
475 |
| - |
476 |
| - self::assertEquals(count($expectedRates), count($actualRates)); |
477 |
| - foreach ($actualRates as $i => $actualRate) { |
478 |
| - $actualRate = $actualRate->getData(); |
479 |
| - unset($actualRate['method_title']); |
480 |
| - self::assertEquals($expectedRates[$i], $actualRate); |
481 |
| - } |
482 |
| - $requestXml = $this->httpClient->getLastRequest()->getBody(); |
483 |
| - self::assertContains('<Weight>18.223</Weight>', $requestXml); |
484 |
| - self::assertContains('<Height>0.630</Height>', $requestXml); |
485 |
| - self::assertContains('<Width>0.630</Width>', $requestXml); |
486 |
| - self::assertContains('<Depth>0.630</Depth>', $requestXml); |
487 | 561 | }
|
488 | 562 | }
|
0 commit comments