Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
AjaxLoadTest.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class AjaxLoadTest extends \PHPUnit\Framework\TestCase
15 {
19  private $request;
20 
24  private $resultFactory;
25 
29  private $taxRateRepository;
30 
31  /*
32  * test setup
33  */
34  protected function setUp()
35  {
36  $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
37  ->disableOriginalConstructor()
38  ->setMethods(['getParam'])
39  ->getMock();
40 
41  $this->resultFactory = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
42  ->disableOriginalConstructor()
43  ->setMethods(['create'])
44  ->getMock();
45 
46  $this->taxRateRepository = $this->getMockBuilder(\Magento\Tax\Model\Calculation\RateRepository::class)
47  ->disableOriginalConstructor()
48  ->setMethods(['get'])
49  ->getMock();
50  }
51 
55  public function testExecute()
56  {
57  $taxRateId=1;
58  $returnArray=[
59  'tax_calculation_rate_id' => null,
60  'tax_country_id' => 'US',
61  'tax_region_id' => 2,
62  'tax_postcode' => null,
63  'code' => 'Tax Rate Code',
64  'rate' => 7.5,
65  'zip_is_range'=> 0,
66  'title[1]' => 'texas',
67  ];
68  $objectManager = new ObjectManager($this);
69  $rateTitles = [$objectManager->getObject(
70  \Magento\Tax\Model\Calculation\Rate\Title::class,
71  ['data' => ['store_id' => 1, 'value' => 'texas']]
72  )
73  ];
74  $rateMock = $objectManager->getObject(
75  \Magento\Tax\Model\Calculation\Rate::class,
76  [
77  'data' =>
78  [
79  'tax_country_id' => 'US',
80  'tax_region_id' => 2,
81  'tax_postcode' => null,
82  'rate' => 7.5,
83  'code' => 'Tax Rate Code',
84  'titles' => $rateTitles,
85  ],
86  ]
87  );
88 
89  $this->request->expects($this->any())
90  ->method('getParam')
91  ->will($this->returnValue($taxRateId));
92 
93  $this->taxRateRepository->expects($this->any())
94  ->method('get')
95  ->with($taxRateId)
96  ->will($this->returnValue($rateMock));
97 
98  $taxRateConverter = $this->getMockBuilder(\Magento\Tax\Model\Calculation\Rate\Converter::class)
99  ->disableOriginalConstructor()
100  ->getMock();
101 
102  $taxRateConverter->expects($this->any())
103  ->method('createArrayFromServiceObject')
104  ->with($rateMock, true)
105  ->willReturn($returnArray);
106 
107  $jsonObject= $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
108  ->disableOriginalConstructor()
109  ->setMethods(['setData'])
110  ->getMock();
111 
112  $jsonObject->expects($this->once())
113  ->method('setData')
114  ->with(['success' => true, 'error_message' => '', 'result'=>
115  $returnArray,
116  ]);
117 
118  $this->resultFactory->expects($this->any())
119  ->method('create')
120  ->with(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
121  ->willReturn($jsonObject);
122 
123  $notification = $objectManager->getObject(
124  \Magento\Tax\Controller\Adminhtml\Rate\AjaxLoad::class,
125  [
126  'taxRateRepository' => $this->taxRateRepository,
127  'taxRateConverter' => $taxRateConverter,
128  'request' => $this->request,
129  'resultFactory' => $this->resultFactory,
130  ]
131  );
132 
133  // No exception thrown
134  $this->assertSame($jsonObject, $notification->execute());
135  }
136 
141  {
142  $taxRateId=999;
143  $exceptionMessage='No such entity with taxRateId = '.$taxRateId;
144  $noSuchEntityEx= new NoSuchEntityException(__($exceptionMessage));
145 
146  $objectManager = new ObjectManager($this);
147 
148  $this->request->expects($this->any())
149  ->method('getParam')
150  ->will($this->returnValue($taxRateId));
151 
152  $this->taxRateRepository->expects($this->any())
153  ->method('get')
154  ->with($taxRateId)
155  ->willThrowException($noSuchEntityEx);
156 
157  $jsonObject= $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
158  ->disableOriginalConstructor()
159  ->setMethods(['setData'])
160  ->getMock();
161 
162  $jsonObject->expects($this->once())
163  ->method('setData')
164  ->with([
165  'success' => false,
166  'error_message' => $exceptionMessage,
167  ]);
168 
169  $this->resultFactory->expects($this->any())
170  ->method('create')
171  ->with(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
172  ->willReturn($jsonObject);
173 
174  $notification = $objectManager->getObject(
175  \Magento\Tax\Controller\Adminhtml\Rate\AjaxLoad::class,
176  [
177  'taxRateRepository' => $this->taxRateRepository,
178  'request' => $this->request,
179  'resultFactory' => $this->resultFactory,
180  ]
181  );
182 
183  //exception thrown with catch
184  $this->assertSame($jsonObject, $notification->execute());
185  }
186 
190  public function testExecuteException()
191  {
192  $taxRateId=999;
193  $exceptionMessage=__('An error occurred while loading this tax rate.');
194  $noSuchEntityEx= new \Exception();
195 
196  $objectManager = new ObjectManager($this);
197 
198  $this->request->expects($this->any())
199  ->method('getParam')
200  ->will($this->returnValue($taxRateId));
201 
202  $this->taxRateRepository->expects($this->any())
203  ->method('get')
204  ->with($taxRateId)
205  ->willThrowException($noSuchEntityEx);
206 
207  $jsonObject= $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
208  ->disableOriginalConstructor()
209  ->setMethods(['setData'])
210  ->getMock();
211 
212  $jsonObject->expects($this->once())
213  ->method('setData')
214  ->with([
215  'success' => false,
216  'error_message' => $exceptionMessage,
217  ]);
218 
219  $this->resultFactory->expects($this->any())
220  ->method('create')
221  ->with(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
222  ->willReturn($jsonObject);
223 
224  $notification = $objectManager->getObject(
225  \Magento\Tax\Controller\Adminhtml\Rate\AjaxLoad::class,
226  [
227  'taxRateRepository' => $this->taxRateRepository,
228  'request' => $this->request,
229  'resultFactory' => $this->resultFactory,
230  ]
231  );
232 
233  //exception thrown with catch
234  $this->assertSame($jsonObject, $notification->execute());
235  }
236 }
$objectManager
Definition: bootstrap.php:17
__()
Definition: __.php:13