class USPSInternationalRateRequestTest in Commerce USPS 8
Class USPSInternationalRateRequestTest.
@coversDefaultClass \Drupal\commerce_usps\USPSRateRequestInternational @group commerce_usps
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\commerce_usps\Unit\USPSUnitTestBase
- class \Drupal\Tests\commerce_usps\Unit\USPSInternationalRateRequestTest
- class \Drupal\Tests\commerce_usps\Unit\USPSUnitTestBase
Expanded class hierarchy of USPSInternationalRateRequestTest
File
- tests/
src/ Unit/ USPSInternationalRateRequestTest.php, line 17
Namespace
Drupal\Tests\commerce_usps\UnitView source
class USPSInternationalRateRequestTest extends USPSUnitTestBase {
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// Add the services to the config.
$this
->setConfig([
'services' => [
1,
2,
9,
],
]);
// Mock all the objects and set the config.
$event_dispatcher = new EventDispatcher();
$this->uspsShipment = new USPSShipmentInternational($event_dispatcher);
$this->rateRequest = new USPSRateRequestInternational($this->uspsShipment, $event_dispatcher);
$this->rateRequest
->setConfig($this
->getConfig());
}
/**
* Tests getRates().
*
* @covers ::getRates
* @covers ::buildRate
* @covers ::setMode
* @covers ::setShipment
* @covers ::resolveRates
*/
public function testGetRates() {
$config = $this
->getConfig();
$shipment = $this
->mockShipment([
'unit' => 'lb',
], [
'unit' => 'in',
], FALSE);
$shipping_method = $this
->prophesize(ShippingMethodInterface::class);
$shipping_method
->id()
->willReturn('123456789');
// Fetch rates from the USPS api.
$rates = $this->rateRequest
->getRates($shipment, $shipping_method
->reveal());
// Make sure the same number of rates requested
// is returned.
$this
->assertEquals(count($config['services']), count($rates));
/** @var \Drupal\commerce_shipping\ShippingRate $rate */
foreach ($rates as $rate) {
$this
->assertInstanceOf(ShippingRate::class, $rate);
$this
->assertNotEmpty($rate
->getAmount()
->getNumber());
}
}
/**
* Test getRates() with commercial rate response.
*
* @throws \Exception
*/
public function testCommercialRates() {
$shipment = $this
->mockShipment([
'unit' => 'lb',
], [
'unit' => 'in',
], FALSE);
$shipping_method = $this
->prophesize(ShippingMethodInterface::class);
$shipping_method
->id()
->willReturn('123456789');
// Fetch a retail rate first.
$config_update = [
'services' => [
1,
],
'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());
// Then fetch a commercial rate for the same service.
$config_update = [
'services' => [
1,
],
'rate_options' => [
'rate_class' => 'commercial_plus',
],
];
$this
->setConfig($config_update);
$config = $this
->getConfig();
// Pass the config to the rate and shipment services.
$this->uspsShipment
->setConfig($config);
$this->rateRequest
->setConfig($config);
$commercial_rates = $this->rateRequest
->getRates($shipment, $shipping_method
->reveal());
// Make sure both return rates.
$this
->assertEquals(count($retail_rates), count($commercial_rates));
/** @var \Drupal\commerce_shipping\ShippingRate $rate */
foreach ($commercial_rates as $delta => $commercial_rate) {
// Ensure a commercial rate was returned.
$this
->assertNotEmpty($commercial_rate
->getAmount()
->getNumber());
// Ensure the commercial rate is less than the retail rate.
$this
->assertLessThan($retail_rates[$delta]
->getAmount()
->getNumber(), $commercial_rate
->getAmount()
->getNumber());
}
}
/**
* Test package setup.
*
* @covers ::getPackages
*/
public function testGetPackages() {
$shipment = $this
->mockShipment([
'unit' => 'lb',
], [
'unit' => 'in',
], FALSE);
$this->rateRequest
->setShipment($shipment);
$packages = $this->rateRequest
->getPackages();
// TODO: Support multiple packages.
/** @var \USPS\RatePackage $package */
$package = reset($packages);
$info = $package
->getPackageInfo();
$this
->assertEquals(28806, $info['OriginZip']);
$this
->assertEquals('Great Britain and Northern Ireland', $info['Country']);
$this
->assertEquals(10, $info['Pounds']);
$this
->assertEquals(0, $info['Ounces']);
$this
->assertEquals('RECTANGULAR', $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']);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
USPSInternationalRateRequestTest:: |
public | function |
Overrides USPSUnitTestBase:: |
|
USPSInternationalRateRequestTest:: |
public | function | Test getRates() with commercial rate response. | |
USPSInternationalRateRequestTest:: |
public | function | Test package setup. | |
USPSInternationalRateRequestTest:: |
public | function | Tests getRates(). | |
USPSUnitTestBase:: |
protected | property | Configuration array. | |
USPSUnitTestBase:: |
protected | property | The USPS Rate Request class. | |
USPSUnitTestBase:: |
protected | property | The USPS shipment class. | |
USPSUnitTestBase:: |
public | function | Get the configuration array. | |
USPSUnitTestBase:: |
public | function | Creates a mock Drupal Commerce shipment entity. | |
USPSUnitTestBase:: |
public | function | Mocks a shipping method. | |
USPSUnitTestBase:: |
protected | function | Mocks the configuration array for tests. |