123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Tax\Test\Unit\Controller\Adminhtml\Rate;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Framework\Exception\NoSuchEntityException;
- /**
- * Test for AjaxLoadTest
- */
- class AjaxLoadTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\App\Request\Http
- */
- private $request;
- /**
- * @var \Magento\Framework\App\Response\Http
- */
- private $resultFactory;
- /**
- * @var \Magento\Tax\Model\Calculation\RateRepository
- */
- private $taxRateRepository;
- /*
- * test setup
- */
- protected function setUp()
- {
- $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
- ->disableOriginalConstructor()
- ->setMethods(['getParam'])
- ->getMock();
- $this->resultFactory = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->taxRateRepository = $this->getMockBuilder(\Magento\Tax\Model\Calculation\RateRepository::class)
- ->disableOriginalConstructor()
- ->setMethods(['get'])
- ->getMock();
- }
- /**
- * Executes the controller action and asserts non exception logic
- */
- public function testExecute()
- {
- $taxRateId=1;
- $returnArray=[
- 'tax_calculation_rate_id' => null,
- 'tax_country_id' => 'US',
- 'tax_region_id' => 2,
- 'tax_postcode' => null,
- 'code' => 'Tax Rate Code',
- 'rate' => 7.5,
- 'zip_is_range'=> 0,
- 'title[1]' => 'texas',
- ];
- $objectManager = new ObjectManager($this);
- $rateTitles = [$objectManager->getObject(
- \Magento\Tax\Model\Calculation\Rate\Title::class,
- ['data' => ['store_id' => 1, 'value' => 'texas']]
- )
- ];
- $rateMock = $objectManager->getObject(
- \Magento\Tax\Model\Calculation\Rate::class,
- [
- 'data' =>
- [
- 'tax_country_id' => 'US',
- 'tax_region_id' => 2,
- 'tax_postcode' => null,
- 'rate' => 7.5,
- 'code' => 'Tax Rate Code',
- 'titles' => $rateTitles,
- ],
- ]
- );
- $this->request->expects($this->any())
- ->method('getParam')
- ->will($this->returnValue($taxRateId));
- $this->taxRateRepository->expects($this->any())
- ->method('get')
- ->with($taxRateId)
- ->will($this->returnValue($rateMock));
- $taxRateConverter = $this->getMockBuilder(\Magento\Tax\Model\Calculation\Rate\Converter::class)
- ->disableOriginalConstructor()
- ->getMock();
- $taxRateConverter->expects($this->any())
- ->method('createArrayFromServiceObject')
- ->with($rateMock, true)
- ->willReturn($returnArray);
- $jsonObject= $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
- ->disableOriginalConstructor()
- ->setMethods(['setData'])
- ->getMock();
- $jsonObject->expects($this->once())
- ->method('setData')
- ->with(['success' => true, 'error_message' => '', 'result'=>
- $returnArray,
- ]);
- $this->resultFactory->expects($this->any())
- ->method('create')
- ->with(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
- ->willReturn($jsonObject);
- $notification = $objectManager->getObject(
- \Magento\Tax\Controller\Adminhtml\Rate\AjaxLoad::class,
- [
- 'taxRateRepository' => $this->taxRateRepository,
- 'taxRateConverter' => $taxRateConverter,
- 'request' => $this->request,
- 'resultFactory' => $this->resultFactory,
- ]
- );
- // No exception thrown
- $this->assertSame($jsonObject, $notification->execute());
- }
- /**
- * Check if validation throws a localized catched exception in case of incorrect id
- */
- public function testExecuteLocalizedException()
- {
- $taxRateId=999;
- $exceptionMessage='No such entity with taxRateId = '.$taxRateId;
- $noSuchEntityEx= new NoSuchEntityException(__($exceptionMessage));
- $objectManager = new ObjectManager($this);
- $this->request->expects($this->any())
- ->method('getParam')
- ->will($this->returnValue($taxRateId));
- $this->taxRateRepository->expects($this->any())
- ->method('get')
- ->with($taxRateId)
- ->willThrowException($noSuchEntityEx);
- $jsonObject= $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
- ->disableOriginalConstructor()
- ->setMethods(['setData'])
- ->getMock();
- $jsonObject->expects($this->once())
- ->method('setData')
- ->with([
- 'success' => false,
- 'error_message' => $exceptionMessage,
- ]);
- $this->resultFactory->expects($this->any())
- ->method('create')
- ->with(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
- ->willReturn($jsonObject);
- $notification = $objectManager->getObject(
- \Magento\Tax\Controller\Adminhtml\Rate\AjaxLoad::class,
- [
- 'taxRateRepository' => $this->taxRateRepository,
- 'request' => $this->request,
- 'resultFactory' => $this->resultFactory,
- ]
- );
- //exception thrown with catch
- $this->assertSame($jsonObject, $notification->execute());
- }
- /**
- * Check if validation throws a localized catched exception in case of incorrect id
- */
- public function testExecuteException()
- {
- $taxRateId=999;
- $exceptionMessage=__('An error occurred while loading this tax rate.');
- $noSuchEntityEx= new \Exception();
- $objectManager = new ObjectManager($this);
- $this->request->expects($this->any())
- ->method('getParam')
- ->will($this->returnValue($taxRateId));
- $this->taxRateRepository->expects($this->any())
- ->method('get')
- ->with($taxRateId)
- ->willThrowException($noSuchEntityEx);
- $jsonObject= $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
- ->disableOriginalConstructor()
- ->setMethods(['setData'])
- ->getMock();
- $jsonObject->expects($this->once())
- ->method('setData')
- ->with([
- 'success' => false,
- 'error_message' => $exceptionMessage,
- ]);
- $this->resultFactory->expects($this->any())
- ->method('create')
- ->with(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
- ->willReturn($jsonObject);
- $notification = $objectManager->getObject(
- \Magento\Tax\Controller\Adminhtml\Rate\AjaxLoad::class,
- [
- 'taxRateRepository' => $this->taxRateRepository,
- 'request' => $this->request,
- 'resultFactory' => $this->resultFactory,
- ]
- );
- //exception thrown with catch
- $this->assertSame($jsonObject, $notification->execute());
- }
- }
|