View source
<?php
namespace Drupal\Tests\commerce_usps\Unit;
use Drupal\commerce_shipping\Plugin\Commerce\ShippingMethod\ShippingMethodInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\physical\Length;
use Drupal\physical\Weight;
use Drupal\profile\Entity\ProfileInterface;
use CommerceGuys\Addressing\Address;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_shipping\Entity\ShipmentInterface;
use Drupal\commerce_shipping\Plugin\Commerce\PackageType\PackageTypeInterface;
use Drupal\commerce_store\Entity\StoreInterface;
abstract class USPSUnitTestBase extends UnitTestCase {
protected $configuration;
protected $rateRequest;
protected $uspsShipment;
protected function setUp() {
parent::setUp();
$this
->setConfig();
}
protected function setConfig(array $config = []) {
$this->configuration = $config + [
'api_information' => [
'user_id' => '972BLUEO5743',
'password' => '972BLUEO5743',
'mode' => 'live',
],
'rate_options' => [
'rate_class' => 'retail',
],
'default_package_type' => 'usps_small_flat_rate_box',
];
}
public function getConfig() {
return $this->configuration;
}
public function mockShipment($weight = [], $dimensions = [], $domestic = TRUE) {
$weight = $weight + [
'weight' => 10,
'unit' => 'lb',
];
$dimensions = $dimensions + [
'length' => 10,
'width' => 3,
'height' => 10,
'unit' => 'in',
];
$order = $this
->prophesize(OrderInterface::class);
$store = $this
->prophesize(StoreInterface::class);
$store
->getAddress()
->willReturn(new Address('US', 'NC', 'Asheville', '', 28806, '', '1025 Brevard Rd'));
$order
->getStore()
->willReturn($store
->reveal());
$shipment = $this
->prophesize(ShipmentInterface::class);
$profile = $this
->prophesize(ProfileInterface::class);
$address_list = $this
->prophesize(FieldItemListInterface::class);
if ($domestic) {
$address_list
->first()
->willReturn(new Address('US', 'CO', 'Morrison', '', 80465, '', '18300 W Alameda Pkwy'));
}
else {
$address_list
->first()
->willReturn(new Address('GB', 'London', 'Pimlico', '', 'SW1V 3EN', '', '113 Lupus St.'));
}
$profile
->get('address')
->willReturn($address_list
->reveal());
$shipment
->getShippingProfile()
->willReturn($profile
->reveal());
$shipment
->getOrder()
->willReturn($order
->reveal());
$package_type = $this
->prophesize(PackageTypeInterface::class);
$package_type
->getHeight()
->willReturn((new Length($dimensions['height'], 'in'))
->convert($dimensions['unit']));
$package_type
->getLength()
->willReturn((new Length($dimensions['length'], 'in'))
->convert($dimensions['unit']));
$package_type
->getWidth()
->willReturn((new Length($dimensions['width'], 'in'))
->convert($dimensions['unit']));
$package_type
->getRemoteId()
->willReturn('custom');
$shipment
->getWeight()
->willReturn((new Weight($weight['weight'], 'lb'))
->convert($weight['unit']));
$shipment
->getPackageType()
->willReturn($package_type
->reveal());
return $shipment
->reveal();
}
public function mockShippingMethod() {
$shipping_method = $this
->prophesize(ShippingMethodInterface::class);
$package_type = $this
->prophesize(PackageTypeInterface::class);
$package_type
->getHeight()
->willReturn(new Length(10, 'in'));
$package_type
->getLength()
->willReturn(new Length(10, 'in'));
$package_type
->getWidth()
->willReturn(new Length(3, 'in'));
$package_type
->getWeight()
->willReturn(new Weight(10, 'lb'));
$package_type
->getRemoteId()
->willReturn('custom');
$shipping_method
->getDefaultPackageType()
->willReturn($package_type);
return $shipping_method
->reveal();
}
}