View source
<?php
namespace Drupal\Tests\commerce_usps\Unit;
use Drupal\commerce_shipping\Entity\ShippingMethodInterface;
use Drupal\commerce_shipping\ShippingRate;
use Drupal\commerce_usps\USPSShipment;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Drupal\commerce_usps\USPSRateRequest;
class USPSRateRequestTest extends USPSUnitTestBase {
public function setUp() {
parent::setUp();
$this
->setConfig([
'services' => [
1,
2,
3,
4,
6,
7,
],
]);
$event_dispatcher = new EventDispatcher();
$this->uspsShipment = new USPSShipment($event_dispatcher);
$this->rateRequest = new USPSRateRequest($this->uspsShipment, $event_dispatcher);
$this->rateRequest
->setConfig($this
->getConfig());
}
public function testGetRates() {
$config = $this
->getConfig();
$shipment = $this
->mockShipment();
$shipping_method = $this
->prophesize(ShippingMethodInterface::class);
$shipping_method
->id()
->willReturn('123456789');
$rates = $this->rateRequest
->getRates($shipment, $shipping_method
->reveal());
$this
->assertEquals(count($config['services']), count($rates));
foreach ($rates as $rate) {
$this
->assertInstanceOf(ShippingRate::class, $rate);
$this
->assertNotEmpty($rate
->getAmount()
->getNumber());
}
}
public function testCommercialRates() {
$shipment = $this
->mockShipment();
$shipping_method = $this
->prophesize(ShippingMethodInterface::class);
$shipping_method
->id()
->willReturn('123456789');
$config_update = [
'services' => [
3,
],
'rate_options' => [
'rate_class' => 'retail',
],
];
$this
->setConfig($config_update);
$config = $this
->getConfig();
$this->uspsShipment
->setConfig($config);
$this->rateRequest
->setConfig($config);
$retail_rates = $this->rateRequest
->getRates($shipment, $shipping_method
->reveal());
$config_update = [
'services' => [
3,
],
'rate_options' => [
'rate_class' => 'commercial',
],
];
$this
->setConfig($config_update);
$config = $this
->getConfig();
$this->uspsShipment
->setConfig($config);
$this->rateRequest
->setConfig($config);
$commercial_rates = $this->rateRequest
->getRates($shipment, $shipping_method
->reveal());
$this
->assertEquals(count($retail_rates), count($commercial_rates));
foreach ($commercial_rates as $delta => $commercial_rate) {
$this
->assertNotEmpty($commercial_rate
->getAmount()
->getNumber());
$this
->assertLessThan($retail_rates[$delta]
->getAmount()
->getNumber(), $commercial_rate
->getAmount()
->getNumber());
}
}
public function testSingleRate() {
$this
->setConfig([
'services' => [
9,
],
]);
$config = $this
->getConfig();
$shipment = $this
->mockShipment([], [
'length' => 30,
'width' => 20,
'height' => 20,
]);
$shipping_method = $this
->prophesize(ShippingMethodInterface::class);
$shipping_method
->id()
->willReturn('123456789');
$rates = $this->rateRequest
->getRates($shipment, $shipping_method
->reveal());
$this
->assertEquals(count($config['services']), count($rates));
foreach ($rates as $rate) {
$this
->assertInstanceOf(ShippingRate::class, $rate);
$this
->assertNotEmpty($rate
->getAmount()
->getNumber());
}
}
public function testCleanServiceName() {
$service = 'Priority Mail Express 2-Day<sup>™</sup>';
$cleaned = $this->rateRequest
->cleanServiceName($service);
$this
->assertEquals('Priority Mail Express 2-Day', $cleaned);
}
public function testGetPackages() {
$this->rateRequest
->setShipment($this
->mockShipment());
$packages = $this->rateRequest
->getPackages();
$package = reset($packages);
$info = $package
->getPackageInfo();
$this
->assertEquals(28806, $info['ZipOrigination']);
$this
->assertEquals(80465, $info['ZipDestination']);
$this
->assertEquals('ALL', $info['Service']);
$this
->assertEquals(10, $info['Pounds']);
$this
->assertEquals(0, $info['Ounces']);
$this
->assertEquals('VARIABLE', $info['Container']);
$this
->assertEquals('REGULAR', $info['Size']);
$this
->assertEquals(3, $info['Width']);
$this
->assertEquals(10, $info['Length']);
$this
->assertEquals(10, $info['Height']);
$this
->assertEquals(0, $info['Girth']);
$this
->assertEquals(TRUE, $info['Machinable']);
$this
->assertEquals(date('Y-m-d'), $info['ShipDate']);
}
}