AdapterTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Rest;
  6. use Magento\TestFramework\Helper\Bootstrap;
  7. use Magento\TestFramework\ObjectManager;
  8. use Temando\Shipping\Rest\Adapter as RestAdapter;
  9. use Temando\Shipping\Rest\Request\AuthRequestInterface;
  10. use Temando\Shipping\Rest\Request\ItemRequestInterface;
  11. use Temando\Shipping\Rest\Request\ListRequestInterface;
  12. use Temando\Shipping\Rest\Request\OrderRequest;
  13. use Temando\Shipping\Rest\Request\Type\OrderRequestType;
  14. use Temando\Shipping\Rest\Response\DataObject\CarrierIntegration;
  15. use Temando\Shipping\Rest\Response\DataObject\Completion;
  16. use Temando\Shipping\Rest\Response\DataObject\Container;
  17. use Temando\Shipping\Rest\Response\DataObject\Location;
  18. use Temando\Shipping\Rest\Response\DataObject\Session;
  19. use Temando\Shipping\Rest\Response\DataObject\Shipment;
  20. use Temando\Shipping\Rest\Response\Document\SaveOrderInterface;
  21. use Temando\Shipping\Test\Integration\Provider\RestResponseProvider;
  22. use Temando\Shipping\Webservice\Filter\CollectionFilter;
  23. use Temando\Shipping\Webservice\HttpClient;
  24. use Temando\Shipping\Webservice\HttpClientInterfaceFactory;
  25. /**
  26. * AdapterTest
  27. *
  28. * @magentoAppIsolation enabled
  29. * @markTestIncomplete
  30. *
  31. * @package Temando\Shipping\Test
  32. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  33. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  34. * @link http://www.temando.com/
  35. */
  36. class AdapterTest extends \PHPUnit\Framework\TestCase
  37. {
  38. /**
  39. * @var ObjectManager
  40. */
  41. private $objectManager;
  42. /**
  43. * @var RestClient|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $restClient;
  46. /**
  47. * @var HttpClient|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $httpClient;
  50. /**
  51. * @var Authentication|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $auth;
  54. /**
  55. * Delegate provisioning of test data to separate class
  56. * @return string[]
  57. */
  58. public function startSessionResponseDataProvider()
  59. {
  60. return RestResponseProvider::startSessionResponseDataProvider();
  61. }
  62. /**
  63. * Delegate provisioning of test data to separate class
  64. * @return string[]
  65. */
  66. public function getCarriersResponseDataProvider()
  67. {
  68. return RestResponseProvider::getCarriersResponseDataProvider();
  69. }
  70. /**
  71. * Delegate provisioning of test data to separate class
  72. * @return string[]
  73. */
  74. public function getLocationsResponseDataProvider()
  75. {
  76. return RestResponseProvider::getLocationsResponseDataProvider();
  77. }
  78. /**
  79. * Delegate provisioning of test data to separate class
  80. * @return string[]
  81. */
  82. public function getContainersResponseDataProvider()
  83. {
  84. return RestResponseProvider::getContainersResponseDataProvider();
  85. }
  86. /**
  87. * Delegate provisioning of test data to separate class
  88. * @return string[]
  89. */
  90. public function getCompletionsDataProvider()
  91. {
  92. return RestResponseProvider::getCompletionsResponseDataProvider();
  93. }
  94. /**
  95. * Delegate provisioning of test data to separate class
  96. * @return string[]
  97. */
  98. public function getCompletionDataProvider()
  99. {
  100. return RestResponseProvider::getCompletionResponseDataProvider();
  101. }
  102. /**
  103. * Delegate provisioning of test data to separate class
  104. * @return string[]
  105. */
  106. public function getShipmentResponseDataProvider()
  107. {
  108. return RestResponseProvider::getShipmentResponseDataProvider();
  109. }
  110. /**
  111. * Delegate provisioning of test data to separate class
  112. * @return string[]
  113. */
  114. public function manifestOrderResponseProvider()
  115. {
  116. return RestResponseProvider::manifestOrderResponseProvider();
  117. }
  118. /**
  119. * Delegate provisioning of test data to separate class
  120. * @return string[]
  121. */
  122. public function updateOrderResponseProvider()
  123. {
  124. return RestResponseProvider::updateOrderResponseProvider();
  125. }
  126. /**
  127. * Init object manager
  128. */
  129. protected function setUp()
  130. {
  131. parent::setUp();
  132. $this->objectManager = Bootstrap::getObjectManager();
  133. $this->auth = $this->getMockBuilder(Authentication::class)
  134. ->disableOriginalConstructor()
  135. ->setMethods(['connect', 'getSessionToken'])
  136. ->getMock();
  137. $this->httpClient = $this->getMockBuilder(HttpClient::class)
  138. ->setMethods(['send'])
  139. ->setConstructorArgs(['client' => new \Zend\Http\Client()])
  140. ->getMock();
  141. $clientFactoryMock = $this->getMockBuilder(HttpClientInterfaceFactory::class)
  142. ->setMethods(['create'])
  143. ->disableOriginalConstructor()
  144. ->getMock();
  145. $clientFactoryMock
  146. ->expects($this->once())
  147. ->method('create')
  148. ->willReturn($this->httpClient);
  149. $this->restClient = $this->objectManager->create(RestClient::class, [
  150. 'httpClientFactory' => $clientFactoryMock,
  151. ]);
  152. }
  153. /**
  154. * @test
  155. *
  156. * @dataProvider startSessionResponseDataProvider
  157. * @magentoConfigFixture default/carriers/temando/logging_enabled 0
  158. * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
  159. * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
  160. *
  161. * @param string $jsonResponse
  162. */
  163. public function startSession($jsonResponse)
  164. {
  165. $this->httpClient
  166. ->expects($this->once())
  167. ->method('send')
  168. ->willReturn($jsonResponse);
  169. /** @var AuthRequestInterface $request */
  170. $request = $this->objectManager->create(AuthRequestInterface::class, [
  171. 'username' => '',
  172. 'password' => '',
  173. 'accountId' => 'foo',
  174. 'bearerToken' => 'bar',
  175. 'scope' => AuthenticationInterface::AUTH_SCOPE_ADMIN,
  176. ]);
  177. /** @var AuthAdapter $adapter */
  178. $adapter = $this->objectManager->create(AuthAdapter::class, [
  179. 'restClient' => $this->restClient,
  180. ]);
  181. $session = $adapter->startSession($request);
  182. $this->assertInstanceOf(Session::class, $session);
  183. $this->assertNotEmpty($session->getAttributes()->getSessionToken());
  184. $this->assertNotEmpty($session->getAttributes()->getExpiry());
  185. }
  186. /**
  187. * @test
  188. * @dataProvider getCarriersResponseDataProvider
  189. * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
  190. * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
  191. * @param string $jsonResponse
  192. */
  193. public function getCarriers($jsonResponse)
  194. {
  195. $this->httpClient
  196. ->expects($this->once())
  197. ->method('send')
  198. ->willReturn($jsonResponse);
  199. /** @var ListRequestInterface $request */
  200. $request = $this->objectManager->create(ListRequestInterface::class, ['offset' => 0, 'limit' => 20]);
  201. /** @var RestAdapter $adapter */
  202. $adapter = $this->objectManager->create(RestAdapter::class, [
  203. 'auth' => $this->auth,
  204. 'restClient' => $this->restClient,
  205. ]);
  206. $carriers = $adapter->getCarrierIntegrations($request);
  207. $this->assertInternalType('array', $carriers);
  208. $this->assertContainsOnly(CarrierIntegration::class, $carriers);
  209. }
  210. /**
  211. * @test
  212. * @dataProvider getLocationsResponseDataProvider
  213. * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
  214. * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
  215. * @param string $jsonResponse
  216. */
  217. public function getLocations($jsonResponse)
  218. {
  219. $this->httpClient
  220. ->expects($this->once())
  221. ->method('send')
  222. ->willReturn($jsonResponse);
  223. /** @var ListRequestInterface $request */
  224. $request = $this->objectManager->create(ListRequestInterface::class, ['offset' => 0, 'limit' => 20]);
  225. /** @var RestAdapter $adapter */
  226. $adapter = $this->objectManager->create(RestAdapter::class, [
  227. 'auth' => $this->auth,
  228. 'restClient' => $this->restClient,
  229. ]);
  230. $locations = $adapter->getLocations($request);
  231. $this->assertInternalType('array', $locations);
  232. $this->assertContainsOnly(Location::class, $locations);
  233. }
  234. /**
  235. * @test
  236. * @dataProvider getContainersResponseDataProvider
  237. * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
  238. * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
  239. * @param string $jsonResponse
  240. */
  241. public function getContainers($jsonResponse)
  242. {
  243. $this->httpClient
  244. ->expects($this->once())
  245. ->method('send')
  246. ->willReturn($jsonResponse);
  247. /** @var ListRequestInterface $request */
  248. $request = $this->objectManager->create(ListRequestInterface::class, ['offset' => 0, 'limit' => 20]);
  249. /** @var RestAdapter $adapter */
  250. $adapter = $this->objectManager->create(RestAdapter::class, [
  251. 'auth' => $this->auth,
  252. 'restClient' => $this->restClient,
  253. ]);
  254. $containers = $adapter->getContainers($request);
  255. $this->assertInternalType('array', $containers);
  256. $this->assertContainsOnly(Container::class, $containers);
  257. }
  258. /**
  259. * @test
  260. * @dataProvider getCompletionsDataProvider
  261. * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
  262. * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
  263. * @param string $jsonResponse
  264. */
  265. public function getCompletions($jsonResponse)
  266. {
  267. $this->httpClient
  268. ->expects($this->once())
  269. ->method('send')
  270. ->willReturn($jsonResponse);
  271. /** @var ListRequestInterface $request */
  272. $request = $this->objectManager->create(ListRequestInterface::class, [
  273. 'offset' => 0,
  274. 'limit' => 20,
  275. 'filter' => $this->objectManager->create(CollectionFilter::class, ['filters' => ['foo' => 'bar']]),
  276. ]);
  277. /** @var RestAdapter $adapter */
  278. $adapter = $this->objectManager->create(RestAdapter::class, [
  279. 'auth' => $this->auth,
  280. 'restClient' => $this->restClient,
  281. ]);
  282. $completions = $adapter->getCompletions($request);
  283. $this->assertInternalType('array', $completions);
  284. $this->assertContainsOnly(Completion::class, $completions);
  285. }
  286. /**
  287. * @test
  288. * @dataProvider getCompletionDataProvider
  289. * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
  290. * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
  291. * @param string $jsonResponse
  292. */
  293. public function getCompletion($jsonResponse)
  294. {
  295. $this->httpClient
  296. ->expects($this->once())
  297. ->method('send')
  298. ->willReturn($jsonResponse);
  299. $completionId = '444cc444-ffff-dddd-eeee-bbbaaddd2000';
  300. /** @var ItemRequestInterface $request */
  301. $request = $this->objectManager->create(ItemRequestInterface::class, [
  302. 'entityId' => $completionId
  303. ]);
  304. /** @var RestAdapter $adapter */
  305. $adapter = $this->objectManager->create(RestAdapter::class, [
  306. 'auth' => $this->auth,
  307. 'restClient' => $this->restClient,
  308. ]);
  309. $completion = $adapter->getCompletion($request);
  310. $this->assertInstanceOf(Completion::class, $completion);
  311. $this->assertEquals($completionId, $completion->getId());
  312. }
  313. /**
  314. * @test
  315. */
  316. public function getTracking()
  317. {
  318. $this->markTestIncomplete('mock response once API is ready');
  319. }
  320. /**
  321. * @test
  322. * @dataProvider getShipmentResponseDataProvider
  323. * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
  324. * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
  325. * @param string $jsonResponse
  326. */
  327. public function getShipment($jsonResponse)
  328. {
  329. $this->httpClient
  330. ->expects($this->once())
  331. ->method('send')
  332. ->willReturn($jsonResponse);
  333. /** @var ItemRequestInterface $request */
  334. $shipmentId = '00000000-5000-0005-0000-000000000000';
  335. $request = $this->objectManager->create(ItemRequestInterface::class, [
  336. 'entityId' => $shipmentId,
  337. ]);
  338. /** @var ShipmentAdapter $adapter */
  339. $adapter = $this->objectManager->create(ShipmentAdapter::class, [
  340. 'auth' => $this->auth,
  341. 'restClient' => $this->restClient,
  342. ]);
  343. $shipment = $adapter->getShipment($request);
  344. $this->assertInstanceOf(Shipment::class, $shipment);
  345. $this->assertEquals($shipmentId, $shipment->getId());
  346. // assert origin location being parsed
  347. $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getContact()->getOrganisationName());
  348. $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getContact()->getPersonLastName());
  349. $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getContact()->getPersonFirstName());
  350. $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getContact()->getEmail());
  351. $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getContact()->getPhoneNumber());
  352. $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getAddress()->getCountryCode());
  353. $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getAddress()->getLines());
  354. $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getAddress()->getPostalCode());
  355. $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getAddress()->getLocality());
  356. $this->assertNotEmpty($shipment->getAttributes()->getOrigin()->getAddress()->getAdministrativeArea());
  357. // assert documentation being parsed
  358. $packages = $shipment->getAttributes()->getPackages();
  359. $this->assertNotEmpty($packages);
  360. $this->assertContainsOnlyInstancesOf(
  361. \Temando\Shipping\Rest\Response\Fields\Generic\Package::class,
  362. $packages
  363. );
  364. foreach ($packages as $package) {
  365. $packageDocs = $package->getDocumentation();
  366. $this->assertContainsOnlyInstancesOf(
  367. \Temando\Shipping\Rest\Response\Fields\Generic\Documentation::class,
  368. $packageDocs
  369. );
  370. foreach ($packageDocs as $packageDoc) {
  371. $this->assertNotEmpty($packageDoc->getSize());
  372. $this->assertNotEmpty($packageDoc->getDescription());
  373. $this->assertNotEmpty($packageDoc->getId());
  374. $this->assertNotEmpty($packageDoc->getMimeType());
  375. $this->assertNotEmpty($packageDoc->getEncoding());
  376. $this->assertNotEmpty($packageDoc->getType());
  377. $this->assertNotEmpty($packageDoc->getUrl());
  378. }
  379. }
  380. }
  381. /**
  382. * @test
  383. * @dataProvider manifestOrderResponseProvider
  384. * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
  385. * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
  386. * @param string $jsonResponse
  387. */
  388. public function manifestOrder($jsonResponse)
  389. {
  390. $this->httpClient
  391. ->expects($this->once())
  392. ->method('send')
  393. ->willReturn($jsonResponse);
  394. $orderType = $this->getMockBuilder(OrderRequestType::class)
  395. ->disableOriginalConstructor()
  396. ->getMock();
  397. /** @var OrderRequest $request */
  398. $request = $this->objectManager->create(OrderRequest::class, [
  399. 'order' => $orderType,
  400. ]);
  401. /** @var OrderAdapter $adapter */
  402. $adapter = $this->objectManager->create(OrderAdapter::class, [
  403. 'auth' => $this->auth,
  404. 'restClient' => $this->restClient,
  405. ]);
  406. $order = $adapter->createOrder($request);
  407. $this->assertInstanceOf(SaveOrderInterface::class, $order);
  408. $this->assertNotEmpty($order->getData()->getId());
  409. $this->assertNotEmpty($order->getData()->getAttributes()->getSource()->getReference());
  410. }
  411. /**
  412. * @test
  413. * @dataProvider updateOrderResponseProvider
  414. * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
  415. * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
  416. * @param string $jsonResponse
  417. */
  418. public function updateOrder($jsonResponse)
  419. {
  420. $this->httpClient
  421. ->expects($this->once())
  422. ->method('send')
  423. ->willReturn($jsonResponse);
  424. $orderType = $this->getMockBuilder(OrderRequestType::class)
  425. ->disableOriginalConstructor()
  426. ->setMethods(['getId'])
  427. ->getMock();
  428. $orderType->expects($this->any())
  429. ->method('getId')
  430. ->willReturn('00000000-0000-0000-0000-000000000000');
  431. /** @var OrderRequest $request */
  432. $request = $this->objectManager->create(OrderRequest::class, [
  433. 'order' => $orderType,
  434. ]);
  435. /** @var OrderAdapter $adapter */
  436. $adapter = $this->objectManager->create(OrderAdapter::class, [
  437. 'auth' => $this->auth,
  438. 'restClient' => $this->restClient,
  439. ]);
  440. $order = $adapter->updateOrder($request);
  441. $this->assertInstanceOf(SaveOrderInterface::class, $order);
  442. $this->assertNotEmpty($order->getData()->getId());
  443. $this->assertNotEmpty($order->getData()->getAttributes()->getSource()->getReference());
  444. }
  445. }