AbstractConditionTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Rule\Test\Unit\Model\Condition;
  7. class AbstractConditionTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var AbstractCondition|\PHPUnit_Framework_MockObject_MockObject
  11. */
  12. protected $_condition;
  13. protected function setUp()
  14. {
  15. $this->_condition = $this->getMockForAbstractClass(
  16. \Magento\Rule\Model\Condition\AbstractCondition::class,
  17. [],
  18. '',
  19. false,
  20. false,
  21. true,
  22. ['getInputType']
  23. );
  24. }
  25. public function testGetjointTables()
  26. {
  27. $this->_condition->setAttribute('category_ids');
  28. $this->assertEquals([], $this->_condition->getTablesToJoin());
  29. $this->_condition->setAttribute('gdsjkfghksldjfg');
  30. $this->assertEmpty($this->_condition->getTablesToJoin());
  31. }
  32. public function testGetMappedSqlField()
  33. {
  34. $this->_condition->setAttribute('category_ids');
  35. $this->assertEquals('category_ids', $this->_condition->getMappedSqlField());
  36. }
  37. /**
  38. * @return array
  39. */
  40. public function validateAttributeDataProvider()
  41. {
  42. return [
  43. // value, operator, valueForValidate, expectedResult
  44. [1, '==', new \stdClass(), false],
  45. [new \stdClass(), '==', new \stdClass(), false],
  46. [1, '==', 1, true],
  47. [0, '==', 1, false],
  48. ['0', '==', 1, false],
  49. ['1', '==', 1, true],
  50. ['x', '==', 'x', true],
  51. ['x', '==', 0, false],
  52. [1, '!=', 1, false],
  53. [0, '!=', 1, true],
  54. ['0', '!=', 1, true],
  55. ['1', '!=', 1, false],
  56. ['x', '!=', 'x', false],
  57. ['x', '!=', 0, true],
  58. [1, '==', [1], true],
  59. [1, '!=', [1], false],
  60. [1, '==', [3, 1, 5], false],
  61. [1, '!=', [1, 5], true],
  62. [[1,2,3], '==', '1,2,3', false],
  63. [[1], '==', 1, false],
  64. // Note: validated value is on the right, so read expression in the array from right to left
  65. // e.g.: 1, <=, 0 actually is 0 <= 1.
  66. [1, '>', 1, false],
  67. [1, '<=', 1, true],
  68. [1, '<=', '1', true],
  69. [1, '<=', 0, true],
  70. [0, '>', [1], false],
  71. [1, '<', 1, false],
  72. [1, '>=', 1, true],
  73. [1, '>=', '1', true],
  74. [1, '>=', 0, false],
  75. [0, '<', [1], false],
  76. ];
  77. }
  78. /**
  79. * @param $existingValue
  80. * @param $operator
  81. * @param $valueForValidate
  82. * @param $expectedResult
  83. *
  84. * @dataProvider validateAttributeDataProvider
  85. */
  86. public function testValidateAttribute($existingValue, $operator, $valueForValidate, $expectedResult)
  87. {
  88. $this->_condition->setOperator($operator);
  89. $this->_condition->setData('value_parsed', $existingValue);
  90. $this->assertEquals(
  91. $expectedResult,
  92. $this->_condition->validateAttribute($valueForValidate),
  93. "Failed asserting that "
  94. . var_export($existingValue, true)
  95. . $operator
  96. . var_export($valueForValidate, true)
  97. );
  98. }
  99. /**
  100. * @param $existingValue
  101. * @param $operator
  102. * @param $valueForValidate
  103. * @param $expectedResult
  104. *
  105. * @dataProvider validateAttributeDataProvider
  106. */
  107. public function testValidate($existingValue, $operator, $valueForValidate, $expectedResult)
  108. {
  109. $objectMock = $this->createPartialMock(
  110. \Magento\Framework\Model\AbstractModel::class,
  111. ['hasData', 'load', 'getId', 'getData']
  112. );
  113. $objectMock->expects($this->once())
  114. ->method('hasData')
  115. ->willReturn(false);
  116. $objectMock->expects($this->once())
  117. ->method('getId')
  118. ->willReturn(7);
  119. $objectMock->expects($this->once())
  120. ->method('load')
  121. ->with(7);
  122. $objectMock->expects($this->once())
  123. ->method('getData')
  124. ->willReturn($valueForValidate);
  125. $this->_condition->setOperator($operator);
  126. $this->_condition->setData('value_parsed', $existingValue);
  127. $this->assertEquals(
  128. $expectedResult,
  129. $this->_condition->validate($objectMock),
  130. "Failed asserting that "
  131. . var_export($existingValue, true)
  132. . $operator
  133. . var_export($valueForValidate, true)
  134. );
  135. }
  136. /**
  137. * @return array
  138. */
  139. public function validateAttributeArrayInputTypeDataProvider()
  140. {
  141. return [
  142. // value, operator, valueForValidate, expectedResult, inputType
  143. [[1, 2, 3], '==', [2, 1, 3], true, 'multiselect'],
  144. [[1, 2], '==', [2, 3], true, 'multiselect'],
  145. [[1, 1, 3], '==', [2, 4], false, 'multiselect'],
  146. [[1, 2], '!=', [2, 3], false, 'multiselect'],
  147. [[1, 2], '!=', 1, false, 'multiselect'],
  148. [[1, 2, 3], '{}', '1', true, 'grid'],
  149. [[1, 2, 3], '{}', '8', false, 'grid'],
  150. [[1, 2, 3], '{}', 5, false, 'grid'],
  151. [[1, 2, 3], '{}', [2, 3, 4], true, 'grid'],
  152. [[1, 2, 3], '{}', [4], false, 'grid'],
  153. [[3], '{}', [], false, 'grid'],
  154. [1, '{}', 1, false, 'grid'],
  155. [1, '!{}', [1, 2, 3], false, 'grid'],
  156. [[1], '{}', null, false, 'grid'],
  157. [null, '{}', null, true, 'input'],
  158. [null, '!{}', null, false, 'input'],
  159. [null, '{}', [1], false, 'input'],
  160. [[1, 2, 3], '()', 1, true, 'select'],
  161. [[1, 2, 3], '!()', 1, false, 'select'],
  162. [[1], '()', 3, false, 'select'],
  163. [[1], '!()', 3, true, 'select'],
  164. [3, '()', 3, false, 'select'],
  165. [[3], '()', [3], true, 'select'],
  166. [3, '()', [3], false, 'select'],
  167. ];
  168. }
  169. /**
  170. * @param $existingValue
  171. * @param $operator
  172. * @param $valueForValidate
  173. * @param $expectedResult
  174. * @param $inputType
  175. *
  176. * @dataProvider validateAttributeArrayInputTypeDataProvider
  177. */
  178. public function testValidateArrayOperatorType(
  179. $existingValue,
  180. $operator,
  181. $valueForValidate,
  182. $expectedResult,
  183. $inputType
  184. ) {
  185. $this->_condition->setOperator($operator);
  186. $this->_condition->setData('value_parsed', $existingValue);
  187. $this->_condition->getDefaultOperatorInputByType();
  188. $this->_condition
  189. ->expects($this->any())
  190. ->method('getInputType')
  191. ->will($this->returnValue($inputType));
  192. $this->assertEquals(
  193. $expectedResult,
  194. $this->_condition->validateAttribute($valueForValidate),
  195. "Failed asserting that "
  196. . var_export($existingValue, true)
  197. . $operator
  198. . var_export($valueForValidate, true)
  199. );
  200. }
  201. public function testGetValueParsed()
  202. {
  203. $value = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  204. $this->_condition->setValue(['1,2,3,4,5,6,7,8,9']);
  205. $this->_condition->setOperator('()');
  206. $this->assertEquals($value, $this->_condition->getValueParsed());
  207. }
  208. }