123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- <?php
- /**
- * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
- */
- namespace Temando\Shipping\Rest;
- use Magento\TestFramework\Helper\Bootstrap;
- use Magento\TestFramework\ObjectManager;
- use Temando\Shipping\Rest\Adapter as RestAdapter;
- use Temando\Shipping\Rest\Request\AuthRequestInterface;
- use Temando\Shipping\Rest\Request\ItemRequestInterface;
- use Temando\Shipping\Rest\Request\ListRequestInterface;
- use Temando\Shipping\Rest\Request\OrderRequest;
- use Temando\Shipping\Rest\Request\Type\OrderRequestType;
- use Temando\Shipping\Rest\Response\DataObject\CarrierIntegration;
- use Temando\Shipping\Rest\Response\DataObject\Completion;
- use Temando\Shipping\Rest\Response\DataObject\Container;
- use Temando\Shipping\Rest\Response\DataObject\Location;
- use Temando\Shipping\Rest\Response\DataObject\Session;
- use Temando\Shipping\Rest\Response\DataObject\Shipment;
- use Temando\Shipping\Rest\Response\Document\SaveOrderInterface;
- use Temando\Shipping\Test\Integration\Provider\RestResponseProvider;
- use Temando\Shipping\Webservice\Filter\CollectionFilter;
- use Temando\Shipping\Webservice\HttpClient;
- use Temando\Shipping\Webservice\HttpClientInterfaceFactory;
- /**
- * AdapterTest
- *
- * @magentoAppIsolation enabled
- * @markTestIncomplete
- *
- * @package Temando\Shipping\Test
- * @author Christoph Aßmann <christoph.assmann@netresearch.de>
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
- * @link http://www.temando.com/
- */
- class AdapterTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var ObjectManager
- */
- private $objectManager;
- /**
- * @var RestClient|\PHPUnit_Framework_MockObject_MockObject
- */
- private $restClient;
- /**
- * @var HttpClient|\PHPUnit_Framework_MockObject_MockObject
- */
- private $httpClient;
- /**
- * @var Authentication|\PHPUnit_Framework_MockObject_MockObject
- */
- private $auth;
- /**
- * Delegate provisioning of test data to separate class
- * @return string[]
- */
- public function startSessionResponseDataProvider()
- {
- return RestResponseProvider::startSessionResponseDataProvider();
- }
- /**
- * Delegate provisioning of test data to separate class
- * @return string[]
- */
- public function getCarriersResponseDataProvider()
- {
- return RestResponseProvider::getCarriersResponseDataProvider();
- }
- /**
- * Delegate provisioning of test data to separate class
- * @return string[]
- */
- public function getLocationsResponseDataProvider()
- {
- return RestResponseProvider::getLocationsResponseDataProvider();
- }
- /**
- * Delegate provisioning of test data to separate class
- * @return string[]
- */
- public function getContainersResponseDataProvider()
- {
- return RestResponseProvider::getContainersResponseDataProvider();
- }
- /**
- * Delegate provisioning of test data to separate class
- * @return string[]
- */
- public function getCompletionsDataProvider()
- {
- return RestResponseProvider::getCompletionsResponseDataProvider();
- }
- /**
- * Delegate provisioning of test data to separate class
- * @return string[]
- */
- public function getCompletionDataProvider()
- {
- return RestResponseProvider::getCompletionResponseDataProvider();
- }
- /**
- * Delegate provisioning of test data to separate class
- * @return string[]
- */
- public function getShipmentResponseDataProvider()
- {
- return RestResponseProvider::getShipmentResponseDataProvider();
- }
- /**
- * Delegate provisioning of test data to separate class
- * @return string[]
- */
- public function manifestOrderResponseProvider()
- {
- return RestResponseProvider::manifestOrderResponseProvider();
- }
- /**
- * Delegate provisioning of test data to separate class
- * @return string[]
- */
- public function updateOrderResponseProvider()
- {
- return RestResponseProvider::updateOrderResponseProvider();
- }
- /**
- * Init object manager
- */
- protected function setUp()
- {
- parent::setUp();
- $this->objectManager = Bootstrap::getObjectManager();
- $this->auth = $this->getMockBuilder(Authentication::class)
- ->disableOriginalConstructor()
- ->setMethods(['connect', 'getSessionToken'])
- ->getMock();
- $this->httpClient = $this->getMockBuilder(HttpClient::class)
- ->setMethods(['send'])
- ->setConstructorArgs(['client' => new \Zend\Http\Client()])
- ->getMock();
- $clientFactoryMock = $this->getMockBuilder(HttpClientInterfaceFactory::class)
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $clientFactoryMock
- ->expects($this->once())
- ->method('create')
- ->willReturn($this->httpClient);
- $this->restClient = $this->objectManager->create(RestClient::class, [
- 'httpClientFactory' => $clientFactoryMock,
- ]);
- }
- /**
- * @test
- *
- * @dataProvider startSessionResponseDataProvider
- * @magentoConfigFixture default/carriers/temando/logging_enabled 0
- * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
- * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
- *
- * @param string $jsonResponse
- */
- public function startSession($jsonResponse)
- {
- $this->httpClient
- ->expects($this->once())
- ->method('send')
- ->willReturn($jsonResponse);
- /** @var AuthRequestInterface $request */
- $request = $this->objectManager->create(AuthRequestInterface::class, [
- 'username' => '',
- 'password' => '',
- 'accountId' => 'foo',
- 'bearerToken' => 'bar',
- 'scope' => AuthenticationInterface::AUTH_SCOPE_ADMIN,
- ]);
- /** @var AuthAdapter $adapter */
- $adapter = $this->objectManager->create(AuthAdapter::class, [
- 'restClient' => $this->restClient,
- ]);
- $session = $adapter->startSession($request);
- $this->assertInstanceOf(Session::class, $session);
- $this->assertNotEmpty($session->getAttributes()->getSessionToken());
- $this->assertNotEmpty($session->getAttributes()->getExpiry());
- }
- /**
- * @test
- * @dataProvider getCarriersResponseDataProvider
- * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
- * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
- * @param string $jsonResponse
- */
- public function getCarriers($jsonResponse)
- {
- $this->httpClient
- ->expects($this->once())
- ->method('send')
- ->willReturn($jsonResponse);
- /** @var ListRequestInterface $request */
- $request = $this->objectManager->create(ListRequestInterface::class, ['offset' => 0, 'limit' => 20]);
- /** @var RestAdapter $adapter */
- $adapter = $this->objectManager->create(RestAdapter::class, [
- 'auth' => $this->auth,
- 'restClient' => $this->restClient,
- ]);
- $carriers = $adapter->getCarrierIntegrations($request);
- $this->assertInternalType('array', $carriers);
- $this->assertContainsOnly(CarrierIntegration::class, $carriers);
- }
- /**
- * @test
- * @dataProvider getLocationsResponseDataProvider
- * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
- * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
- * @param string $jsonResponse
- */
- public function getLocations($jsonResponse)
- {
- $this->httpClient
- ->expects($this->once())
- ->method('send')
- ->willReturn($jsonResponse);
- /** @var ListRequestInterface $request */
- $request = $this->objectManager->create(ListRequestInterface::class, ['offset' => 0, 'limit' => 20]);
- /** @var RestAdapter $adapter */
- $adapter = $this->objectManager->create(RestAdapter::class, [
- 'auth' => $this->auth,
- 'restClient' => $this->restClient,
- ]);
- $locations = $adapter->getLocations($request);
- $this->assertInternalType('array', $locations);
- $this->assertContainsOnly(Location::class, $locations);
- }
- /**
- * @test
- * @dataProvider getContainersResponseDataProvider
- * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
- * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
- * @param string $jsonResponse
- */
- public function getContainers($jsonResponse)
- {
- $this->httpClient
- ->expects($this->once())
- ->method('send')
- ->willReturn($jsonResponse);
- /** @var ListRequestInterface $request */
- $request = $this->objectManager->create(ListRequestInterface::class, ['offset' => 0, 'limit' => 20]);
- /** @var RestAdapter $adapter */
- $adapter = $this->objectManager->create(RestAdapter::class, [
- 'auth' => $this->auth,
- 'restClient' => $this->restClient,
- ]);
- $containers = $adapter->getContainers($request);
- $this->assertInternalType('array', $containers);
- $this->assertContainsOnly(Container::class, $containers);
- }
- /**
- * @test
- * @dataProvider getCompletionsDataProvider
- * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
- * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
- * @param string $jsonResponse
- */
- public function getCompletions($jsonResponse)
- {
- $this->httpClient
- ->expects($this->once())
- ->method('send')
- ->willReturn($jsonResponse);
- /** @var ListRequestInterface $request */
- $request = $this->objectManager->create(ListRequestInterface::class, [
- 'offset' => 0,
- 'limit' => 20,
- 'filter' => $this->objectManager->create(CollectionFilter::class, ['filters' => ['foo' => 'bar']]),
- ]);
- /** @var RestAdapter $adapter */
- $adapter = $this->objectManager->create(RestAdapter::class, [
- 'auth' => $this->auth,
- 'restClient' => $this->restClient,
- ]);
- $completions = $adapter->getCompletions($request);
- $this->assertInternalType('array', $completions);
- $this->assertContainsOnly(Completion::class, $completions);
- }
- /**
- * @test
- * @dataProvider getCompletionDataProvider
- * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
- * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
- * @param string $jsonResponse
- */
- public function getCompletion($jsonResponse)
- {
- $this->httpClient
- ->expects($this->once())
- ->method('send')
- ->willReturn($jsonResponse);
- $completionId = '444cc444-ffff-dddd-eeee-bbbaaddd2000';
- /** @var ItemRequestInterface $request */
- $request = $this->objectManager->create(ItemRequestInterface::class, [
- 'entityId' => $completionId
- ]);
- /** @var RestAdapter $adapter */
- $adapter = $this->objectManager->create(RestAdapter::class, [
- 'auth' => $this->auth,
- 'restClient' => $this->restClient,
- ]);
- $completion = $adapter->getCompletion($request);
- $this->assertInstanceOf(Completion::class, $completion);
- $this->assertEquals($completionId, $completion->getId());
- }
- /**
- * @test
- */
- public function getTracking()
- {
- $this->markTestIncomplete('mock response once API is ready');
- }
- /**
- * @test
- * @dataProvider getShipmentResponseDataProvider
- * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
- * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
- * @param string $jsonResponse
- */
- public function getShipment($jsonResponse)
- {
- $this->httpClient
- ->expects($this->once())
- ->method('send')
- ->willReturn($jsonResponse);
- /** @var ItemRequestInterface $request */
- $shipmentId = '00000000-5000-0005-0000-000000000000';
- $request = $this->objectManager->create(ItemRequestInterface::class, [
- 'entityId' => $shipmentId,
- ]);
- /** @var ShipmentAdapter $adapter */
- $adapter = $this->objectManager->create(ShipmentAdapter::class, [
- 'auth' => $this->auth,
- 'restClient' => $this->restClient,
- ]);
- $shipment = $adapter->getShipment($request);
- $this->assertInstanceOf(Shipment::class, $shipment);
- $this->assertEquals($shipmentId, $shipment->getId());
- // assert origin location being parsed
- $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getContact()->getOrganisationName());
- $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getContact()->getPersonLastName());
- $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getContact()->getPersonFirstName());
- $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getContact()->getEmail());
- $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getContact()->getPhoneNumber());
- $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getAddress()->getCountryCode());
- $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getAddress()->getLines());
- $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getAddress()->getPostalCode());
- $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getAddress()->getLocality());
- $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getAddress()->getAdministrativeArea());
- // assert documentation being parsed
- $packages = $shipment->getAttributes()->getPackages();
- $this->assertNotEmpty($packages);
- $this->assertContainsOnlyInstancesOf(
- \Temando\Shipping\Rest\Response\Fields\Generic\Package::class,
- $packages
- );
- foreach ($packages as $package) {
- $packageDocs = $package->getDocumentation();
- $this->assertContainsOnlyInstancesOf(
- \Temando\Shipping\Rest\Response\Fields\Generic\Documentation::class,
- $packageDocs
- );
- foreach ($packageDocs as $packageDoc) {
- $this->assertNotEmpty($packageDoc->getSize());
- $this->assertNotEmpty($packageDoc->getDescription());
- $this->assertNotEmpty($packageDoc->getId());
- $this->assertNotEmpty($packageDoc->getMimeType());
- $this->assertNotEmpty($packageDoc->getEncoding());
- $this->assertNotEmpty($packageDoc->getType());
- $this->assertNotEmpty($packageDoc->getUrl());
- }
- }
- }
- /**
- * @test
- * @dataProvider manifestOrderResponseProvider
- * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
- * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
- * @param string $jsonResponse
- */
- public function manifestOrder($jsonResponse)
- {
- $this->httpClient
- ->expects($this->once())
- ->method('send')
- ->willReturn($jsonResponse);
- $orderType = $this->getMockBuilder(OrderRequestType::class)
- ->disableOriginalConstructor()
- ->getMock();
- /** @var OrderRequest $request */
- $request = $this->objectManager->create(OrderRequest::class, [
- 'order' => $orderType,
- ]);
- /** @var OrderAdapter $adapter */
- $adapter = $this->objectManager->create(OrderAdapter::class, [
- 'auth' => $this->auth,
- 'restClient' => $this->restClient,
- ]);
- $order = $adapter->createOrder($request);
- $this->assertInstanceOf(SaveOrderInterface::class, $order);
- $this->assertNotEmpty($order->getData()->getId());
- $this->assertNotEmpty($order->getData()->getAttributes()->getSource()->getReference());
- }
- /**
- * @test
- * @dataProvider updateOrderResponseProvider
- * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
- * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
- * @param string $jsonResponse
- */
- public function updateOrder($jsonResponse)
- {
- $this->httpClient
- ->expects($this->once())
- ->method('send')
- ->willReturn($jsonResponse);
- $orderType = $this->getMockBuilder(OrderRequestType::class)
- ->disableOriginalConstructor()
- ->setMethods(['getId'])
- ->getMock();
- $orderType->expects($this->any())
- ->method('getId')
- ->willReturn('00000000-0000-0000-0000-000000000000');
- /** @var OrderRequest $request */
- $request = $this->objectManager->create(OrderRequest::class, [
- 'order' => $orderType,
- ]);
- /** @var OrderAdapter $adapter */
- $adapter = $this->objectManager->create(OrderAdapter::class, [
- 'auth' => $this->auth,
- 'restClient' => $this->restClient,
- ]);
- $order = $adapter->updateOrder($request);
- $this->assertInstanceOf(SaveOrderInterface::class, $order);
- $this->assertNotEmpty($order->getData()->getId());
- $this->assertNotEmpty($order->getData()->getAttributes()->getSource()->getReference());
- }
- }
|