TaxRateCollectionTest.php 5.8 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 139 140 141 142 143 144 145 146 147 148
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Tax\Test\Unit\Model;

use \Magento\Tax\Model\TaxRateCollection;
 
class TaxRateCollectionTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var TaxRateCollection
     */
    protected $model;
    
    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $entityFactoryMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $filterBuilderMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $searchCriteriaBuilderMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $sortOrderBuilderMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $rateServiceMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $rateConverterMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $searchCriteriaMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $searchResultsMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $taxRateMock;

    protected function setUp()
    {
        $this->entityFactoryMock = $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class);
        $this->filterBuilderMock = $this->createMock(\Magento\Framework\Api\FilterBuilder::class);
        $this->searchCriteriaBuilderMock =
            $this->createMock(\Magento\Framework\Api\SearchCriteriaBuilder::class);
        $this->sortOrderBuilderMock = $this->createMock(\Magento\Framework\Api\SortOrderBuilder::class);
        $this->rateServiceMock = $this->createPartialMock(\Magento\Tax\Api\TaxRateRepositoryInterface::class, [
                'save',
                'get',
                'deleteById',
                'getList',
                'delete',
                '__wakeup'
            ]);
        $this->rateConverterMock = $this->createMock(\Magento\Tax\Model\Calculation\Rate\Converter::class);
        $this->searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteriaInterface::class);
        $this->searchResultsMock = $this->createMock(\Magento\Tax\Api\Data\TaxRateSearchResultsInterface::class);
        $this->taxRateMock = $this->createMock(\Magento\Tax\Model\Calculation\Rate::class);

        $this->searchCriteriaBuilderMock->expects($this->any())
            ->method('create')
            ->willReturn($this->searchCriteriaMock);

        $this->model = new TaxRateCollection(
            $this->entityFactoryMock,
            $this->filterBuilderMock,
            $this->searchCriteriaBuilderMock,
            $this->sortOrderBuilderMock,
            $this->rateServiceMock,
            $this->rateConverterMock
        );
    }

    public function testLoadData()
    {
        $this->rateServiceMock->expects($this->once())
            ->method('getList')
            ->with($this->searchCriteriaMock)
            ->willReturn($this->searchResultsMock);

        $this->searchResultsMock->expects($this->once())->method('getTotalCount')->willReturn(123);

        $this->searchResultsMock->expects($this->once())->method('getItems')->willReturn([$this->taxRateMock]);
        $this->taxRateMock->expects($this->once())->method('getId')->willReturn(33);
        $this->taxRateMock->expects($this->once())->method('getCode')->willReturn(44);
        $this->taxRateMock->expects($this->once())->method('getTaxCountryId')->willReturn('CountryId');
        $this->taxRateMock->expects($this->once())->method('getTaxRegionId')->willReturn(55);
        $this->taxRateMock->expects($this->once())->method('getRegionName')->willReturn('Region Name');
        $this->taxRateMock->expects($this->once())->method('getTaxPostcode')->willReturn('Post Code');
        $this->taxRateMock->expects($this->once())->method('getRate')->willReturn(1.85);
        $this->rateConverterMock->expects($this->once())
            ->method('createTitleArrayFromServiceObject')
            ->with($this->taxRateMock)
            ->willReturn([]);
        $this->taxRateMock->expects($this->once())->method('getZipTo')->willReturn(null);
        $this->taxRateMock->expects($this->never())->method('getZipFrom');

        $this->model->loadData();
    }

    public function testCreateTaxRateCollectionItem()
    {
        $this->rateServiceMock->expects($this->once())
            ->method('getList')
            ->with($this->searchCriteriaMock)
            ->willReturn($this->searchResultsMock);

        $this->searchResultsMock->expects($this->once())->method('getTotalCount')->willReturn(123);
        $this->searchResultsMock->expects($this->once())->method('getItems')->willReturn([$this->taxRateMock]);
        $this->taxRateMock->expects($this->once())->method('getId')->willReturn(33);
        $this->taxRateMock->expects($this->once())->method('getCode')->willReturn(44);
        $this->taxRateMock->expects($this->once())->method('getTaxCountryId')->willReturn('CountryId');
        $this->taxRateMock->expects($this->once())->method('getTaxRegionId')->willReturn(55);
        $this->taxRateMock->expects($this->once())->method('getRegionName')->willReturn('Region Name');
        $this->taxRateMock->expects($this->once())->method('getTaxPostcode')->willReturn('Post Code');
        $this->taxRateMock->expects($this->once())->method('getRate')->willReturn(1.85);
        $this->rateConverterMock->expects($this->once())
            ->method('createTitleArrayFromServiceObject')
            ->with($this->taxRateMock)
            ->willReturn([]);
        $this->taxRateMock->expects($this->exactly(2))->method('getZipTo')->willReturn(1);
        $this->taxRateMock->expects($this->exactly(2))->method('getZipFrom')->willReturn(200);

        $this->model->loadData();
    }
}