View source
<?php
namespace Drupal\Tests\commerce_ups\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 Ups\Entity\PackagingType;
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 UPSUnitTestBase extends UnitTestCase {
protected $configuration;
protected function setUp() {
parent::setUp();
$this->configuration = [
'api_information' => [
'access_key' => '5D3096C1F3C7953D',
'user_id' => 'drupal_commerce',
'password' => 'DCDev101!',
'mode' => 'test',
],
'rate_options' => [
'rate_type' => TRUE,
],
'services' => [
"01" => "01",
"02" => "02",
"03" => "03",
"07" => "07",
"08" => "08",
"11" => "11",
"12" => "12",
"13" => "13",
"14" => "14",
"54" => "54",
"59" => "59",
"65" => "65",
"70" => "70",
],
];
}
public function mockShipment($weight_unit = 'lb', $length_unit = 'in', $send_form_usa = TRUE) {
$order = $this
->prophesize(OrderInterface::class);
$store = $this
->prophesize(StoreInterface::class);
if ($send_form_usa) {
$store
->getAddress()
->willReturn(new Address('US', 'NC', 'Asheville', '', 28806, '', '1025 Brevard Rd'));
}
else {
$store
->getAddress()
->willReturn(new Address('DE', '', 'Darmstadt', '', 64283, '', 'Schlossgraben 1'));
}
$order
->getStore()
->willReturn($store
->reveal());
$shipment = $this
->prophesize(ShipmentInterface::class);
$profile = $this
->prophesize(ProfileInterface::class);
$address_list = $this
->prophesize(FieldItemListInterface::class);
$address_list
->first()
->willReturn(new Address('US', 'NC', 'Asheville', '', 28806, '', '1025 Brevard Rd'));
$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(10, 'in'))
->convert($length_unit));
$package_type
->getLength()
->willReturn((new Length(10, 'in'))
->convert($length_unit));
$package_type
->getWidth()
->willReturn((new Length(3, 'in'))
->convert($length_unit));
$package_type
->getRemoteId()
->willReturn(PackagingType::PT_UNKNOWN);
$shipment
->getWeight()
->willReturn((new Weight(10, '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();
}
}