AjaxLoadRatesTest.php 3.99 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Tax\Test\Unit\Controller\Adminhtml\Rule;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\Http as Request;
use Magento\Framework\App\Response\Http as Response;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Tax\Model\Rate\Provider as RatesProvider;
use Magento\Tax\Controller\Adminhtml\Rule\AjaxLoadRates;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
 * Test for AjaxLoadTest
 */
class AjaxLoadRatesTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var Request | MockObject
     */
    private $request;

    /**
     * @var Response | MockObject
     */
    private $resultFactory;

    /**
     * @var RatesProvider | MockObject
     */
    private $ratesProvider;

    /**
     * @var Context | MockObject
     */
    private $context;

    /**
     * @var SearchCriteriaBuilder | MockObject
     */
    private $searchCriteriaBuilder;

    /**
     * @inheritdoc
     */
    protected function setUp()
    {
        $this->context = $this->getMockBuilder(Context::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->request = $this->getMockBuilder(Request::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->resultFactory = $this->getMockBuilder(ResultFactory::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->ratesProvider = $this->getMockBuilder(RatesProvider::class)
            ->disableOriginalConstructor()
            ->getMock();
    }

    /**
     * Executes the controller action and asserts an exception logic
     */
    public function testExecute()
    {
        $objectManager = new ObjectManager($this);

        $this->request->expects($this->any())
            ->method('getParam')
            ->withAnyParameters()
            ->willReturn('');

        $this->searchCriteriaBuilder->expects($this->once())
            ->method('setCurrentPage')
            ->willReturnSelf();

        $this->searchCriteriaBuilder->expects($this->once())
            ->method('setPageSize')
            ->willReturnSelf();

        $searchCriteria = $this->getMockBuilder(SearchCriteriaInterface::class)
            ->getMockForAbstractClass();

        $this->searchCriteriaBuilder->expects($this->once())
            ->method('create')
            ->willReturn($searchCriteria);

        $this->ratesProvider->expects($this->any())
            ->method('toOptionArray')
            ->with($searchCriteria)
            ->willThrowException(new \Exception());

        $jsonObject= $this->getMockBuilder(Json::class)
            ->disableOriginalConstructor()
            ->setMethods(['setData'])
            ->getMock();

        $jsonObject->expects($this->once())
            ->method('setData')
            ->with([
                'success' => false,
                'errorMessage' => __('An error occurred while loading tax rates.')
            ]);

        $this->resultFactory->expects($this->any())
            ->method('create')
            ->with(ResultFactory::TYPE_JSON)
            ->willReturn($jsonObject);

        $controller = $objectManager->getObject(
            AjaxLoadRates::class,
            [
                'context' => $this->context,
                'searchCriteriaBuilder' => $this->searchCriteriaBuilder,
                'ratesProvider' => $this->ratesProvider,
                'resultFactory' => $this->resultFactory,
                '_request' => $this->request
            ]
        );

        $this->assertSame($jsonObject, $controller->execute());
    }
}