AttributeProviderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\Indexer;
  7. use Magento\Customer\Model\Customer;
  8. use Magento\Customer\Model\Indexer\AttributeProvider;
  9. use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection;
  10. class AttributeProviderTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $eavConfig;
  16. /**
  17. * @var AttributeProvider
  18. */
  19. protected $object;
  20. protected function setUp()
  21. {
  22. $this->eavConfig = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
  23. ->disableOriginalConstructor()
  24. ->getMock();
  25. $this->object = new AttributeProvider(
  26. $this->eavConfig
  27. );
  28. }
  29. /**
  30. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  31. */
  32. public function testAddDynamicData()
  33. {
  34. $existentName = 'field';
  35. $existentField = [
  36. 'name' => $existentName,
  37. 'handler' => 'handler',
  38. 'origin' => $existentName,
  39. 'type' => 'type',
  40. 'filters' => ['filter'],
  41. 'dataType' => 'data_type',
  42. ];
  43. $data = ['fields' => [$existentName => $existentField]];
  44. $attrName = 'attrName';
  45. $attrBackendType = 'b_type';
  46. $attrFrontendInput = 'int';
  47. /** @var \Magento\Eav\Model\Entity\Type|\PHPUnit_Framework_MockObject_MockObject $collectionMock $entityType */
  48. $entityType = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
  52. $collectionMock = $this->getMockBuilder(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. /** @var \Magento\Customer\Model\ResourceModel\Customer|\PHPUnit_Framework_MockObject_MockObject $entity */
  56. $entity = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. /** @var \Magento\Customer\Model\Attribute|\PHPUnit_Framework_MockObject_MockObject $attribute */
  60. $attribute = $this->getMockBuilder(\Magento\Customer\Model\Attribute::class)
  61. ->disableOriginalConstructor()
  62. ->setMethods(
  63. [
  64. 'setEntity',
  65. 'getName',
  66. 'getFrontendInput',
  67. 'getBackendType',
  68. 'canBeSearchableInGrid',
  69. 'canBeFilterableInGrid',
  70. 'getData',
  71. ]
  72. )
  73. ->getMock();
  74. $this->eavConfig->expects($this->once())
  75. ->method('getEntityType')
  76. ->with(Customer::ENTITY)
  77. ->willReturn($entityType);
  78. $entityType->expects($this->once())
  79. ->method('getEntity')
  80. ->willReturn($entity);
  81. $entityType->expects($this->once())
  82. ->method('getAttributeCollection')
  83. ->willReturn($collectionMock);
  84. $collectionMock->expects($this->once())
  85. ->method('getItems')
  86. ->willReturn([$attribute]);
  87. $attribute->expects($this->once())
  88. ->method('setEntity')
  89. ->with($entity)
  90. ->willReturnSelf();
  91. $attribute->expects($this->exactly(3))
  92. ->method('getName')
  93. ->willReturn($attrName);
  94. $attribute->expects($this->any())
  95. ->method('getBackendType')
  96. ->willReturn($attrBackendType);
  97. $attribute->expects($this->any())
  98. ->method('getFrontendInput')
  99. ->willReturn($attrFrontendInput);
  100. $attribute->expects($this->any())
  101. ->method('canBeSearchableInGrid')
  102. ->willReturn(false);
  103. $attribute->expects($this->any())
  104. ->method('canBeFilterableInGrid')
  105. ->willReturn(false);
  106. $attribute->expects($this->any())
  107. ->method('getData')
  108. ->willReturnMap(
  109. [
  110. ['is_used_in_grid', null, true],
  111. ]
  112. );
  113. $this->assertEquals(
  114. ['fields' =>
  115. [
  116. $existentName => $existentField,
  117. $attrName => [
  118. 'name' => $attrName,
  119. 'handler' => \Magento\Framework\Indexer\Handler\AttributeHandler::class,
  120. 'origin' => $attrName,
  121. 'type' => 'virtual',
  122. 'filters' => [],
  123. 'dataType' => $attrBackendType,
  124. 'entity' => Customer::ENTITY,
  125. 'bind' => null,
  126. ],
  127. ],
  128. ],
  129. $this->object->addDynamicData($data)
  130. );
  131. }
  132. public function testAddDynamicDataWithStaticAndSearchable()
  133. {
  134. $existentName = 'field';
  135. $existentField = [
  136. 'name' => $existentName,
  137. 'handler' => 'handler',
  138. 'origin' => $existentName,
  139. 'type' => 'searchable',
  140. 'filters' => ['filter'],
  141. 'dataType' => 'data_type',
  142. ];
  143. $data = ['fields' => [$existentName => $existentField]];
  144. $attrName = $existentName;
  145. $attrBackendType = 'static';
  146. $attrFrontendInput = 'text';
  147. /** @var \Magento\Eav\Model\Entity\Type|\PHPUnit_Framework_MockObject_MockObject $collectionMock $entityType */
  148. $entityType = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
  149. ->disableOriginalConstructor()
  150. ->getMock();
  151. /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
  152. $collectionMock = $this->getMockBuilder(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class)
  153. ->disableOriginalConstructor()
  154. ->getMock();
  155. /** @var \Magento\Customer\Model\ResourceModel\Customer|\PHPUnit_Framework_MockObject_MockObject $entity */
  156. $entity = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer::class)
  157. ->disableOriginalConstructor()
  158. ->getMock();
  159. /** @var \Magento\Customer\Model\Attribute|\PHPUnit_Framework_MockObject_MockObject $attribute */
  160. $attribute = $this->getMockBuilder(\Magento\Customer\Model\Attribute::class)
  161. ->disableOriginalConstructor()
  162. ->setMethods(
  163. [
  164. 'setEntity',
  165. 'getName',
  166. 'getFrontendInput',
  167. 'getBackendType',
  168. 'canBeSearchableInGrid',
  169. 'canBeFilterableInGrid',
  170. 'getData',
  171. ]
  172. )
  173. ->getMock();
  174. $this->eavConfig->expects($this->once())
  175. ->method('getEntityType')
  176. ->with(Customer::ENTITY)
  177. ->willReturn($entityType);
  178. $entityType->expects($this->once())
  179. ->method('getAttributeCollection')
  180. ->willReturn($collectionMock);
  181. $collectionMock->expects($this->once())
  182. ->method('getItems')
  183. ->willReturn([$attribute]);
  184. $entityType->expects($this->once())
  185. ->method('getEntity')
  186. ->willReturn($entity);
  187. $attribute->expects($this->once())
  188. ->method('setEntity')
  189. ->with($entity)
  190. ->willReturnSelf();
  191. $attribute->expects($this->any())
  192. ->method('getFrontendInput')
  193. ->willReturn($attrFrontendInput);
  194. $attribute->expects($this->any())
  195. ->method('getBackendType')
  196. ->willReturn($attrBackendType);
  197. $attribute->expects($this->any())
  198. ->method('canBeSearchableInGrid')
  199. ->willReturn(true);
  200. $attribute->expects($this->never())
  201. ->method('canBeFilterableInGrid');
  202. $this->assertEquals(
  203. ['fields' =>
  204. [
  205. $attrName => [
  206. 'name' => $attrName,
  207. 'handler' => 'handler',
  208. 'origin' => $attrName,
  209. 'type' => 'searchable',
  210. 'filters' => ['filter'],
  211. 'dataType' => 'data_type',
  212. ],
  213. ],
  214. ],
  215. $this->object->addDynamicData($data)
  216. );
  217. }
  218. /**
  219. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  220. */
  221. public function testAddDynamicDataWithStaticAndFilterable()
  222. {
  223. $existentName = 'field';
  224. $existentField = [
  225. 'name' => $existentName,
  226. 'handler' => 'handler',
  227. 'origin' => $existentName,
  228. 'type' => 'type',
  229. 'filters' => ['filter'],
  230. 'dataType' => 'data_type',
  231. ];
  232. $data = [
  233. 'fields' => [$existentName => $existentField],
  234. 'references' => [
  235. 'customer' => [
  236. 'to' => 'to_field',
  237. ],
  238. ],
  239. ];
  240. $attrName = $existentName;
  241. $attrBackendType = 'varchar';
  242. $attrFrontendInput = 'text';
  243. /** @var \Magento\Eav\Model\Entity\Type|\PHPUnit_Framework_MockObject_MockObject $collectionMock $entityType */
  244. $entityType = $this->getMockBuilder(\Magento\Eav\Model\Entity\Type::class)
  245. ->disableOriginalConstructor()
  246. ->getMock();
  247. /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
  248. $collectionMock = $this->getMockBuilder(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class)
  249. ->disableOriginalConstructor()
  250. ->getMock();
  251. /** @var \Magento\Customer\Model\ResourceModel\Customer|\PHPUnit_Framework_MockObject_MockObject $entity */
  252. $entity = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer::class)
  253. ->disableOriginalConstructor()
  254. ->getMock();
  255. /** @var \Magento\Customer\Model\Attribute|\PHPUnit_Framework_MockObject_MockObject $attribute */
  256. $attribute = $this->getMockBuilder(\Magento\Customer\Model\Attribute::class)
  257. ->disableOriginalConstructor()
  258. ->setMethods(
  259. [
  260. 'setEntity',
  261. 'getName',
  262. 'getFrontendInput',
  263. 'getBackendType',
  264. 'canBeSearchableInGrid',
  265. 'canBeFilterableInGrid',
  266. 'getData',
  267. ]
  268. )
  269. ->getMock();
  270. $this->eavConfig->expects($this->once())
  271. ->method('getEntityType')
  272. ->with(Customer::ENTITY)
  273. ->willReturn($entityType);
  274. $entityType->expects($this->once())
  275. ->method('getAttributeCollection')
  276. ->willReturn($collectionMock);
  277. $collectionMock->expects($this->once())
  278. ->method('getItems')
  279. ->willReturn([$attribute]);
  280. $entityType->expects($this->once())
  281. ->method('getEntity')
  282. ->willReturn($entity);
  283. $attribute->expects($this->once())
  284. ->method('setEntity')
  285. ->with($entity)
  286. ->willReturnSelf();
  287. $attribute->expects($this->exactly(3))
  288. ->method('getName')
  289. ->willReturn($attrName);
  290. $attribute->expects($this->any())
  291. ->method('getFrontendInput')
  292. ->willReturn($attrFrontendInput);
  293. $attribute->expects($this->any())
  294. ->method('getBackendType')
  295. ->willReturn($attrBackendType);
  296. $attribute->expects($this->any())
  297. ->method('canBeSearchableInGrid')
  298. ->willReturn(false);
  299. $attribute->expects($this->any())
  300. ->method('canBeFilterableInGrid')
  301. ->willReturn(true);
  302. $attribute->expects($this->any())
  303. ->method('getData')
  304. ->willReturnMap(
  305. [
  306. ['is_used_in_grid', null, true],
  307. ]
  308. );
  309. $this->assertEquals(
  310. ['fields' =>
  311. [
  312. $attrName => [
  313. 'name' => $attrName,
  314. 'handler' => \Magento\Framework\Indexer\Handler\AttributeHandler::class,
  315. 'origin' => $attrName,
  316. 'type' => 'filterable',
  317. 'filters' => [],
  318. 'dataType' => 'varchar',
  319. 'entity' => Customer::ENTITY,
  320. 'bind' => 'to_field',
  321. ],
  322. ],
  323. 'references' => [
  324. 'customer' => [
  325. 'to' => 'to_field',
  326. ],
  327. ],
  328. ],
  329. $this->object->addDynamicData($data)
  330. );
  331. }
  332. }