AdvancedTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Test\Unit\Model;
  7. /**
  8. * Class AdvancedTest
  9. * @see \Magento\CatalogSearch\Model\Advanced
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class AdvancedTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection
  16. */
  17. protected $collection;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\CatalogSearch\Model\ResourceModel\Advanced
  20. */
  21. protected $resource;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\CatalogSearch\Model\ResourceModel\ResourceProvider
  24. */
  25. protected $resourceProvider;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject[]|\Magento\Catalog\Model\ResourceModel\Eav\Attribute[]
  28. */
  29. protected $attributes;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Data\Collection
  32. */
  33. protected $dataCollection;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Directory\Model\Currency
  36. */
  37. private $currency;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\StoreManagerInterface
  40. */
  41. private $storeManager;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\Store
  44. */
  45. private $store;
  46. protected function setUp()
  47. {
  48. $this->collection = $this->createPartialMock(
  49. \Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection::class,
  50. [
  51. 'addAttributeToSelect',
  52. 'setStore',
  53. 'addMinimalPrice',
  54. 'addTaxPercents',
  55. 'addStoreFilter',
  56. 'setVisibility',
  57. 'addFieldsToFilter'
  58. ]
  59. );
  60. $this->resource = $this->createPartialMock(
  61. \Magento\CatalogSearch\Model\ResourceModel\Advanced::class,
  62. ['prepareCondition', '__wakeup', 'getIdFieldName']
  63. );
  64. $this->resourceProvider = $this->getMockBuilder(
  65. \Magento\CatalogSearch\Model\ResourceModel\ResourceProvider::class
  66. )
  67. ->setMethods(['getResource', 'getResourceCollection', 'getAdvancedResultCollection'])
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->dataCollection = $this->createPartialMock(\Magento\Framework\Data\Collection::class, ['getIterator']);
  71. $this->currency = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
  72. ->setMethods(['getRate'])
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  76. ->setMethods(['getCurrentCurrencyCode', 'getBaseCurrencyCode', 'getBaseCurrency'])
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $this->store->expects($this->any())
  80. ->method('getBaseCurrency')
  81. ->willReturn($this->currency);
  82. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  83. ->setMethods(['getStore'])
  84. ->getMockForAbstractClass();
  85. $this->storeManager->expects($this->any())
  86. ->method('getStore')
  87. ->willReturn($this->store);
  88. }
  89. /**
  90. * @return array
  91. */
  92. public function addFiltersDataProvider()
  93. {
  94. return array_merge(
  95. [
  96. 'sku' => [
  97. 'attributes' => [
  98. $this->createAttribute(
  99. $this->createBackend('catalog_product_entity'),
  100. $this->createSource(),
  101. 'sku',
  102. 'SKU',
  103. 'text',
  104. 'static'
  105. )
  106. ],
  107. 'values' => ['sku' => 'simple']
  108. ],
  109. 'color_multiselect' => [
  110. 'attributes' => [
  111. $this->createAttribute(
  112. $this->createBackend('color_multiselect'),
  113. $this->createSource(['label' => 'Color']),
  114. 'color',
  115. 'Color',
  116. 'multiselect',
  117. 'static'
  118. )
  119. ],
  120. 'values' => ['color' => [100 => 'red', 200 => 'blue']],
  121. 'currentCurrencyCode' => 'GBP',
  122. 'baseCurrencyCode' => 'USD'
  123. ],
  124. 'color_select' => [
  125. 'attributes' => [
  126. $this->createAttribute(
  127. $this->createBackend('color_select'),
  128. $this->createSource(['label' => 'Color']),
  129. 'color',
  130. 'Color',
  131. 'select',
  132. 'static'
  133. )
  134. ],
  135. 'values' => ['color' => 'red'],
  136. 'currentCurrencyCode' => 'GBP',
  137. 'baseCurrencyCode' => 'USD'
  138. ],
  139. 'boolean' => [
  140. 'attributes' => [
  141. $this->createAttribute(
  142. $this->createBackend('boolean'),
  143. $this->createSource(['label' => 'Color']),
  144. 'is_active',
  145. 'Is active?',
  146. 'boolean',
  147. 'static'
  148. )
  149. ],
  150. 'values' => ['is_active' => 0],
  151. 'currentCurrencyCode' => 'GBP',
  152. 'baseCurrencyCode' => 'USD'
  153. ],
  154. ],
  155. $this->addFiltersPriceDataProvider()
  156. );
  157. }
  158. /**
  159. * @param array $attributes
  160. * @param array $values
  161. * @param string $currentCurrencyCode
  162. * @param string $baseCurrencyCode
  163. * @dataProvider addFiltersDataProvider
  164. */
  165. public function testAddFiltersVerifyAddConditionsToRegistry(
  166. array $attributes,
  167. array $values,
  168. $currentCurrencyCode = 'GBP',
  169. $baseCurrencyCode = 'USD'
  170. ) {
  171. $registry = new \Magento\Framework\Registry();
  172. $this->collection->expects($this->any())->method('addAttributeToSelect')->will($this->returnSelf());
  173. $this->collection->expects($this->any())->method('setStore')->will($this->returnSelf());
  174. $this->collection->expects($this->any())->method('addMinimalPrice')->will($this->returnSelf());
  175. $this->collection->expects($this->any())->method('addTaxPercents')->will($this->returnSelf());
  176. $this->collection->expects($this->any())->method('addStoreFilter')->will($this->returnSelf());
  177. $this->collection->expects($this->any())->method('setVisibility')->will($this->returnSelf());
  178. $this->resource->expects($this->any())->method('prepareCondition')
  179. ->will($this->returnValue(['like' => '%simple%']));
  180. $this->resource->expects($this->any())->method('getIdFieldName')->will($this->returnValue('entity_id'));
  181. $this->dataCollection->expects($this->any())->method('getIterator')
  182. ->will($this->returnValue(new \ArrayIterator($attributes)));
  183. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  184. $advancedFactory = $this->getMockBuilder(\Magento\CatalogSearch\Model\ResourceModel\AdvancedFactory::class)
  185. ->setMethods(['create'])
  186. ->disableOriginalConstructor()
  187. ->getMock();
  188. $advancedFactory->expects($this->once())->method('create')->willReturn($this->resource);
  189. $productCollectionFactory =
  190. $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class)
  191. ->setMethods(['create'])
  192. ->disableOriginalConstructor()
  193. ->getMock();
  194. $productCollectionFactory->expects($this->any())->method('create')->willReturn($this->collection);
  195. $this->store->expects($this->any())
  196. ->method('getCurrentCurrencyCode')
  197. ->willReturn($currentCurrencyCode);
  198. $this->store->expects($this->any())
  199. ->method('getBaseCurrencyCode')
  200. ->willReturn($baseCurrencyCode);
  201. $this->currency->expects($this->any())
  202. ->method('getRate')
  203. ->with($currentCurrencyCode)
  204. ->willReturn(1.5);
  205. $currency = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
  206. ->setMethods(['load', 'format'])
  207. ->disableOriginalConstructor()
  208. ->getMock();
  209. $currency->expects($this->any())
  210. ->method('load')
  211. ->willReturnSelf();
  212. $currency->expects($this->any())
  213. ->method('format')
  214. ->willReturnArgument(0);
  215. $currencyFactory = $this->getMockBuilder(\Magento\Directory\Model\CurrencyFactory::class)
  216. ->setMethods(['create'])
  217. ->disableOriginalConstructor()
  218. ->getMock();
  219. $currencyFactory->expects($this->any())
  220. ->method('create')
  221. ->willReturn($currency);
  222. /** @var \Magento\CatalogSearch\Model\Advanced $instance */
  223. $instance = $objectManager->getObject(
  224. \Magento\CatalogSearch\Model\Advanced::class,
  225. [
  226. 'registry' => $registry,
  227. 'resourceProvider' => $this->resourceProvider,
  228. 'data' => ['attributes' => $this->dataCollection],
  229. 'advancedFactory' => $advancedFactory,
  230. 'productCollectionFactory' => $productCollectionFactory,
  231. 'storeManager' => $this->storeManager,
  232. 'currencyFactory' => $currencyFactory,
  233. ]
  234. );
  235. $instance->addFilters($values);
  236. $this->assertNotNull($registry->registry('advanced_search_conditions'));
  237. }
  238. /**
  239. * @param $table
  240. * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
  241. */
  242. private function createBackend($table)
  243. {
  244. $backend = $this->createPartialMock(
  245. \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend::class,
  246. ['getTable']
  247. );
  248. $backend->expects($this->once())
  249. ->method('getTable')
  250. ->willReturn($table);
  251. return $backend;
  252. }
  253. /**
  254. * @param string $optionText
  255. * @return \PHPUnit_Framework_MockObject_MockObject
  256. */
  257. private function createSource($optionText = 'optionText')
  258. {
  259. $source = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::class)
  260. ->setMethods(['getOptionText'])
  261. ->disableOriginalConstructor()
  262. ->getMockForAbstractClass();
  263. $source->expects($this->any())
  264. ->method('getOptionText')
  265. ->willReturn($optionText);
  266. return $source;
  267. }
  268. /**
  269. * @return array
  270. */
  271. private function addFiltersPriceDataProvider()
  272. {
  273. return [
  274. 'price_without_currency' => [
  275. 'attributes' => [
  276. $this->createAttribute(
  277. $this->createBackend('table_price_without_currency'),
  278. $this->createSource(),
  279. 'price',
  280. 'Price',
  281. 'multiselect',
  282. 'static'
  283. )
  284. ],
  285. 'values' => ['price' => ['from' => 10, 'to' => 40]],
  286. 'currentCurrencyCode' => 'GBP',
  287. 'baseCurrencyCode' => 'USD'
  288. ],
  289. 'price_without_to' => [
  290. 'attributes' => [
  291. $this->createAttribute(
  292. $this->createBackend('price_without_to'),
  293. $this->createSource(),
  294. 'price',
  295. 'Price',
  296. 'multiselect',
  297. 'static'
  298. )
  299. ],
  300. 'values' => ['price' => ['from' => 10, 'to' => '']],
  301. 'currentCurrencyCode' => 'GBP',
  302. 'baseCurrencyCode' => 'USD'
  303. ],
  304. 'price_without_from' => [
  305. 'attributes' => [
  306. $this->createAttribute(
  307. $this->createBackend('price_without_from'),
  308. $this->createSource(),
  309. 'price',
  310. 'Price',
  311. 'multiselect',
  312. 'static'
  313. )
  314. ],
  315. 'values' => ['price' => ['from' => '', 'to' => 30]],
  316. 'currentCurrencyCode' => 'GBP',
  317. 'baseCurrencyCode' => 'USD'
  318. ],
  319. 'price_empty' => [
  320. 'attributes' => [
  321. $this->createAttribute(
  322. $this->createBackend('price_empty'),
  323. $this->createSource(),
  324. 'price',
  325. 'Price',
  326. 'multiselect',
  327. 'static'
  328. )
  329. ],
  330. 'values' => ['price' => ['from' => '', 'to' => '']],
  331. 'currentCurrencyCode' => 'GBP',
  332. 'baseCurrencyCode' => 'USD'
  333. ],
  334. 'price_with_currency' => [
  335. 'attributes' => [
  336. $this->createAttribute(
  337. $this->createBackend('price_with_currency'),
  338. $this->createSource(),
  339. 'price',
  340. 'Price',
  341. 'multiselect',
  342. 'static'
  343. )
  344. ],
  345. 'values' => ['price' => ['from' => 10, 'to' => 40, 'currency' => 'ASD']],
  346. 'currentCurrencyCode' => 'GBP',
  347. 'baseCurrencyCode' => 'USD'
  348. ]
  349. ];
  350. }
  351. /**
  352. * @param $backend
  353. * @param null $source
  354. * @param null $attributeCode
  355. * @param null $storeLabel
  356. * @param null $frontendInput
  357. * @param null $backendType
  358. * @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute|\PHPUnit_Framework_MockObject_MockObject
  359. */
  360. private function createAttribute(
  361. $backend,
  362. $source = null,
  363. $attributeCode = null,
  364. $storeLabel = null,
  365. $frontendInput = null,
  366. $backendType = null
  367. ) {
  368. $attribute = $this->createPartialMock(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class, [
  369. 'getAttributeCode',
  370. 'getStoreLabel',
  371. 'getFrontendInput',
  372. 'getBackend',
  373. 'getBackendType',
  374. 'getSource',
  375. '__wakeup'
  376. ]);
  377. $attribute->expects($this->any())->method('getBackend')->willReturn($backend);
  378. $attribute->expects($this->any())->method('getSource')->willReturn($source);
  379. $attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode));
  380. $attribute->expects($this->any())->method('getStoreLabel')->will($this->returnValue($storeLabel));
  381. $attribute->expects($this->any())->method('getFrontendInput')->will($this->returnValue($frontendInput));
  382. $attribute->expects($this->any())->method('getBackendType')->will($this->returnValue($backendType));
  383. return $attribute;
  384. }
  385. }