AjaxLoadTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Test\Unit\Controller\Adminhtml\Rate;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. /**
  10. * Test for AjaxLoadTest
  11. */
  12. class AjaxLoadTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\App\Request\Http
  16. */
  17. private $request;
  18. /**
  19. * @var \Magento\Framework\App\Response\Http
  20. */
  21. private $resultFactory;
  22. /**
  23. * @var \Magento\Tax\Model\Calculation\RateRepository
  24. */
  25. private $taxRateRepository;
  26. /*
  27. * test setup
  28. */
  29. protected function setUp()
  30. {
  31. $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  32. ->disableOriginalConstructor()
  33. ->setMethods(['getParam'])
  34. ->getMock();
  35. $this->resultFactory = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
  36. ->disableOriginalConstructor()
  37. ->setMethods(['create'])
  38. ->getMock();
  39. $this->taxRateRepository = $this->getMockBuilder(\Magento\Tax\Model\Calculation\RateRepository::class)
  40. ->disableOriginalConstructor()
  41. ->setMethods(['get'])
  42. ->getMock();
  43. }
  44. /**
  45. * Executes the controller action and asserts non exception logic
  46. */
  47. public function testExecute()
  48. {
  49. $taxRateId=1;
  50. $returnArray=[
  51. 'tax_calculation_rate_id' => null,
  52. 'tax_country_id' => 'US',
  53. 'tax_region_id' => 2,
  54. 'tax_postcode' => null,
  55. 'code' => 'Tax Rate Code',
  56. 'rate' => 7.5,
  57. 'zip_is_range'=> 0,
  58. 'title[1]' => 'texas',
  59. ];
  60. $objectManager = new ObjectManager($this);
  61. $rateTitles = [$objectManager->getObject(
  62. \Magento\Tax\Model\Calculation\Rate\Title::class,
  63. ['data' => ['store_id' => 1, 'value' => 'texas']]
  64. )
  65. ];
  66. $rateMock = $objectManager->getObject(
  67. \Magento\Tax\Model\Calculation\Rate::class,
  68. [
  69. 'data' =>
  70. [
  71. 'tax_country_id' => 'US',
  72. 'tax_region_id' => 2,
  73. 'tax_postcode' => null,
  74. 'rate' => 7.5,
  75. 'code' => 'Tax Rate Code',
  76. 'titles' => $rateTitles,
  77. ],
  78. ]
  79. );
  80. $this->request->expects($this->any())
  81. ->method('getParam')
  82. ->will($this->returnValue($taxRateId));
  83. $this->taxRateRepository->expects($this->any())
  84. ->method('get')
  85. ->with($taxRateId)
  86. ->will($this->returnValue($rateMock));
  87. $taxRateConverter = $this->getMockBuilder(\Magento\Tax\Model\Calculation\Rate\Converter::class)
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $taxRateConverter->expects($this->any())
  91. ->method('createArrayFromServiceObject')
  92. ->with($rateMock, true)
  93. ->willReturn($returnArray);
  94. $jsonObject= $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
  95. ->disableOriginalConstructor()
  96. ->setMethods(['setData'])
  97. ->getMock();
  98. $jsonObject->expects($this->once())
  99. ->method('setData')
  100. ->with(['success' => true, 'error_message' => '', 'result'=>
  101. $returnArray,
  102. ]);
  103. $this->resultFactory->expects($this->any())
  104. ->method('create')
  105. ->with(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
  106. ->willReturn($jsonObject);
  107. $notification = $objectManager->getObject(
  108. \Magento\Tax\Controller\Adminhtml\Rate\AjaxLoad::class,
  109. [
  110. 'taxRateRepository' => $this->taxRateRepository,
  111. 'taxRateConverter' => $taxRateConverter,
  112. 'request' => $this->request,
  113. 'resultFactory' => $this->resultFactory,
  114. ]
  115. );
  116. // No exception thrown
  117. $this->assertSame($jsonObject, $notification->execute());
  118. }
  119. /**
  120. * Check if validation throws a localized catched exception in case of incorrect id
  121. */
  122. public function testExecuteLocalizedException()
  123. {
  124. $taxRateId=999;
  125. $exceptionMessage='No such entity with taxRateId = '.$taxRateId;
  126. $noSuchEntityEx= new NoSuchEntityException(__($exceptionMessage));
  127. $objectManager = new ObjectManager($this);
  128. $this->request->expects($this->any())
  129. ->method('getParam')
  130. ->will($this->returnValue($taxRateId));
  131. $this->taxRateRepository->expects($this->any())
  132. ->method('get')
  133. ->with($taxRateId)
  134. ->willThrowException($noSuchEntityEx);
  135. $jsonObject= $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
  136. ->disableOriginalConstructor()
  137. ->setMethods(['setData'])
  138. ->getMock();
  139. $jsonObject->expects($this->once())
  140. ->method('setData')
  141. ->with([
  142. 'success' => false,
  143. 'error_message' => $exceptionMessage,
  144. ]);
  145. $this->resultFactory->expects($this->any())
  146. ->method('create')
  147. ->with(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
  148. ->willReturn($jsonObject);
  149. $notification = $objectManager->getObject(
  150. \Magento\Tax\Controller\Adminhtml\Rate\AjaxLoad::class,
  151. [
  152. 'taxRateRepository' => $this->taxRateRepository,
  153. 'request' => $this->request,
  154. 'resultFactory' => $this->resultFactory,
  155. ]
  156. );
  157. //exception thrown with catch
  158. $this->assertSame($jsonObject, $notification->execute());
  159. }
  160. /**
  161. * Check if validation throws a localized catched exception in case of incorrect id
  162. */
  163. public function testExecuteException()
  164. {
  165. $taxRateId=999;
  166. $exceptionMessage=__('An error occurred while loading this tax rate.');
  167. $noSuchEntityEx= new \Exception();
  168. $objectManager = new ObjectManager($this);
  169. $this->request->expects($this->any())
  170. ->method('getParam')
  171. ->will($this->returnValue($taxRateId));
  172. $this->taxRateRepository->expects($this->any())
  173. ->method('get')
  174. ->with($taxRateId)
  175. ->willThrowException($noSuchEntityEx);
  176. $jsonObject= $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
  177. ->disableOriginalConstructor()
  178. ->setMethods(['setData'])
  179. ->getMock();
  180. $jsonObject->expects($this->once())
  181. ->method('setData')
  182. ->with([
  183. 'success' => false,
  184. 'error_message' => $exceptionMessage,
  185. ]);
  186. $this->resultFactory->expects($this->any())
  187. ->method('create')
  188. ->with(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
  189. ->willReturn($jsonObject);
  190. $notification = $objectManager->getObject(
  191. \Magento\Tax\Controller\Adminhtml\Rate\AjaxLoad::class,
  192. [
  193. 'taxRateRepository' => $this->taxRateRepository,
  194. 'request' => $this->request,
  195. 'resultFactory' => $this->resultFactory,
  196. ]
  197. );
  198. //exception thrown with catch
  199. $this->assertSame($jsonObject, $notification->execute());
  200. }
  201. }