123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?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());
- }
- }
|