OptionManagementTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Test\Unit\Model\Entity\Attribute;
  7. class OptionManagementTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Eav\Model\Entity\Attribute\OptionManagement
  11. */
  12. protected $model;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $attributeRepositoryMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $resourceModelMock;
  21. protected function setUp()
  22. {
  23. $this->attributeRepositoryMock = $this->createMock(\Magento\Eav\Model\AttributeRepository::class);
  24. $this->resourceModelMock =
  25. $this->createMock(\Magento\Eav\Model\ResourceModel\Entity\Attribute::class);
  26. $this->model = new \Magento\Eav\Model\Entity\Attribute\OptionManagement(
  27. $this->attributeRepositoryMock,
  28. $this->resourceModelMock
  29. );
  30. }
  31. public function testAdd()
  32. {
  33. $entityType = 42;
  34. $attributeCode = 'atrCde';
  35. $optionMock = $this->getMockForAbstractClass(
  36. \Magento\Eav\Api\Data\AttributeOptionInterface::class,
  37. [],
  38. '',
  39. false,
  40. false,
  41. true,
  42. ['getSourceLabels']
  43. );
  44. $attributeMock = $this->getMockForAbstractClass(
  45. \Magento\Framework\Model\AbstractModel::class,
  46. [],
  47. '',
  48. false,
  49. false,
  50. true,
  51. ['usesSource', 'setDefault', 'setOption']
  52. );
  53. $labelMock = $this->createMock(\Magento\Eav\Api\Data\AttributeOptionLabelInterface::class);
  54. $option =
  55. ['value' => [
  56. 'id_new_option' => [
  57. 0 => 'optionLabel',
  58. 42 => 'labelLabel',
  59. ],
  60. ],
  61. 'order' => [
  62. 'id_new_option' => 'optionSortOrder',
  63. ],
  64. ];
  65. $this->attributeRepositoryMock->expects($this->once())->method('get')->with($entityType, $attributeCode)
  66. ->willReturn($attributeMock);
  67. $attributeMock->expects($this->once())->method('usesSource')->willReturn(true);
  68. $optionMock->expects($this->once())->method('getLabel')->willReturn('optionLabel');
  69. $optionMock->expects($this->once())->method('getSortOrder')->willReturn('optionSortOrder');
  70. $optionMock->expects($this->exactly(2))->method('getStoreLabels')->willReturn([$labelMock]);
  71. $labelMock->expects($this->once())->method('getStoreId')->willReturn(42);
  72. $labelMock->expects($this->once())->method('getLabel')->willReturn('labelLabel');
  73. $optionMock->expects($this->once())->method('getIsDefault')->willReturn(true);
  74. $attributeMock->expects($this->once())->method('setDefault')->with(['id_new_option']);
  75. $attributeMock->expects($this->once())->method('setOption')->with($option);
  76. $this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock);
  77. $this->assertEquals('id_new_option', $this->model->add($entityType, $attributeCode, $optionMock));
  78. }
  79. /**
  80. * @expectedException \Magento\Framework\Exception\InputException
  81. * @expectedExceptionMessage The attribute code is empty. Enter the code and try again.
  82. */
  83. public function testAddWithEmptyAttributeCode()
  84. {
  85. $entityType = 42;
  86. $attributeCode = '';
  87. $optionMock = $this->getMockForAbstractClass(
  88. \Magento\Eav\Api\Data\AttributeOptionInterface::class,
  89. [],
  90. '',
  91. false,
  92. false,
  93. true,
  94. ['getSourceLabels']
  95. );
  96. $this->resourceModelMock->expects($this->never())->method('save');
  97. $this->model->add($entityType, $attributeCode, $optionMock);
  98. }
  99. /**
  100. * @expectedException \Magento\Framework\Exception\StateException
  101. * @expectedExceptionMessage The "testAttribute" attribute doesn't work with options.
  102. */
  103. public function testAddWithWrongOptions()
  104. {
  105. $entityType = 42;
  106. $attributeCode = 'testAttribute';
  107. $optionMock = $this->getMockForAbstractClass(
  108. \Magento\Eav\Api\Data\AttributeOptionInterface::class,
  109. [],
  110. '',
  111. false,
  112. false,
  113. true,
  114. ['getSourceLabels']
  115. );
  116. $attributeMock = $this->getMockForAbstractClass(
  117. \Magento\Framework\Model\AbstractModel::class,
  118. [],
  119. '',
  120. false,
  121. false,
  122. true,
  123. ['usesSource', 'setDefault', 'setOption']
  124. );
  125. $this->attributeRepositoryMock->expects($this->once())->method('get')->with($entityType, $attributeCode)
  126. ->willReturn($attributeMock);
  127. $attributeMock->expects($this->once())->method('usesSource')->willReturn(false);
  128. $this->resourceModelMock->expects($this->never())->method('save');
  129. $this->model->add($entityType, $attributeCode, $optionMock);
  130. }
  131. /**
  132. * @expectedException \Magento\Framework\Exception\StateException
  133. * @expectedExceptionMessage The "atrCde" attribute can't be saved.
  134. */
  135. public function testAddWithCannotSaveException()
  136. {
  137. $entityType = 42;
  138. $attributeCode = 'atrCde';
  139. $optionMock = $this->getMockForAbstractClass(
  140. \Magento\Eav\Api\Data\AttributeOptionInterface::class,
  141. [],
  142. '',
  143. false,
  144. false,
  145. true,
  146. ['getSourceLabels']
  147. );
  148. $attributeMock = $this->getMockForAbstractClass(
  149. \Magento\Framework\Model\AbstractModel::class,
  150. [],
  151. '',
  152. false,
  153. false,
  154. true,
  155. ['usesSource', 'setDefault', 'setOption']
  156. );
  157. $labelMock = $this->createMock(\Magento\Eav\Api\Data\AttributeOptionLabelInterface::class);
  158. $option =
  159. ['value' => [
  160. 'id_new_option' => [
  161. 0 => 'optionLabel',
  162. 42 => 'labelLabel',
  163. ],
  164. ],
  165. 'order' => [
  166. 'id_new_option' => 'optionSortOrder',
  167. ],
  168. ];
  169. $this->attributeRepositoryMock->expects($this->once())->method('get')->with($entityType, $attributeCode)
  170. ->willReturn($attributeMock);
  171. $attributeMock->expects($this->once())->method('usesSource')->willReturn(true);
  172. $optionMock->expects($this->once())->method('getLabel')->willReturn('optionLabel');
  173. $optionMock->expects($this->once())->method('getSortOrder')->willReturn('optionSortOrder');
  174. $optionMock->expects($this->exactly(2))->method('getStoreLabels')->willReturn([$labelMock]);
  175. $labelMock->expects($this->once())->method('getStoreId')->willReturn(42);
  176. $labelMock->expects($this->once())->method('getLabel')->willReturn('labelLabel');
  177. $optionMock->expects($this->once())->method('getIsDefault')->willReturn(true);
  178. $attributeMock->expects($this->once())->method('setDefault')->with(['id_new_option']);
  179. $attributeMock->expects($this->once())->method('setOption')->with($option);
  180. $this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock)
  181. ->willThrowException(new \Exception());
  182. $this->model->add($entityType, $attributeCode, $optionMock);
  183. }
  184. public function testDelete()
  185. {
  186. $entityType = 42;
  187. $attributeCode = 'atrCode';
  188. $optionId = 'option';
  189. $attributeMock = $this->getMockForAbstractClass(
  190. \Magento\Framework\Model\AbstractModel::class,
  191. [],
  192. '',
  193. false,
  194. false,
  195. true,
  196. ['usesSource', 'getSource', 'getId', 'getOptionText', 'addData']
  197. );
  198. $removalMarker = [
  199. 'option' => [
  200. 'value' => [$optionId => []],
  201. 'delete' => [$optionId => '1'],
  202. ],
  203. ];
  204. $this->attributeRepositoryMock->expects($this->once())->method('get')->with($entityType, $attributeCode)
  205. ->willReturn($attributeMock);
  206. $attributeMock->expects($this->once())->method('usesSource')->willReturn(true);
  207. $attributeMock->expects($this->once())->method('getSource')->willReturnSelf();
  208. $attributeMock->expects($this->once())->method('getOptionText')->willReturn('optionText');
  209. $attributeMock->expects($this->never())->method('getId');
  210. $attributeMock->expects($this->once())->method('addData')->with($removalMarker);
  211. $this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock);
  212. $this->assertTrue($this->model->delete($entityType, $attributeCode, $optionId));
  213. }
  214. /**
  215. * @expectedException \Magento\Framework\Exception\StateException
  216. * @expectedExceptionMessage The "atrCode" attribute can't be saved.
  217. */
  218. public function testDeleteWithCannotSaveException()
  219. {
  220. $entityType = 42;
  221. $attributeCode = 'atrCode';
  222. $optionId = 'option';
  223. $attributeMock = $this->getMockForAbstractClass(
  224. \Magento\Framework\Model\AbstractModel::class,
  225. [],
  226. '',
  227. false,
  228. false,
  229. true,
  230. ['usesSource', 'getSource', 'getId', 'getOptionText', 'addData']
  231. );
  232. $removalMarker = [
  233. 'option' => [
  234. 'value' => [$optionId => []],
  235. 'delete' => [$optionId => '1'],
  236. ],
  237. ];
  238. $this->attributeRepositoryMock->expects($this->once())->method('get')->with($entityType, $attributeCode)
  239. ->willReturn($attributeMock);
  240. $attributeMock->expects($this->once())->method('usesSource')->willReturn(true);
  241. $attributeMock->expects($this->once())->method('getSource')->willReturnSelf();
  242. $attributeMock->expects($this->once())->method('getOptionText')->willReturn('optionText');
  243. $attributeMock->expects($this->never())->method('getId');
  244. $attributeMock->expects($this->once())->method('addData')->with($removalMarker);
  245. $this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock)
  246. ->willThrowException(new \Exception());
  247. $this->model->delete($entityType, $attributeCode, $optionId);
  248. }
  249. /**
  250. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  251. * @expectedExceptionMessage The "atrCode" attribute doesn't include an option with "option" ID.
  252. */
  253. public function testDeleteWithWrongOption()
  254. {
  255. $entityType = 42;
  256. $attributeCode = 'atrCode';
  257. $optionId = 'option';
  258. $attributeMock = $this->getMockForAbstractClass(
  259. \Magento\Framework\Model\AbstractModel::class,
  260. [],
  261. '',
  262. false,
  263. false,
  264. true,
  265. ['usesSource', 'getSource', 'getAttributeCode']
  266. );
  267. $this->attributeRepositoryMock->expects($this->once())->method('get')->with($entityType, $attributeCode)
  268. ->willReturn($attributeMock);
  269. $sourceMock = $this->getMockForAbstractClass(\Magento\Eav\Model\Entity\Attribute\Source\SourceInterface::class);
  270. $sourceMock->expects($this->once())->method('getOptionText')->willReturn(false);
  271. $attributeMock->expects($this->once())->method('usesSource')->willReturn(true);
  272. $attributeMock->expects($this->once())->method('getSource')->willReturn($sourceMock);
  273. $attributeMock->expects($this->any())->method('getAttributeCode')->willReturn($attributeCode);
  274. $this->resourceModelMock->expects($this->never())->method('save');
  275. $this->model->delete($entityType, $attributeCode, $optionId);
  276. }
  277. /**
  278. * @expectedException \Magento\Framework\Exception\StateException
  279. * @expectedExceptionMessage The "atrCode" attribute has no option.
  280. */
  281. public function testDeleteWithAbsentOption()
  282. {
  283. $entityType = 42;
  284. $attributeCode = 'atrCode';
  285. $optionId = 'option';
  286. $attributeMock = $this->getMockForAbstractClass(
  287. \Magento\Framework\Model\AbstractModel::class,
  288. [],
  289. '',
  290. false,
  291. false,
  292. true,
  293. ['usesSource', 'getSource', 'getId', 'getOptionText', 'addData']
  294. );
  295. $this->attributeRepositoryMock->expects($this->once())->method('get')->with($entityType, $attributeCode)
  296. ->willReturn($attributeMock);
  297. $attributeMock->expects($this->once())->method('usesSource')->willReturn(false);
  298. $this->resourceModelMock->expects($this->never())->method('save');
  299. $this->model->delete($entityType, $attributeCode, $optionId);
  300. }
  301. /**
  302. * @expectedException \Magento\Framework\Exception\InputException
  303. * @expectedExceptionMessage The attribute code is empty. Enter the code and try again.
  304. */
  305. public function testDeleteWithEmptyAttributeCode()
  306. {
  307. $entityType = 42;
  308. $attributeCode = '';
  309. $optionId = 'option';
  310. $this->resourceModelMock->expects($this->never())->method('save');
  311. $this->model->delete($entityType, $attributeCode, $optionId);
  312. }
  313. public function testGetItems()
  314. {
  315. $entityType = 42;
  316. $attributeCode = 'atrCode';
  317. $attributeMock = $this->getMockForAbstractClass(
  318. \Magento\Framework\Model\AbstractModel::class,
  319. [],
  320. '',
  321. false,
  322. false,
  323. true,
  324. ['getOptions']
  325. );
  326. $optionsMock = [$this->createMock(\Magento\Eav\Api\Data\AttributeOptionInterface::class)];
  327. $this->attributeRepositoryMock->expects($this->once())->method('get')->with($entityType, $attributeCode)
  328. ->willReturn($attributeMock);
  329. $attributeMock->expects($this->once())->method('getOptions')->willReturn($optionsMock);
  330. $this->assertEquals($optionsMock, $this->model->getItems($entityType, $attributeCode));
  331. }
  332. /**
  333. * @expectedException \Magento\Framework\Exception\StateException
  334. * @expectedExceptionMessage The options for "atrCode" attribute can't be loaded.
  335. */
  336. public function testGetItemsWithCannotLoadException()
  337. {
  338. $entityType = 42;
  339. $attributeCode = 'atrCode';
  340. $attributeMock = $this->getMockForAbstractClass(
  341. \Magento\Framework\Model\AbstractModel::class,
  342. [],
  343. '',
  344. false,
  345. false,
  346. true,
  347. ['getOptions']
  348. );
  349. $this->attributeRepositoryMock->expects($this->once())->method('get')->with($entityType, $attributeCode)
  350. ->willReturn($attributeMock);
  351. $attributeMock->expects($this->once())->method('getOptions')->willThrowException(new \Exception());
  352. $this->model->getItems($entityType, $attributeCode);
  353. }
  354. /**
  355. * @expectedException \Magento\Framework\Exception\InputException
  356. * @expectedExceptionMessage The attribute code is empty. Enter the code and try again.
  357. */
  358. public function testGetItemsWithEmptyAttributeCode()
  359. {
  360. $entityType = 42;
  361. $attributeCode = '';
  362. $this->model->getItems($entityType, $attributeCode);
  363. }
  364. }