AbstractConditionTest.php 7.16 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Rule\Test\Unit\Model\Condition;

class AbstractConditionTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var AbstractCondition|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $_condition;

    protected function setUp()
    {
        $this->_condition = $this->getMockForAbstractClass(
            \Magento\Rule\Model\Condition\AbstractCondition::class,
            [],
            '',
            false,
            false,
            true,
            ['getInputType']
        );
    }

    public function testGetjointTables()
    {
        $this->_condition->setAttribute('category_ids');
        $this->assertEquals([], $this->_condition->getTablesToJoin());
        $this->_condition->setAttribute('gdsjkfghksldjfg');
        $this->assertEmpty($this->_condition->getTablesToJoin());
    }

    public function testGetMappedSqlField()
    {
        $this->_condition->setAttribute('category_ids');
        $this->assertEquals('category_ids', $this->_condition->getMappedSqlField());
    }

    /**
     * @return array
     */
    public function validateAttributeDataProvider()
    {
        return [
            // value, operator, valueForValidate, expectedResult
            [1, '==', new \stdClass(), false],
            [new \stdClass(), '==', new \stdClass(), false],

            [1, '==', 1, true],
            [0, '==', 1, false],
            ['0', '==', 1, false],
            ['1', '==', 1, true],
            ['x', '==', 'x', true],
            ['x', '==', 0, false],

            [1, '!=', 1, false],
            [0, '!=', 1, true],
            ['0', '!=', 1, true],
            ['1', '!=', 1, false],
            ['x', '!=', 'x', false],
            ['x', '!=', 0, true],

            [1, '==', [1], true],
            [1, '!=', [1], false],
            [1, '==', [3, 1, 5], false],
            [1, '!=', [1, 5], true],

            [[1,2,3], '==', '1,2,3', false],
            [[1], '==', 1, false],

            // Note: validated value is on the right, so read expression in the array from right to left
            // e.g.: 1, <=, 0 actually is 0 <= 1.
            [1, '>', 1, false],
            [1, '<=', 1, true],
            [1, '<=', '1', true],
            [1, '<=', 0, true],
            [0, '>', [1], false],

            [1, '<', 1, false],
            [1, '>=', 1, true],
            [1, '>=', '1', true],
            [1, '>=', 0, false],
            [0, '<', [1], false],
        ];
    }

    /**
     * @param $existingValue
     * @param $operator
     * @param $valueForValidate
     * @param $expectedResult
     *
     * @dataProvider validateAttributeDataProvider
     */
    public function testValidateAttribute($existingValue, $operator, $valueForValidate, $expectedResult)
    {
        $this->_condition->setOperator($operator);
        $this->_condition->setData('value_parsed', $existingValue);
        $this->assertEquals(
            $expectedResult,
            $this->_condition->validateAttribute($valueForValidate),
            "Failed asserting that "
            . var_export($existingValue, true)
            . $operator
            . var_export($valueForValidate, true)
        );
    }

    /**
     * @param $existingValue
     * @param $operator
     * @param $valueForValidate
     * @param $expectedResult
     *
     * @dataProvider validateAttributeDataProvider
     */
    public function testValidate($existingValue, $operator, $valueForValidate, $expectedResult)
    {
        $objectMock = $this->createPartialMock(
            \Magento\Framework\Model\AbstractModel::class,
            ['hasData', 'load', 'getId', 'getData']
        );
        $objectMock->expects($this->once())
            ->method('hasData')
            ->willReturn(false);
        $objectMock->expects($this->once())
            ->method('getId')
            ->willReturn(7);
        $objectMock->expects($this->once())
            ->method('load')
            ->with(7);
        $objectMock->expects($this->once())
            ->method('getData')
            ->willReturn($valueForValidate);

        $this->_condition->setOperator($operator);
        $this->_condition->setData('value_parsed', $existingValue);
        $this->assertEquals(
            $expectedResult,
            $this->_condition->validate($objectMock),
            "Failed asserting that "
            . var_export($existingValue, true)
            . $operator
            . var_export($valueForValidate, true)
        );
    }

    /**
     * @return array
     */
    public function validateAttributeArrayInputTypeDataProvider()
    {
        return [
            // value, operator, valueForValidate, expectedResult, inputType
            [[1, 2, 3], '==', [2, 1, 3], true, 'multiselect'],
            [[1, 2], '==', [2, 3], true, 'multiselect'],
            [[1, 1, 3], '==', [2, 4], false, 'multiselect'],
            [[1, 2], '!=', [2, 3], false, 'multiselect'],
            [[1, 2], '!=', 1, false, 'multiselect'],

            [[1, 2, 3], '{}', '1', true, 'grid'],
            [[1, 2, 3], '{}', '8', false, 'grid'],
            [[1, 2, 3], '{}', 5, false, 'grid'],
            [[1, 2, 3], '{}', [2, 3, 4], true, 'grid'],
            [[1, 2, 3], '{}', [4], false, 'grid'],
            [[3], '{}', [], false, 'grid'],
            [1, '{}', 1, false, 'grid'],
            [1, '!{}', [1, 2, 3], false, 'grid'],
            [[1], '{}', null, false, 'grid'],
            [null, '{}', null, true, 'input'],
            [null, '!{}', null, false, 'input'],
            [null, '{}', [1], false, 'input'],

            [[1, 2, 3], '()', 1, true, 'select'],
            [[1, 2, 3], '!()', 1, false, 'select'],
            [[1], '()', 3, false, 'select'],
            [[1], '!()', 3, true, 'select'],
            [3, '()', 3, false, 'select'],
            [[3], '()', [3], true, 'select'],
            [3, '()', [3], false, 'select'],

        ];
    }

    /**
     * @param $existingValue
     * @param $operator
     * @param $valueForValidate
     * @param $expectedResult
     * @param $inputType
     *
     * @dataProvider validateAttributeArrayInputTypeDataProvider
     */
    public function testValidateArrayOperatorType(
        $existingValue,
        $operator,
        $valueForValidate,
        $expectedResult,
        $inputType
    ) {
        $this->_condition->setOperator($operator);
        $this->_condition->setData('value_parsed', $existingValue);
        $this->_condition->getDefaultOperatorInputByType();
        $this->_condition
            ->expects($this->any())
            ->method('getInputType')
            ->will($this->returnValue($inputType));

        $this->assertEquals(
            $expectedResult,
            $this->_condition->validateAttribute($valueForValidate),
            "Failed asserting that "
            . var_export($existingValue, true)
            . $operator
            . var_export($valueForValidate, true)
        );
    }

    public function testGetValueParsed()
    {
        $value = [1, 2, 3, 4, 5, 6, 7, 8, 9];
        $this->_condition->setValue(['1,2,3,4,5,6,7,8,9']);
        $this->_condition->setOperator('()');
        $this->assertEquals($value, $this->_condition->getValueParsed());
    }
}