ViewTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Controller\Adminhtml\Dispatch;
  6. use Magento\Framework\Exception\NoSuchEntityException;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. use Magento\TestFramework\TestCase\AbstractBackendController;
  9. use Temando\Shipping\Model\Dispatch;
  10. use Temando\Shipping\Model\DispatchProvider;
  11. use Temando\Shipping\Model\ResourceModel\Dispatch\DispatchRepository;
  12. /**
  13. * @magentoAppArea adminhtml
  14. */
  15. class ViewTest extends AbstractBackendController
  16. {
  17. /**
  18. * The resource used to authorize action
  19. *
  20. * @var string
  21. */
  22. protected $resource = 'Temando_Shipping::dispatches';
  23. /**
  24. * The uri at which to access the controller
  25. *
  26. * @var string
  27. */
  28. protected $uri = 'backend/temando/dispatch/view';
  29. /**
  30. * @var DispatchRepository|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $dispatchRepo;
  33. protected function setUp()
  34. {
  35. parent::setUp();
  36. $this->dispatchRepo = $this->getMockBuilder(DispatchRepository::class)
  37. ->setMethods(['getById'])
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. Bootstrap::getObjectManager()->addSharedInstance($this->dispatchRepo, DispatchRepository::class);
  41. }
  42. protected function tearDown()
  43. {
  44. Bootstrap::getObjectManager()->removeSharedInstance(DispatchProvider::class);
  45. parent::tearDown();
  46. }
  47. /**
  48. * @param string $dispatchId
  49. * @return Dispatch
  50. */
  51. private function createDispatch($dispatchId)
  52. {
  53. $status = 'processed';
  54. $carrierName = 'Foo';
  55. $carrierMessages = ['Message Foo', 'Message Bar'];
  56. $createdAtDate = '1999-01-19T03:03:33.000Z';
  57. $readyAtDate = '2099-01-19T03:03:33.000Z';
  58. $pickupNumbers = ['pnum 123', 'pnum 987'];
  59. $pickupCharges = [
  60. new Dispatch\PickupCharge([
  61. Dispatch\PickupChargeInterface::DESCRIPTION => 'Treats',
  62. Dispatch\PickupChargeInterface::AMOUNT => 0.99,
  63. Dispatch\PickupChargeInterface::CURRENCY => 'AUD'
  64. ]),
  65. new Dispatch\PickupCharge([
  66. Dispatch\PickupChargeInterface::DESCRIPTION => 'Sweets',
  67. Dispatch\PickupChargeInterface::AMOUNT => 3.03,
  68. Dispatch\PickupChargeInterface::CURRENCY => 'AUD'
  69. ]),
  70. ];
  71. $includedShipments = [
  72. new Dispatch\Shipment([
  73. Dispatch\ShipmentInterface::SHIPMENT_ID => '1234-ship',
  74. Dispatch\ShipmentInterface::STATUS => 'fulfilled',
  75. ]),
  76. ];
  77. $failedShipments = [];
  78. $documentation = [];
  79. $dispatch = new Dispatch([
  80. Dispatch::DISPATCH_ID => $dispatchId,
  81. Dispatch::STATUS => $status,
  82. Dispatch::CARRIER_NAME => $carrierName,
  83. Dispatch::CARRIER_MESSAGES => $carrierMessages,
  84. Dispatch::CREATED_AT_DATE => $createdAtDate,
  85. Dispatch::READY_AT_DATE => $readyAtDate,
  86. Dispatch::PICKUP_NUMBERS => $pickupNumbers,
  87. Dispatch::PICKUP_CHARGES => $pickupCharges,
  88. Dispatch::INCLUDED_SHIPMENTS => $includedShipments,
  89. Dispatch::FAILED_SHIPMENTS => $failedShipments,
  90. Dispatch::DOCUMENTATION => $documentation,
  91. ]);
  92. return $dispatch;
  93. }
  94. /**
  95. * @test
  96. * @magentoConfigFixture default/carriers/temando/account_id 23
  97. * @magentoConfigFixture default/carriers/temando/bearer_token 808
  98. */
  99. public function dispatchLoadSuccess()
  100. {
  101. $dispatchId = '1234-abcd';
  102. $dispatch = $this->createDispatch($dispatchId);
  103. $this->dispatchRepo
  104. ->expects($this->once())
  105. ->method('getById')
  106. ->with($dispatchId)
  107. ->willReturn($dispatch);
  108. $this->getRequest()->setParam('dispatch_id', $dispatchId);
  109. $this->dispatch($this->uri);
  110. $this->assertContains($dispatch->getCarrierName(), $this->getResponse()->getBody());
  111. $this->assertContains('Documentation', $this->getResponse()->getBody());
  112. foreach ($dispatch->getPickupCharges() as $pickupCharge) {
  113. $this->assertContains($pickupCharge->getDescription(), $this->getResponse()->getBody());
  114. }
  115. foreach ($dispatch->getCarrierMessages() as $carrierMessage) {
  116. $this->assertContains($carrierMessage, $this->getResponse()->getBody());
  117. }
  118. }
  119. /**
  120. * @test
  121. * @magentoConfigFixture default/carriers/temando/account_id 23
  122. * @magentoConfigFixture default/carriers/temando/bearer_token 808
  123. */
  124. public function dispatchLoadError()
  125. {
  126. $dispatchId = '1234-abcd';
  127. $this->dispatchRepo
  128. ->expects($this->once())
  129. ->method('getById')
  130. ->with($dispatchId)
  131. ->willThrowException(new NoSuchEntityException(__('Not found.')));
  132. $this->getRequest()->setParam('dispatch_id', $dispatchId);
  133. $this->dispatch($this->uri);
  134. $this->assertNotContains('Documentation', $this->getResponse()->getBody());
  135. }
  136. /**
  137. * @test
  138. * @magentoConfigFixture default/carriers/temando/account_id 23
  139. * @magentoConfigFixture default/carriers/temando/bearer_token 808
  140. */
  141. public function testAclHasAccess()
  142. {
  143. $dispatchId = '1234-abcd';
  144. $dispatch = $this->createDispatch($dispatchId);
  145. $this->dispatchRepo
  146. ->expects($this->once())
  147. ->method('getById')
  148. ->with($dispatchId)
  149. ->willReturn($dispatch);
  150. $this->getRequest()->setParam('dispatch_id', $dispatchId);
  151. parent::testAclHasAccess();
  152. }
  153. /**
  154. * @test
  155. * @magentoConfigFixture default/carriers/temando/account_id 23
  156. * @magentoConfigFixture default/carriers/temando/bearer_token 808
  157. */
  158. public function testAclNoAccess()
  159. {
  160. $dispatchId = '1234-abcd';
  161. $dispatch = $this->createDispatch($dispatchId);
  162. $this->dispatchRepo
  163. ->expects($this->once())
  164. ->method('getById')
  165. ->with($dispatchId)
  166. ->willReturn($dispatch);
  167. $this->getRequest()->setParam('dispatch_id', $dispatchId);
  168. parent::testAclNoAccess();
  169. }
  170. }