RateTest.php 4.84 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Tax\Test\Unit\Model\Calculation;

class RateTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
     */
    protected $objectHelper;

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

    /**
     *  Init data
     */
    protected function setUp()
    {
        $this->objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $this->resourceMock = $this->createPartialMock(
            \Magento\Framework\Model\ResourceModel\AbstractResource::class,
            [
                '_construct',
                'getConnection',
                'getIdFieldName',
                'beginTransaction',
                'rollBack'
            ]
        );
        $this->resourceMock->expects($this->any())->method('beginTransaction')->will($this->returnSelf());
    }

    /**
     * Check if validation throws exceptions in case of incorrect input data
     *
     * @param string $exceptionMessage
     * @param array $data
     *
     * @dataProvider exceptionOfValidationDataProvider
     */
    public function testExceptionOfValidation($exceptionMessage, $data)
    {
        $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
        $this->expectExceptionMessage($exceptionMessage);
        $rate = $this->objectHelper->getObject(
            \Magento\Tax\Model\Calculation\Rate::class,
            ['resource' => $this->resourceMock]
        );
        foreach ($data as $key => $value) {
            $rate->setData($key, $value);
        }
        $rate->beforeSave();
    }

    /**
     * Data provider
     *
     * @return array
     */
    public function exceptionOfValidationDataProvider()
    {
        return [
            'fill all required fields 1' => [
                'exceptionMessage' => 'The required information is invalid. Verify the information and try again.',
                'data' => [
                    'zip_is_range' => true,
                    'zip_from' => '0111',
                    'zip_to' => '',
                    'code' => '',
                    'tax_country_id' => '',
                    'rate' => '',
                    'tax_postcode' => '',
                ],
            ],
            'fill all required fields 2' => [
                'exceptionMessage' => 'The required information is invalid. Verify the information and try again.',
                'data' => [
                    'zip_is_range' => '',
                    'zip_from' => '',
                    'zip_to' => '',
                    'code' => '',
                    'tax_country_id' => '',
                    'rate' => '0.2',
                    'tax_postcode' => '1234',
                ],
            ],
            'positive number' => [
                'exceptionMessage' => 'The Rate Percent is invalid. Enter a positive number and try again.',
                'data' => [
                    'zip_is_range' => '',
                    'zip_from' => '',
                    'zip_to' => '',
                    'code' => 'code',
                    'tax_country_id' => 'US',
                    'rate' => '-1',
                    'tax_postcode' => '1234',
                ],
            ],
            'zip code length' => [
                'exceptionMessage' => 'The ZIP Code length is invalid. '
                    . 'Verify that the length is nine characters or fewer and try again.',
                'data' => [
                    'zip_is_range' => true,
                    'zip_from' => '1234567890',
                    'zip_to' => '1234',
                    'code' => 'code',
                    'tax_country_id' => 'US',
                    'rate' => '1.1',
                    'tax_postcode' => '1234',
                ],
            ],
            'contain characters' => [
                'exceptionMessage' => 'The ZIP Code is invalid. Use numbers only.',
                'data' => [
                    'zip_is_range' => true,
                    'zip_from' => 'foo',
                    'zip_to' => '1234',
                    'code' => 'code',
                    'tax_country_id' => 'US',
                    'rate' => '1.1',
                    'tax_postcode' => '1234',
                ],
            ],
            'equal or greater' => [
                'exceptionMessage' => 'Range To should be equal or greater than Range From.',
                'data' => [
                    'zip_is_range' => true,
                    'zip_from' => '321',
                    'zip_to' => '123',
                    'code' => 'code',
                    'tax_country_id' => 'US',
                    'rate' => '1.1',
                    'tax_postcode' => '1234',
                ],
            ],
        ];
    }
}