AbstractCriteriaTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data\Test\Unit;
  7. use Magento\Framework\Api\CriteriaInterface;
  8. /**
  9. * Class AbstractCriteriaTest
  10. */
  11. class AbstractCriteriaTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\Data\Test\Unit\Criteria\Sample
  15. */
  16. protected $criteria;
  17. /**
  18. * Set up
  19. *
  20. * @return void
  21. */
  22. protected function setUp()
  23. {
  24. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  25. $this->criteria = $objectManager->getObject(\Magento\Framework\Data\Test\Unit\Criteria\Sample::class);
  26. }
  27. /**
  28. * Run test addField method
  29. *
  30. * @param string|array $field
  31. * @param string|null $alias
  32. * @param array $result
  33. * @return void
  34. *
  35. * @dataProvider dataProviderAddField
  36. */
  37. public function testAddField($field, $alias, array $result)
  38. {
  39. $this->criteria->addField($field, $alias);
  40. $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_FIELDS]['list']);
  41. }
  42. /**
  43. * Run test addFilter method
  44. *
  45. * @param string $name
  46. * @param string|array $field
  47. * @param string|int|array $condition
  48. * @param string $type
  49. * @param array $result
  50. * @return void
  51. *
  52. * @dataProvider dataProviderAddFilter
  53. */
  54. public function testAddFilter($name, $field, $condition, $type, array $result)
  55. {
  56. $this->criteria->addFilter($name, $field, $condition, $type);
  57. $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_FILTERS]['list']);
  58. }
  59. /**
  60. * Run test addOrder method
  61. *
  62. * @param string $field
  63. * @param string $direction
  64. * @param bool $unShift
  65. * @param array $result
  66. * @return void
  67. *
  68. * @dataProvider dataProviderAddOrder
  69. */
  70. public function testAddOrder($field, $direction, $unShift, array $result)
  71. {
  72. $this->criteria->addOrder($field, $direction, $unShift);
  73. $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_ORDERS]['list']);
  74. }
  75. /**
  76. * Run test setLimit method
  77. *
  78. * @param int $offset
  79. * @param int $size
  80. * @param array $result
  81. * @return void
  82. *
  83. * @dataProvider dataProviderSetLimit
  84. */
  85. public function testSetLimit($offset, $size, array $result)
  86. {
  87. $this->criteria->setLimit($offset, $size);
  88. $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_LIMIT]);
  89. }
  90. /**
  91. * Run test removeField method
  92. *
  93. * @param array $actualField
  94. * @param string|null $field
  95. * @param bool $isAlias Alias identifier
  96. * @param array $result
  97. * @return void
  98. *
  99. * @dataProvider dataProviderRemoveField
  100. */
  101. public function testRemoveField(array $actualField, $field, $isAlias, array $result)
  102. {
  103. list($name, $alias) = $actualField;
  104. $this->criteria->addField($name, $alias);
  105. $this->criteria->removeField($field, $isAlias);
  106. $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_FIELDS]['list']);
  107. }
  108. /**
  109. * Run test removeAllFields method
  110. *
  111. * @param array $actualField
  112. * @param array $result
  113. * @return void
  114. *
  115. * @dataProvider dataProviderRemoveAllFields
  116. */
  117. public function testRemoveAllFields(array $actualField, array $result)
  118. {
  119. list($name, $alias) = $actualField;
  120. $this->criteria->addField($name, $alias);
  121. $this->criteria->removeAllFields();
  122. $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_FIELDS]['list']);
  123. }
  124. /**
  125. * Run test removeFilter method
  126. *
  127. * @param array $actualField
  128. * @param string $name
  129. * @param array $result
  130. * @return void
  131. *
  132. * @dataProvider dataProviderRemoveFilter
  133. */
  134. public function testRemoveFilter(array $actualField, $name, array $result)
  135. {
  136. list($filterName, $field, $condition, $type) = $actualField;
  137. $this->criteria->addFilter($filterName, $field, $condition, $type);
  138. $this->criteria->removeFilter($name);
  139. $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_FILTERS]['list']);
  140. }
  141. /**
  142. * Run test removeAllFilters method
  143. *
  144. * @param array $actualField
  145. * @param array $result
  146. * @return void
  147. *
  148. * @dataProvider dataProviderRemoveAllFilters
  149. */
  150. public function testRemoveAllFilters(array $actualField, array $result)
  151. {
  152. list($filterName, $field, $condition, $type) = $actualField;
  153. $this->criteria->addFilter($filterName, $field, $condition, $type);
  154. $this->criteria->removeAllFilters();
  155. $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_FILTERS]['list']);
  156. }
  157. /**
  158. * Run test reset method
  159. *
  160. * @param array $result
  161. * @return void
  162. *
  163. * @dataProvider dataProviderReset
  164. */
  165. public function testReset(array $result)
  166. {
  167. $this->criteria->reset();
  168. $this->assertEquals($result, $this->criteria->toArray());
  169. }
  170. /**
  171. * Data provider for reset method
  172. *
  173. * @return array
  174. */
  175. public function dataProviderReset()
  176. {
  177. return [
  178. [
  179. 'result' => [
  180. 'fields' => [
  181. 'list' => [],
  182. ],
  183. 'filters' => [
  184. 'list' => [],
  185. ],
  186. 'orders' => [
  187. 'list' => [],
  188. ],
  189. 'criteria_list' => [
  190. 'list' => [],
  191. ],
  192. ],
  193. ]
  194. ];
  195. }
  196. /**
  197. * Data provider for removeAllFilters method
  198. *
  199. * @return array
  200. */
  201. public function dataProviderRemoveAllFilters()
  202. {
  203. return [
  204. [
  205. 'actualResult' => [
  206. 'test-filter-name',
  207. 'test-field-name',
  208. 'test-condition',
  209. 'test-type',
  210. ],
  211. 'result' => [],
  212. ]
  213. ];
  214. }
  215. /**
  216. * Data provider for removeFilter method
  217. *
  218. * @return array
  219. */
  220. public function dataProviderRemoveFilter()
  221. {
  222. return [
  223. [
  224. 'actualResult' => [
  225. 'test-filter-name',
  226. 'test-field-name',
  227. 'test-condition',
  228. 'test-type',
  229. ],
  230. 'name' => 'test-filter-name',
  231. 'result' => [],
  232. ]
  233. ];
  234. }
  235. /**
  236. * Data provider for removeAllFields method
  237. *
  238. * @return array
  239. */
  240. public function dataProviderRemoveAllFields()
  241. {
  242. return [
  243. [
  244. 'actualField' => [
  245. 'test-field-name',
  246. 'test-field-alias',
  247. ],
  248. 'result' => [],
  249. ]
  250. ];
  251. }
  252. /**
  253. * Data provider for removeField method
  254. *
  255. * @return array
  256. */
  257. public function dataProviderRemoveField()
  258. {
  259. return [
  260. [
  261. 'actualField' => [
  262. 'test-field-name',
  263. null,
  264. ],
  265. 'field' => 'test-field-name',
  266. 'isAlias' => false,
  267. 'result' => [],
  268. ],
  269. [
  270. 'actualField' => [
  271. '*',
  272. null,
  273. ],
  274. 'field' => '*',
  275. 'isAlias' => false,
  276. 'result' => []
  277. ],
  278. [
  279. 'actualField' => [
  280. 'test-field-name',
  281. 'test-field-alias',
  282. ],
  283. 'field' => 'test-field-alias',
  284. 'isAlias' => true,
  285. 'result' => []
  286. ]
  287. ];
  288. }
  289. /**
  290. * Data provider for setLimit method
  291. *
  292. * @return array
  293. */
  294. public function dataProviderSetLimit()
  295. {
  296. return [
  297. [
  298. 'offset' => 99,
  299. 'size' => 30,
  300. 'result' => [99, 30],
  301. ]
  302. ];
  303. }
  304. /**
  305. * Data provider for addOrder method
  306. *
  307. * @return array
  308. */
  309. public function dataProviderAddOrder()
  310. {
  311. return [
  312. [
  313. 'field' => 'test-field-name',
  314. 'direction' => 'desc',
  315. 'unShift' => false,
  316. 'result' => [
  317. 'test-field-name' => 'DESC',
  318. ],
  319. ],
  320. [
  321. 'field' => 'test-field-name',
  322. 'direction' => 'asc',
  323. 'unShift' => false,
  324. 'result' => [
  325. 'test-field-name' => 'ASC',
  326. ]
  327. ],
  328. [
  329. 'field' => 'test-field-name',
  330. 'direction' => 'fail',
  331. 'unShift' => false,
  332. 'result' => [
  333. 'test-field-name' => 'DESC',
  334. ]
  335. ]
  336. ];
  337. }
  338. /**
  339. * Data provider for addFilter
  340. *
  341. * @return array
  342. */
  343. public function dataProviderAddFilter()
  344. {
  345. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  346. return [
  347. [
  348. 'name' => 'test-filter-name',
  349. 'field' => 'test-field-name',
  350. 'condition' => 'test-condition',
  351. 'type' => 'test-type',
  352. 'result' => [
  353. 'test-filter-name' => $objectManager->getObject(
  354. \Magento\Framework\DataObject::class,
  355. [
  356. 'data' => [
  357. 'name' => 'test-filter-name',
  358. 'field' => 'test-field-name',
  359. 'condition' => 'test-condition',
  360. 'type' => 'test-type',
  361. ]
  362. ]
  363. ),
  364. ],
  365. ]
  366. ];
  367. }
  368. /**
  369. * Data provider for addField
  370. *
  371. * @return array
  372. */
  373. public function dataProviderAddField()
  374. {
  375. return [
  376. [
  377. 'field' => 'test-field-name',
  378. 'alias' => null,
  379. 'result' => [
  380. 'test-field-name' => 'test-field-name',
  381. ],
  382. ],
  383. [
  384. 'field' => '*',
  385. 'alias' => null,
  386. 'result' => [
  387. '*',
  388. ],
  389. ],
  390. [
  391. 'field' => [
  392. 'test-field-name-1',
  393. 'test-field-name-2',
  394. 'test-field-name-3',
  395. ],
  396. 'alias' => null,
  397. 'result' => [
  398. 'test-field-name-1' => 'test-field-name-1',
  399. 'test-field-name-2' => 'test-field-name-2',
  400. 'test-field-name-3' => 'test-field-name-3',
  401. ]
  402. ],
  403. [
  404. 'field' => 'test-field-name',
  405. 'alias' => 'alias-test',
  406. 'result' => [
  407. 'alias-test' => 'test-field-name',
  408. ]
  409. ],
  410. [
  411. 'field' => '*',
  412. 'alias' => null,
  413. 'result' => [
  414. '*',
  415. ]
  416. ],
  417. [
  418. 'field' => [
  419. 'alias-1' => 'test-field-name',
  420. 'alias-2' => 'test-field-name',
  421. 'alias-3' => 'test-field-name',
  422. ],
  423. 'alias' => null,
  424. 'result' => [
  425. 'alias-1' => 'test-field-name',
  426. 'alias-2' => 'test-field-name',
  427. 'alias-3' => 'test-field-name',
  428. ]
  429. ]
  430. ];
  431. }
  432. }