Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractConditionTest.php
Go to the documentation of this file.
1 <?php
8 
9 class AbstractConditionTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $_condition;
15 
16  protected function setUp()
17  {
18  $this->_condition = $this->getMockForAbstractClass(
19  \Magento\Rule\Model\Condition\AbstractCondition::class,
20  [],
21  '',
22  false,
23  false,
24  true,
25  ['getInputType']
26  );
27  }
28 
29  public function testGetjointTables()
30  {
31  $this->_condition->setAttribute('category_ids');
32  $this->assertEquals([], $this->_condition->getTablesToJoin());
33  $this->_condition->setAttribute('gdsjkfghksldjfg');
34  $this->assertEmpty($this->_condition->getTablesToJoin());
35  }
36 
37  public function testGetMappedSqlField()
38  {
39  $this->_condition->setAttribute('category_ids');
40  $this->assertEquals('category_ids', $this->_condition->getMappedSqlField());
41  }
42 
47  {
48  return [
49  // value, operator, valueForValidate, expectedResult
50  [1, '==', new \stdClass(), false],
51  [new \stdClass(), '==', new \stdClass(), false],
52 
53  [1, '==', 1, true],
54  [0, '==', 1, false],
55  ['0', '==', 1, false],
56  ['1', '==', 1, true],
57  ['x', '==', 'x', true],
58  ['x', '==', 0, false],
59 
60  [1, '!=', 1, false],
61  [0, '!=', 1, true],
62  ['0', '!=', 1, true],
63  ['1', '!=', 1, false],
64  ['x', '!=', 'x', false],
65  ['x', '!=', 0, true],
66 
67  [1, '==', [1], true],
68  [1, '!=', [1], false],
69  [1, '==', [3, 1, 5], false],
70  [1, '!=', [1, 5], true],
71 
72  [[1,2,3], '==', '1,2,3', false],
73  [[1], '==', 1, false],
74 
75  // Note: validated value is on the right, so read expression in the array from right to left
76  // e.g.: 1, <=, 0 actually is 0 <= 1.
77  [1, '>', 1, false],
78  [1, '<=', 1, true],
79  [1, '<=', '1', true],
80  [1, '<=', 0, true],
81  [0, '>', [1], false],
82 
83  [1, '<', 1, false],
84  [1, '>=', 1, true],
85  [1, '>=', '1', true],
86  [1, '>=', 0, false],
87  [0, '<', [1], false],
88  ];
89  }
90 
99  public function testValidateAttribute($existingValue, $operator, $valueForValidate, $expectedResult)
100  {
101  $this->_condition->setOperator($operator);
102  $this->_condition->setData('value_parsed', $existingValue);
103  $this->assertEquals(
104  $expectedResult,
105  $this->_condition->validateAttribute($valueForValidate),
106  "Failed asserting that "
107  . var_export($existingValue, true)
108  . $operator
109  . var_export($valueForValidate, true)
110  );
111  }
112 
121  public function testValidate($existingValue, $operator, $valueForValidate, $expectedResult)
122  {
123  $objectMock = $this->createPartialMock(
124  \Magento\Framework\Model\AbstractModel::class,
125  ['hasData', 'load', 'getId', 'getData']
126  );
127  $objectMock->expects($this->once())
128  ->method('hasData')
129  ->willReturn(false);
130  $objectMock->expects($this->once())
131  ->method('getId')
132  ->willReturn(7);
133  $objectMock->expects($this->once())
134  ->method('load')
135  ->with(7);
136  $objectMock->expects($this->once())
137  ->method('getData')
138  ->willReturn($valueForValidate);
139 
140  $this->_condition->setOperator($operator);
141  $this->_condition->setData('value_parsed', $existingValue);
142  $this->assertEquals(
143  $expectedResult,
144  $this->_condition->validate($objectMock),
145  "Failed asserting that "
146  . var_export($existingValue, true)
147  . $operator
148  . var_export($valueForValidate, true)
149  );
150  }
151 
156  {
157  return [
158  // value, operator, valueForValidate, expectedResult, inputType
159  [[1, 2, 3], '==', [2, 1, 3], true, 'multiselect'],
160  [[1, 2], '==', [2, 3], true, 'multiselect'],
161  [[1, 1, 3], '==', [2, 4], false, 'multiselect'],
162  [[1, 2], '!=', [2, 3], false, 'multiselect'],
163  [[1, 2], '!=', 1, false, 'multiselect'],
164 
165  [[1, 2, 3], '{}', '1', true, 'grid'],
166  [[1, 2, 3], '{}', '8', false, 'grid'],
167  [[1, 2, 3], '{}', 5, false, 'grid'],
168  [[1, 2, 3], '{}', [2, 3, 4], true, 'grid'],
169  [[1, 2, 3], '{}', [4], false, 'grid'],
170  [[3], '{}', [], false, 'grid'],
171  [1, '{}', 1, false, 'grid'],
172  [1, '!{}', [1, 2, 3], false, 'grid'],
173  [[1], '{}', null, false, 'grid'],
174  [null, '{}', null, true, 'input'],
175  [null, '!{}', null, false, 'input'],
176  [null, '{}', [1], false, 'input'],
177 
178  [[1, 2, 3], '()', 1, true, 'select'],
179  [[1, 2, 3], '!()', 1, false, 'select'],
180  [[1], '()', 3, false, 'select'],
181  [[1], '!()', 3, true, 'select'],
182  [3, '()', 3, false, 'select'],
183  [[3], '()', [3], true, 'select'],
184  [3, '()', [3], false, 'select'],
185 
186  ];
187  }
188 
199  $existingValue,
200  $operator,
201  $valueForValidate,
202  $expectedResult,
203  $inputType
204  ) {
205  $this->_condition->setOperator($operator);
206  $this->_condition->setData('value_parsed', $existingValue);
207  $this->_condition->getDefaultOperatorInputByType();
208  $this->_condition
209  ->expects($this->any())
210  ->method('getInputType')
211  ->will($this->returnValue($inputType));
212 
213  $this->assertEquals(
214  $expectedResult,
215  $this->_condition->validateAttribute($valueForValidate),
216  "Failed asserting that "
217  . var_export($existingValue, true)
218  . $operator
219  . var_export($valueForValidate, true)
220  );
221  }
222 
223  public function testGetValueParsed()
224  {
225  $value = [1, 2, 3, 4, 5, 6, 7, 8, 9];
226  $this->_condition->setValue(['1,2,3,4,5,6,7,8,9']);
227  $this->_condition->setOperator('()');
228  $this->assertEquals($value, $this->_condition->getValueParsed());
229  }
230 }
testValidate($existingValue, $operator, $valueForValidate, $expectedResult)
testValidateAttribute($existingValue, $operator, $valueForValidate, $expectedResult)
$value
Definition: gender.phtml:16
testValidateArrayOperatorType( $existingValue, $operator, $valueForValidate, $expectedResult, $inputType)