123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Usps\Test\Unit\Model;
- use Magento\Catalog\Model\ResourceModel\Product\Collection;
- use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Framework\DataObject;
- use Magento\Framework\HTTP\ZendClient;
- use Magento\Framework\HTTP\ZendClientFactory;
- use Magento\Framework\Locale\ResolverInterface;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Framework\Xml\Security;
- use Magento\Quote\Model\Quote\Address\RateRequest;
- use Magento\Quote\Model\Quote\Address\RateResult\Error;
- use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
- use Magento\Quote\Model\Quote\Address\RateResult\Method;
- use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;
- use Magento\Shipping\Helper\Carrier as CarrierHelper;
- use Magento\Shipping\Model\Rate\Result;
- use Magento\Shipping\Model\Rate\ResultFactory;
- use Magento\Shipping\Model\Shipment\ReturnShipment;
- use Magento\Shipping\Model\Simplexml\Element;
- use Magento\Shipping\Model\Simplexml\ElementFactory;
- use Magento\Usps\Helper\Data as DataHelper;
- use Magento\Usps\Model\Carrier;
- use PHPUnit_Framework_MockObject_MockObject as MockObject;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class CarrierTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Zend_Http_Response|MockObject
- */
- private $httpResponse;
- /**
- * @var ObjectManager
- */
- private $objectManager;
- /**
- * @var Error|MockObject
- */
- private $error;
- /**
- * @var ErrorFactory|MockObject
- */
- private $errorFactory;
- /**
- * @var Carrier|MockObject
- */
- private $carrier;
- /**
- * @var ScopeConfigInterface|MockObject
- */
- private $scope;
- /**
- * @var DataHelper|MockObject
- */
- private $dataHelper;
- /**
- * @var ZendClient|MockObject
- */
- private $httpClient;
- /**
- * @inheritdoc
- */
- protected function setUp()
- {
- $this->objectManager = new ObjectManager($this);
- $this->scope = $this->getMockBuilder(ScopeConfigInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->scope->method('getValue')
- ->willReturnCallback([$this, 'scopeConfiggetValue']);
- $xmlElFactory = $this->getXmlFactory();
- $rateFactory = $this->getRateFactory();
- $rateMethodFactory = $this->getRateMethodFactory();
- $httpClientFactory = $this->getHttpClientFactory();
- $data = ['id' => 'usps', 'store' => '1'];
- $this->error = $this->getMockBuilder(Error::class)
- ->setMethods(['setCarrier', 'setCarrierTitle', 'setErrorMessage'])
- ->getMock();
- $this->errorFactory = $this->getMockBuilder(ErrorFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->errorFactory->expects($this->any())->method('create')->willReturn($this->error);
- $carrierHelper = $this->getCarrierHelper();
- $productCollectionFactory = $this->getProductCollectionFactory();
- $arguments = [
- 'scopeConfig' => $this->scope,
- 'xmlSecurity' => new Security(),
- 'xmlElFactory' => $xmlElFactory,
- 'rateFactory' => $rateFactory,
- 'rateMethodFactory' => $rateMethodFactory,
- 'httpClientFactory' => $httpClientFactory,
- 'data' => $data,
- 'rateErrorFactory' => $this->errorFactory,
- 'carrierHelper' => $carrierHelper,
- 'productCollectionFactory' => $productCollectionFactory,
- 'dataHelper' => $this->dataHelper,
- ];
- $this->dataHelper = $this->getMockBuilder(DataHelper::class)
- ->disableOriginalConstructor()
- ->setMethods(['displayGirthValue'])
- ->getMock();
- $this->carrier = $this->objectManager->getObject(Carrier::class, $arguments);
- }
- /**
- * @dataProvider codeDataProvider
- */
- public function testGetCodeArray($code)
- {
- $this->assertNotEmpty($this->carrier->getCode($code));
- }
- public function testGetCodeBool()
- {
- $this->assertFalse($this->carrier->getCode('test_code'));
- }
- public function testCollectRates()
- {
- $expectedRequest = '<?xml version="1.0" encoding="UTF-8"?><RateV4Request USERID="213MAGEN6752">'
- . '<Revision>2</Revision><Package ID="0"><Service>ALL</Service><ZipOrigination/>'
- . '<ZipDestination>90032</ZipDestination><Pounds>4</Pounds><Ounces>4.2512000000</Ounces>'
- . '<Container>VARIABLE</Container><Size>REGULAR</Size><Machinable/></Package></RateV4Request>';
- $expectedXml = new \SimpleXMLElement($expectedRequest);
- $this->scope->method('isSetFlag')
- ->willReturn(true);
- $this->httpClient->expects(self::exactly(2))
- ->method('setParameterGet')
- ->withConsecutive(
- ['API', 'RateV4'],
- ['XML', self::equalTo($expectedXml->asXML())]
- );
- $this->httpResponse->method('getBody')
- ->willReturn(file_get_contents(__DIR__ . '/_files/success_usps_response_rates.xml'));
- $data = require __DIR__ . '/_files/rates_request_data.php';
- $request = $this->objectManager->getObject(RateRequest::class, ['data' => $data[0]]);
- self::assertNotEmpty($this->carrier->collectRates($request)->getAllRates());
- }
- public function testCollectRatesWithUnavailableService()
- {
- $expectedCount = 5;
- $this->scope->expects($this->once())
- ->method('isSetFlag')
- ->willReturn(true);
- $this->httpResponse->expects($this->once())
- ->method('getBody')
- ->willReturn(file_get_contents(__DIR__ . '/_files/response_rates.xml'));
- $data = require __DIR__ . '/_files/rates_request_data.php';
- $request = $this->objectManager->getObject(RateRequest::class, ['data' => $data[1]]);
- $rates = $this->carrier->collectRates($request)->getAllRates();
- $this->assertEquals($expectedCount, count($rates));
- }
- public function testReturnOfShipment()
- {
- $this->httpResponse->method('getBody')
- ->willReturn(file_get_contents(__DIR__ . '/_files/success_usps_response_return_shipment.xml'));
- $request = $this->objectManager->getObject(
- ReturnShipment::class,
- require __DIR__ . '/_files/return_shipment_request_data.php'
- );
- $this->httpClient->expects(self::exactly(2))
- ->method('setParameterGet')
- ->withConsecutive(
- ['API', 'SignatureConfirmationCertifyV3'],
- ['XML', $this->stringContains('<WeightInOunces>80</WeightInOunces>')]
- );
- $this->assertNotEmpty($this->carrier->returnOfShipment($request)->getInfo()[0]['tracking_number']);
- }
- public function testFormattingFloatValuesForIntlShipmentRequest()
- {
- $this->httpResponse->method('getBody')
- ->willReturn(
- file_get_contents(__DIR__ . '/_files/success_usps_response_return_shipment.xml')
- );
- $request = $this->objectManager->getObject(
- ReturnShipment::class,
- require __DIR__ . '/_files/return_shipment_request_data.php'
- );
- $request->setRecipientAddressCountryCode('UK');
- $formattedValuesRegex = '(<Value>5.00<\/Value>).*';
- $formattedValuesRegex .= '(<NetOunces>0.00<\/NetOunces>)';
- $this->httpClient->expects($this->exactly(2))
- ->method('setParameterGet')
- ->withConsecutive(
- ['API', 'ExpressMailIntl'],
- ['XML', $this->matchesRegularExpression('/' . $formattedValuesRegex . '/')]
- );
- $this->carrier->returnOfShipment($request);
- }
- /**
- * Emulates the config's `getValue` method.
- *
- * @param $path
- * @return string|string
- */
- public function scopeConfigGetValue($path)
- {
- $pathMap = [
- 'carriers/usps/allowed_methods' => '0_FCLE,0_FCL,0_FCP,1,2,3,4,6,7,13,16,17,22,23,25,27,28,33,' .
- '34,35,36,37,42,43,53,55,56,57,61,INT_1,INT_2,INT_4,INT_6,INT_7,INT_8,INT_9,INT_10,INT_11,' .
- 'INT_12,INT_13,INT_14,INT_15,INT_16,INT_20,INT_26',
- 'carriers/usps/showmethod' => 1,
- 'carriers/usps/debug' => 1,
- 'carriers/usps/userid' => 'test',
- 'carriers/usps/mode' => 0,
- ];
- return isset($pathMap[$path]) ? $pathMap[$path] : null;
- }
- /**
- * @return array
- */
- public function codeDataProvider()
- {
- return [['container'], ['machinable'], ['method'], ['size']];
- }
- public function testCollectRatesErrorMessage()
- {
- $this->scope->method('isSetFlag')
- ->willReturn(false);
- $this->error->method('setCarrier')
- ->with('usps');
- $this->error->expects($this->once())
- ->method('setCarrierTitle');
- $this->error->expects($this->once())
- ->method('setErrorMessage');
- $request = new RateRequest();
- $this->assertSame($this->error, $this->carrier->collectRates($request));
- }
- public function testCollectRatesFail()
- {
- $this->scope->method('isSetFlag')
- ->willReturn(true);
- $this->scope->expects($this->atLeastOnce())
- ->method('getValue')
- ->willReturnMap(
- [
- ['carriers/usps/userid' => 123],
- ['carriers/usps/container' => 11],
- ]
- );
- $request = new RateRequest();
- $request->setPackageWeight(1);
- $this->assertNotEmpty($this->carrier->collectRates($request));
- }
- /**
- * @param string $data
- * @param array $maskFields
- * @param string $expected
- * @dataProvider logDataProvider
- */
- public function testFilterDebugData($data, array $maskFields, $expected)
- {
- $refClass = new \ReflectionClass(Carrier::class);
- $property = $refClass->getProperty('_debugReplacePrivateDataKeys');
- $property->setAccessible(true);
- $property->setValue($this->carrier, $maskFields);
- $refMethod = $refClass->getMethod('filterDebugData');
- $refMethod->setAccessible(true);
- $result = $refMethod->invoke($this->carrier, $data);
- $expectedXml = new \SimpleXMLElement($expected);
- $resultXml = new \SimpleXMLElement($result);
- $this->assertEquals($expectedXml->asXML(), $resultXml->asXML());
- }
- /**
- * Get list of variations
- */
- public function logDataProvider()
- {
- return [
- [
- '<?xml version="1.0" encoding="UTF-8"?>
- <RateRequest USERID="12312">
- <Package ID="0">
- <Service>ALL</Service>
- </Package>
- </RateRequest>',
- ['USERID'],
- '<?xml version="1.0" encoding="UTF-8"?>
- <RateRequest USERID="****">
- <Package ID="0">
- <Service>ALL</Service>
- </Package>
- </RateRequest>',
- ],
- ];
- }
- /**
- * @param string $countyCode
- * @param string $carrierMethodCode
- * @param bool $displayGirthValueResult
- * @param bool $result
- * @dataProvider isGirthAllowedDataProvider
- */
- public function testIsGirthAllowed($countyCode, $carrierMethodCode, $displayGirthValueResult, $result)
- {
- $this->dataHelper->method('displayGirthValue')
- ->with($carrierMethodCode)
- ->willReturn($displayGirthValueResult);
- $this->assertEquals($result, $this->carrier->isGirthAllowed($countyCode, $carrierMethodCode));
- }
- /**
- * @return array
- */
- public function isGirthAllowedDataProvider()
- {
- return [
- ['US', 'usps_1', true, false],
- ['UK', 'usps_1', true, true],
- ['US', 'usps_0', false, true],
- ];
- }
- /**
- * @return MockObject
- */
- private function getXmlFactory(): MockObject
- {
- $xmlElFactory = $this->getMockBuilder(ElementFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $xmlElFactory->method('create')
- ->willReturnCallback(
- function ($data) {
- $helper = new ObjectManager($this);
- return $helper->getObject(
- Element::class,
- ['data' => $data['data']]
- );
- }
- );
- return $xmlElFactory;
- }
- /**
- * @return MockObject
- */
- private function getRateFactory(): MockObject
- {
- $rateFactory = $this->getMockBuilder(ResultFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $rateResult = $this->getMockBuilder(Result::class)
- ->disableOriginalConstructor()
- ->setMethods(null)
- ->getMock();
- $rateFactory->method('create')
- ->willReturn($rateResult);
- return $rateFactory;
- }
- /**
- * @return MockObject
- */
- private function getRateMethodFactory(): MockObject
- {
- $rateMethodFactory = $this->getMockBuilder(MethodFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $rateMethod = $this->getMockBuilder(Method::class)
- ->disableOriginalConstructor()
- ->setMethods(['setPrice'])
- ->getMock();
- $rateMethod->method('setPrice')
- ->willReturnSelf();
- $rateMethodFactory->method('create')
- ->willReturn($rateMethod);
- return $rateMethodFactory;
- }
- /**
- * @return MockObject
- */
- private function getHttpClientFactory(): MockObject
- {
- $this->httpResponse = $this->getMockBuilder(\Zend_Http_Response::class)
- ->disableOriginalConstructor()
- ->setMethods(['getBody'])
- ->getMock();
- $this->httpClient = $this->getMockBuilder(ZendClient::class)
- ->getMock();
- $this->httpClient->method('request')
- ->willReturn($this->httpResponse);
- $httpClientFactory = $this->getMockBuilder(ZendClientFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $httpClientFactory->method('create')
- ->willReturn($this->httpClient);
- return $httpClientFactory;
- }
- /**
- * @return MockObject
- */
- private function getProductCollectionFactory(): MockObject
- {
- $productCollection = $this->getMockBuilder(Collection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $productCollection->method('addStoreFilter')
- ->willReturnSelf();
- $productCollection->method('addFieldToFilter')
- ->willReturnSelf();
- $productCollection->method('addAttributeToSelect')
- ->willReturn([]);
- $productCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $productCollectionFactory->method('create')
- ->willReturn($productCollection);
- return $productCollectionFactory;
- }
- /**
- * @return CarrierHelper
- */
- private function getCarrierHelper(): CarrierHelper
- {
- $localeResolver = $this->getMockForAbstractClass(ResolverInterface::class);
- $localeResolver->method('getLocale')->willReturn('fr_FR');
- $carrierHelper = $this->objectManager->getObject(
- CarrierHelper::class,
- [
- 'localeResolver' => $localeResolver,
- ]
- );
- return $carrierHelper;
- }
- }
|