ViewTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Block\Adminhtml\Dispatch;
  6. use Magento\Framework\Session\SessionManagerInterface;
  7. use Magento\Framework\View\LayoutInterface;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. use Temando\Shipping\Model\DispatchInterface;
  10. use Temando\Shipping\Model\DispatchProvider;
  11. use Temando\Shipping\Model\ResourceModel\Dispatch\DispatchRepository;
  12. use Temando\Shipping\Model\ResourceModel\Repository\DispatchRepositoryInterface;
  13. use Temando\Shipping\Rest\Adapter;
  14. use Temando\Shipping\Rest\AuthenticationInterface;
  15. use Temando\Shipping\Rest\RestClient;
  16. use Temando\Shipping\Test\Integration\Provider\RestResponseProvider;
  17. use Temando\Shipping\Webservice\HttpClientInterface;
  18. use Temando\Shipping\Webservice\HttpClientInterfaceFactory;
  19. /**
  20. * Temando View Dispatch Page Test
  21. *
  22. * @package Temando\Shipping\Test\Integration
  23. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * @link http://www.temando.com/
  26. *
  27. */
  28. class ViewTest extends \PHPUnit\Framework\TestCase
  29. {
  30. /**
  31. * Delegate provisioning of test data to separate class
  32. * @return string[]
  33. */
  34. public function getCompletionDataProvider()
  35. {
  36. return RestResponseProvider::getCompletionResponseDataProvider();
  37. }
  38. protected function setUp()
  39. {
  40. parent::setUp();
  41. /** @var SessionManagerInterface $adminSession */
  42. $adminSession = Bootstrap::getObjectManager()->get(SessionManagerInterface::class);
  43. $adminSession->setData(AuthenticationInterface::DATA_KEY_SESSION_TOKEN_EXPIRY, '2038-01-19T03:03:33.000Z');
  44. }
  45. protected function tearDown()
  46. {
  47. /** @var \Magento\TestFramework\ObjectManager $objectManager */
  48. $objectManager = Bootstrap::getObjectManager();
  49. /** @var SessionManagerInterface $adminSession */
  50. $adminSession = $objectManager->get(SessionManagerInterface::class);
  51. $adminSession->unsetData(AuthenticationInterface::DATA_KEY_SESSION_TOKEN_EXPIRY);
  52. $objectManager->removeSharedInstance(RestClient::class);
  53. $objectManager->removeSharedInstance(DispatchProvider::class);
  54. $objectManager->removeSharedInstance(DispatchRepository::class);
  55. $objectManager->removeSharedInstance(Adapter::class);
  56. parent::tearDown();
  57. }
  58. /**
  59. * Assert dispatch listing url is being generated.
  60. *
  61. * @test
  62. * @magentoAppArea adminhtml
  63. * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
  64. */
  65. public function getDispatchListingPageUrl()
  66. {
  67. /** @var LayoutInterface $layout */
  68. $layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
  69. /** @var View $block */
  70. $block = $layout->createBlock(View::class);
  71. $this->assertContains('dispatch/index', $block->getDispatchesPageUrl());
  72. }
  73. /**
  74. * Assert exception is caught if repository cannot load dispatch.
  75. *
  76. * @test
  77. * @magentoAppArea adminhtml
  78. * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
  79. */
  80. public function dispatchCannotBeLoaded()
  81. {
  82. // prepare dispatch view request
  83. $request = Bootstrap::getObjectManager()->get(\Magento\TestFramework\Request::class);
  84. $request->setParam('dispatch_id', 'f00');
  85. // prepare api response (dispatch not found)
  86. $body = '{"errors":[{"status":"404","title":"Completion with id \'f00\' not found.","code":"NotFoundError"}]}';
  87. $testResponse = new \Zend\Http\Response();
  88. $testResponse->setStatusCode(\Zend\Http\Response::STATUS_CODE_404);
  89. $testResponse->setContent($body);
  90. $testAdapter = new \Zend\Http\Client\Adapter\Test();
  91. $testAdapter->setResponse($testResponse);
  92. $zendClient = new \Zend\Http\Client();
  93. $zendClient->setAdapter($testAdapter);
  94. $httpClient = Bootstrap::getObjectManager()->create(HttpClientInterface::class, [
  95. 'client' => $zendClient,
  96. ]);
  97. $clientFactoryMock = $this->getMockBuilder(HttpClientInterfaceFactory::class)
  98. ->disableOriginalConstructor()
  99. ->setMethods(['create'])
  100. ->getMock();
  101. $clientFactoryMock
  102. ->expects($this->any())
  103. ->method('create')
  104. ->willReturn($httpClient);
  105. Bootstrap::getObjectManager()->addSharedInstance($clientFactoryMock, HttpClientInterfaceFactory::class);
  106. // obtain dispatch through block
  107. /** @var LayoutInterface $layout */
  108. $layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
  109. /** @var View $block */
  110. $block = $layout->createBlock(View::class);
  111. $this->assertNull($block->getDispatch());
  112. }
  113. /**
  114. * @test
  115. * @magentoAppArea adminhtml
  116. * @dataProvider getCompletionDataProvider
  117. * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
  118. * @param string $responseBody
  119. */
  120. public function dispatchIsLoaded($responseBody)
  121. {
  122. $dispatchId = '444cc444-ffff-dddd-eeee-bbbaaddd2000';
  123. // prepare dispatch view request
  124. $request = Bootstrap::getObjectManager()->get(\Magento\TestFramework\Request::class);
  125. $request->setParam('dispatch_id', $dispatchId);
  126. // prepare api response (dispatch not found)
  127. $testResponse = new \Zend\Http\Response();
  128. $testResponse->setStatusCode(\Zend\Http\Response::STATUS_CODE_200);
  129. $testResponse->setContent($responseBody);
  130. $testAdapter = new \Zend\Http\Client\Adapter\Test();
  131. $testAdapter->setResponse($testResponse);
  132. $zendClient = new \Zend\Http\Client();
  133. $zendClient->setAdapter($testAdapter);
  134. $httpClient = Bootstrap::getObjectManager()->create(HttpClientInterface::class, [
  135. 'client' => $zendClient,
  136. ]);
  137. $clientFactoryMock = $this->getMockBuilder(HttpClientInterfaceFactory::class)
  138. ->disableOriginalConstructor()
  139. ->setMethods(['create'])
  140. ->getMock();
  141. $clientFactoryMock
  142. ->expects($this->any())
  143. ->method('create')
  144. ->willReturn($httpClient);
  145. Bootstrap::getObjectManager()->addSharedInstance($clientFactoryMock, HttpClientInterfaceFactory::class);
  146. /** @var DispatchRepositoryInterface $dispatchRepository */
  147. $dispatchRepository = Bootstrap::getObjectManager()->get(DispatchRepositoryInterface::class);
  148. $dispatch = $dispatchRepository->getById($dispatchId);
  149. /** @var DispatchProvider $dispatchProvider */
  150. $dispatchProvider = Bootstrap::getObjectManager()->get(DispatchProvider::class);
  151. $dispatchProvider->setDispatch($dispatch);
  152. // obtain dispatch through block
  153. /** @var LayoutInterface $layout */
  154. $layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
  155. /** @var View $block */
  156. $block = $layout->createBlock(View::class);
  157. /** @var DispatchInterface $completion */
  158. $completion = $block->getDispatch();
  159. $this->assertInstanceOf(DispatchInterface::class, $completion);
  160. $this->assertEquals($dispatchId, $completion->getDispatchId());
  161. }
  162. /**
  163. * @test
  164. * @magentoAppArea adminhtml
  165. * @magentoConfigFixture default/general/locale/timezone Europe/London
  166. */
  167. public function getLocalizedDate()
  168. {
  169. /** @var LayoutInterface $layout */
  170. $layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
  171. /** @var View $block */
  172. $block = $layout->createBlock(View::class);
  173. $dateTime = $block->getDate('2017-01-01T00:00:01Z');
  174. $this->assertInstanceOf(\DateTime::class, $dateTime);
  175. $this->assertEquals('2017-01-01 12:00 am', $dateTime->format('Y-m-d g:i a'));
  176. }
  177. /**
  178. * @test
  179. * @magentoAppArea adminhtml
  180. * @magentoConfigFixture default/general/locale/timezone Australia/Brisbane
  181. */
  182. public function getLocalizedDateAu()
  183. {
  184. /** @var LayoutInterface $layout */
  185. $layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
  186. /** @var View $block */
  187. $block = $layout->createBlock(View::class);
  188. $dateTime = $block->getDate('2017-01-01T00:00:01Z');
  189. $this->assertInstanceOf(\DateTime::class, $dateTime);
  190. $this->assertEquals('2017-01-01 10:00 am', $dateTime->format('Y-m-d g:i a'));
  191. }
  192. }