AbstractExtensibleModelTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Model\Test\Unit;
  7. use Magento\Framework\Api\AttributeValue;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class AbstractExtensibleModelTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\Model\AbstractExtensibleModel
  15. */
  16. protected $model;
  17. /**
  18. * @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $contextMock;
  21. /**
  22. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $registryMock;
  25. /**
  26. * @var \Magento\Framework\Model\ResourceModel\Db\AbstractDb|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $resourceMock;
  29. /**
  30. * @var \Magento\Framework\Data\Collection\AbstractDb|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $resourceCollectionMock;
  33. /** @var \Magento\Framework\Api\MetadataServiceInterface|\PHPUnit_Framework_MockObject_MockObject */
  34. protected $metadataServiceMock;
  35. /** @var \Magento\Framework\Api\AttributeValueFactory|\PHPUnit_Framework_MockObject_MockObject */
  36. protected $attributeValueFactoryMock;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $actionValidatorMock;
  41. /**
  42. * @var AttributeValue
  43. */
  44. protected $customAttribute;
  45. protected function setUp()
  46. {
  47. $this->actionValidatorMock = $this->createMock(\Magento\Framework\Model\ActionValidator\RemoveAction::class);
  48. $this->contextMock = new \Magento\Framework\Model\Context(
  49. $this->createMock(\Psr\Log\LoggerInterface::class),
  50. $this->createMock(\Magento\Framework\Event\ManagerInterface::class),
  51. $this->createMock(\Magento\Framework\App\CacheInterface::class),
  52. $this->createMock(\Magento\Framework\App\State::class),
  53. $this->actionValidatorMock
  54. );
  55. $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
  56. $this->resourceMock = $this->createPartialMock(\Magento\Framework\Model\ResourceModel\Db\AbstractDb::class, [
  57. '_construct',
  58. 'getConnection',
  59. '__wakeup',
  60. 'commit',
  61. 'delete',
  62. 'getIdFieldName',
  63. 'rollBack'
  64. ]);
  65. $this->resourceCollectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
  66. ->disableOriginalConstructor()
  67. ->getMockForAbstractClass();
  68. $this->metadataServiceMock = $this->getMockBuilder(\Magento\Framework\Api\MetadataServiceInterface::class)
  69. ->getMock();
  70. $this->metadataServiceMock
  71. ->expects($this->any())
  72. ->method('getCustomAttributesMetadata')
  73. ->willReturn(
  74. [
  75. new \Magento\Framework\DataObject(['attribute_code' => 'attribute1']),
  76. new \Magento\Framework\DataObject(['attribute_code' => 'attribute2']),
  77. new \Magento\Framework\DataObject(['attribute_code' => 'attribute3']),
  78. ]
  79. );
  80. $extensionAttributesFactory = $this->getMockBuilder(\Magento\Framework\Api\ExtensionAttributesFactory::class)
  81. ->setMethods(['extractExtensionAttributes'])
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $extensionAttributesFactory->expects($this->any())
  85. ->method('extractExtensionAttributes')
  86. ->willReturnArgument(1);
  87. $this->attributeValueFactoryMock = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class)
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $this->model = $this->getMockForAbstractClass(
  91. \Magento\Framework\Model\AbstractExtensibleModel::class,
  92. [
  93. $this->contextMock,
  94. $this->registryMock,
  95. $extensionAttributesFactory,
  96. $this->attributeValueFactoryMock,
  97. $this->resourceMock,
  98. $this->resourceCollectionMock
  99. ],
  100. '',
  101. true,
  102. true,
  103. true,
  104. ['getCustomAttributesCodes']
  105. );
  106. $this->customAttribute = new AttributeValue();
  107. }
  108. /**
  109. * Test implementation of interface for work with custom attributes.
  110. */
  111. public function testCustomAttributesWithEmptyCustomAttributes()
  112. {
  113. $this->model->expects($this->any())->method('getCustomAttributesCodes')->willReturn([]);
  114. $this->assertEquals(
  115. [],
  116. $this->model->getCustomAttributes(),
  117. "Empty array is expected as a result of getCustomAttributes() when custom attributes are not set."
  118. );
  119. $this->assertEquals(
  120. null,
  121. $this->model->getCustomAttribute('not_existing_custom_attribute'),
  122. "Null is expected as a result of getCustomAttribute(\$code) when custom attribute is not set."
  123. );
  124. $attributesAsArray = ['attribute1' => true, 'attribute2' => 'Attribute Value', 'attribute3' => 333];
  125. $this->addCustomAttributesToModel($attributesAsArray, $this->model);
  126. $this->assertEquals(
  127. [],
  128. $this->model->getCustomAttributes(),
  129. 'Custom attributes retrieved from the model using getCustomAttributes() are invalid.'
  130. );
  131. }
  132. public function testCustomAttributesWithNonEmptyCustomAttributes()
  133. {
  134. $customAttributeCode = 'attribute_code';
  135. $customAttributeValue = 'attribute_value';
  136. $this->model->expects($this->any())->method('getCustomAttributesCodes')->willReturn([$customAttributeCode]);
  137. $this->assertEquals(
  138. [],
  139. $this->model->getCustomAttributes(),
  140. "Empty array is expected as a result of getCustomAttributes() when custom attributes are not set."
  141. );
  142. $this->attributeValueFactoryMock->expects($this->once())
  143. ->method('create')
  144. ->willReturn($this->customAttribute);
  145. $this->customAttribute->setAttributeCode($customAttributeCode)->setValue($customAttributeValue);
  146. $this->model->setData($customAttributeCode, $customAttributeValue);
  147. $this->assertEquals(
  148. [$this->customAttribute],
  149. $this->model->getCustomAttributes(),
  150. "One custom attribute expected"
  151. );
  152. $this->assertNotNull($this->model->getCustomAttribute($customAttributeCode), 'customer attribute expected');
  153. $this->assertEquals(
  154. $customAttributeValue,
  155. $this->model->getCustomAttribute($customAttributeCode)->getValue(),
  156. "Custom attribute value is incorrect"
  157. );
  158. //unset the data
  159. $this->model->unsetData($customAttributeCode);
  160. $this->assertEquals(
  161. [],
  162. $this->model->getCustomAttributes(),
  163. "Empty array is expected as a result of getCustomAttributes() when custom attributes are not set."
  164. );
  165. }
  166. /**
  167. * Test if getData works with custom attributes as expected
  168. */
  169. public function testGetDataWithCustomAttributes()
  170. {
  171. $this->model->expects($this->any())->method('getCustomAttributesCodes')->willReturn([]);
  172. $attributesAsArray = [
  173. 'attribute1' => true,
  174. 'attribute2' => 'Attribute Value',
  175. 'attribute3' => 333,
  176. 'invalid' => true,
  177. ];
  178. $modelData = ['key1' => 'value1', 'key2' => 222];
  179. foreach ($modelData as $key => $value) {
  180. $this->model->setData($key, $value);
  181. }
  182. $this->addCustomAttributesToModel($attributesAsArray, $this->model);
  183. $this->assertEquals(
  184. $modelData,
  185. $this->model->getData(),
  186. 'All model data should be represented as a flat array, including custom attributes.'
  187. );
  188. foreach ($modelData as $field => $value) {
  189. $this->assertEquals(
  190. $value,
  191. $this->model->getData($field),
  192. "Model data item '{$field}' was retrieved incorrectly."
  193. );
  194. }
  195. }
  196. /**
  197. * @expectedException \LogicException
  198. */
  199. public function testRestrictedCustomAttributesGet()
  200. {
  201. $this->model->getData(\Magento\Framework\Api\CustomAttributesDataInterface::CUSTOM_ATTRIBUTES);
  202. }
  203. public function testSetCustomAttributesAsLiterals()
  204. {
  205. $this->model->expects($this->any())->method('getCustomAttributesCodes')->willReturn([]);
  206. $attributeCode = 'attribute2';
  207. $attributeValue = 'attribute_value';
  208. $attributeMock = $this->getMockBuilder(\Magento\Framework\Api\AttributeValue::class)
  209. ->disableOriginalConstructor()
  210. ->getMock();
  211. $attributeMock->expects($this->never())
  212. ->method('setAttributeCode')
  213. ->with($attributeCode)
  214. ->will($this->returnSelf());
  215. $attributeMock->expects($this->never())
  216. ->method('setValue')
  217. ->with($attributeValue)
  218. ->will($this->returnSelf());
  219. $this->attributeValueFactoryMock->expects($this->never())->method('create')
  220. ->willReturn($attributeMock);
  221. $this->model->setData(
  222. \Magento\Framework\Api\CustomAttributesDataInterface::CUSTOM_ATTRIBUTES,
  223. [$attributeCode => $attributeValue]
  224. );
  225. }
  226. /**
  227. * @param string[] $attributesAsArray
  228. * @param \Magento\Framework\Model\AbstractExtensibleModel $model
  229. * @return \Magento\Framework\Api\AttributeInterface[]
  230. */
  231. protected function addCustomAttributesToModel($attributesAsArray, $model)
  232. {
  233. $addedAttributes = [];
  234. foreach ($attributesAsArray as $attributeCode => $attributeValue) {
  235. $addedAttributes[$attributeCode] = new AttributeValue(
  236. [
  237. AttributeValue::ATTRIBUTE_CODE => $attributeCode,
  238. AttributeValue::VALUE => $attributeValue,
  239. ]
  240. );
  241. }
  242. $model->setData(
  243. array_merge(
  244. $model->getData(),
  245. [\Magento\Framework\Api\CustomAttributesDataInterface::CUSTOM_ATTRIBUTES => $addedAttributes]
  246. )
  247. );
  248. return $addedAttributes;
  249. }
  250. }