class UPSRateRequestTest in Commerce UPS 8.3
Class UPSRateRequestTest.
@coversDefaultClass \Drupal\commerce_ups\UPSRateRequest @group commerce_ups
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\commerce_ups\Unit\UPSUnitTestBase
- class \Drupal\Tests\commerce_ups\Unit\UPSRateRequestTest
- class \Drupal\Tests\commerce_ups\Unit\UPSUnitTestBase
Expanded class hierarchy of UPSRateRequestTest
File
- tests/
src/ Unit/ UPSRateRequestTest.php, line 22
Namespace
Drupal\Tests\commerce_ups\UnitView source
class UPSRateRequestTest extends UPSUnitTestBase {
/**
* A UPS rate request object.
*
* @var \Drupal\commerce_ups\UPSRateRequest
*/
protected $rateRequest;
/**
* Set up requirements for test.
*/
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);
}
/**
* Test getAuth response.
*
* @covers ::getAuth
*/
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']);
}
/**
* Test useIntegrationMode().
*
* @covers ::useIntegrationMode
*/
public function testIntegrationMode() {
$mode = $this->rateRequest
->useIntegrationMode();
$this
->assertEquals(TRUE, $mode);
}
/**
* Test getRateType().
*
* @covers ::getRateType
*/
public function testRateType() {
$type = $this->rateRequest
->getRateType();
$this
->assertEquals(TRUE, $type);
}
/**
* Test rate requests return valid rates.
*
* @param string $weight_unit
* Weight unit.
* @param string $length_unit
* Length unit.
* @param bool $send_from_usa
* Whether the shipment should be sent from USA.
*
* @covers ::getRates
*
* @dataProvider measurementUnitsDataProvider
*/
public function testRateRequest($weight_unit, $length_unit, $send_from_usa) {
// Invoke the rate request object.
$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());
// Make sure at least one rate was returned.
$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());
}
}
/**
* Data provider for testRateRequest()
*/
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,
]);
}
}
}
}
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. | |
UPSRateRequestTest:: |
protected | property | A UPS rate request object. | |
UPSRateRequestTest:: |
public | function | Data provider for testRateRequest() | |
UPSRateRequestTest:: |
public | function |
Set up requirements for test. Overrides UPSUnitTestBase:: |
|
UPSRateRequestTest:: |
public | function | Test getAuth response. | |
UPSRateRequestTest:: |
public | function | Test useIntegrationMode(). | |
UPSRateRequestTest:: |
public | function | Test rate requests return valid rates. | |
UPSRateRequestTest:: |
public | function | Test getRateType(). | |
UPSUnitTestBase:: |
protected | property | A shipping method configuration array. | |
UPSUnitTestBase:: |
public | function | Creates a mock Drupal Commerce shipment entity. | |
UPSUnitTestBase:: |
public | function | Creates a mock Drupal Commerce shipping method. |