View source
<?php
namespace Drupal\Tests\commerce_ups\Unit;
use Drupal\commerce_shipping\Entity\ShippingMethodInterface;
use Drupal\commerce_ups\UPSRateRequest;
use Drupal\commerce_ups\UPSRateRequestInterface;
use Drupal\commerce_ups\UPSShipment;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\physical\LengthUnit;
use Drupal\physical\WeightUnit;
use Drupal\commerce_shipping\ShippingRate;
use Drupal\commerce_price\Price;
class UPSRateRequestTest extends UPSUnitTestBase {
protected $rateRequest;
public function setUp() {
parent::setUp();
$logger_factory = $this
->prophesize(LoggerChannelFactoryInterface::class);
$logger = $this
->prophesize(LoggerChannelInterface::class);
$logger_factory
->get(UPSRateRequestInterface::LOGGER_CHANNEL)
->willReturn($logger
->reveal());
$this->rateRequest = new UPSRateRequest(new UPSShipment(), $logger_factory
->reveal());
$this->rateRequest
->setConfig($this->configuration);
}
public function testAuth() {
$auth = $this->rateRequest
->getAuth();
$this
->assertEquals($auth['access_key'], $this->configuration['api_information']['access_key']);
$this
->assertEquals($auth['user_id'], $this->configuration['api_information']['user_id']);
$this
->assertEquals($auth['password'], $this->configuration['api_information']['password']);
}
public function testIntegrationMode() {
$mode = $this->rateRequest
->useIntegrationMode();
$this
->assertEquals(TRUE, $mode);
}
public function testRateType() {
$type = $this->rateRequest
->getRateType();
$this
->assertEquals(TRUE, $type);
}
public function testRateRequest($weight_unit, $length_unit, $send_from_usa) {
$shipment = $this
->mockShipment($weight_unit, $length_unit, $send_from_usa);
$shipping_method_plugin = $this
->mockShippingMethod();
$shipping_method_entity = $this
->prophesize(ShippingMethodInterface::class);
$shipping_method_entity
->id()
->willReturn('123456789');
$shipping_method_entity
->getPlugin()
->willReturn($shipping_method_plugin);
$rates = $this->rateRequest
->getRates($shipment, $shipping_method_entity
->reveal());
$this
->assertArrayHasKey(0, $rates);
foreach ($rates as $rate) {
assert($rate instanceof ShippingRate);
$this
->assertGreaterThan(0, $rate
->getAmount()
->getNumber());
$this
->assertGreaterThan(0, $rate
->getOriginalAmount()
->getNumber());
$this
->assertEquals($rate
->getAmount()
->getCurrencyCode(), $send_from_usa ? 'USD' : 'EUR');
$this
->assertNotEmpty($rate
->getService()
->getLabel());
$this
->assertEquals('123456789', $rate
->getShippingMethodId());
}
}
public function measurementUnitsDataProvider() {
$weight_units = [
WeightUnit::GRAM,
WeightUnit::KILOGRAM,
WeightUnit::OUNCE,
WeightUnit::POUND,
];
$length_units = [
LengthUnit::MILLIMETER,
LengthUnit::CENTIMETER,
LengthUnit::METER,
LengthUnit::INCH,
LengthUnit::FOOT,
];
foreach ($weight_units as $weight_unit) {
foreach ($length_units as $length_unit) {
(yield [
$weight_unit,
$length_unit,
TRUE,
]);
(yield [
$weight_unit,
$length_unit,
FALSE,
]);
}
}
}
}