Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractExtensibleModelTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class AbstractExtensibleModelTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $model;
20 
24  protected $contextMock;
25 
29  protected $registryMock;
30 
34  protected $resourceMock;
35 
40 
43 
46 
51 
55  protected $customAttribute;
56 
57  protected function setUp()
58  {
59  $this->actionValidatorMock = $this->createMock(\Magento\Framework\Model\ActionValidator\RemoveAction::class);
60  $this->contextMock = new \Magento\Framework\Model\Context(
61  $this->createMock(\Psr\Log\LoggerInterface::class),
62  $this->createMock(\Magento\Framework\Event\ManagerInterface::class),
63  $this->createMock(\Magento\Framework\App\CacheInterface::class),
64  $this->createMock(\Magento\Framework\App\State::class),
65  $this->actionValidatorMock
66  );
67  $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
68  $this->resourceMock = $this->createPartialMock(\Magento\Framework\Model\ResourceModel\Db\AbstractDb::class, [
69  '_construct',
70  'getConnection',
71  '__wakeup',
72  'commit',
73  'delete',
74  'getIdFieldName',
75  'rollBack'
76  ]);
77  $this->resourceCollectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
78  ->disableOriginalConstructor()
79  ->getMockForAbstractClass();
80  $this->metadataServiceMock = $this->getMockBuilder(\Magento\Framework\Api\MetadataServiceInterface::class)
81  ->getMock();
82  $this->metadataServiceMock
83  ->expects($this->any())
84  ->method('getCustomAttributesMetadata')
85  ->willReturn(
86  [
87  new \Magento\Framework\DataObject(['attribute_code' => 'attribute1']),
88  new \Magento\Framework\DataObject(['attribute_code' => 'attribute2']),
89  new \Magento\Framework\DataObject(['attribute_code' => 'attribute3']),
90  ]
91  );
92  $extensionAttributesFactory = $this->getMockBuilder(\Magento\Framework\Api\ExtensionAttributesFactory::class)
93  ->setMethods(['extractExtensionAttributes'])
94  ->disableOriginalConstructor()
95  ->getMock();
96  $extensionAttributesFactory->expects($this->any())
97  ->method('extractExtensionAttributes')
98  ->willReturnArgument(1);
99  $this->attributeValueFactoryMock = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class)
100  ->disableOriginalConstructor()
101  ->getMock();
102  $this->model = $this->getMockForAbstractClass(
103  \Magento\Framework\Model\AbstractExtensibleModel::class,
104  [
105  $this->contextMock,
106  $this->registryMock,
108  $this->attributeValueFactoryMock,
109  $this->resourceMock,
110  $this->resourceCollectionMock
111  ],
112  '',
113  true,
114  true,
115  true,
116  ['getCustomAttributesCodes']
117  );
118  $this->customAttribute = new AttributeValue();
119  }
120 
125  {
126  $this->model->expects($this->any())->method('getCustomAttributesCodes')->willReturn([]);
127  $this->assertEquals(
128  [],
129  $this->model->getCustomAttributes(),
130  "Empty array is expected as a result of getCustomAttributes() when custom attributes are not set."
131  );
132  $this->assertEquals(
133  null,
134  $this->model->getCustomAttribute('not_existing_custom_attribute'),
135  "Null is expected as a result of getCustomAttribute(\$code) when custom attribute is not set."
136  );
137  $attributesAsArray = ['attribute1' => true, 'attribute2' => 'Attribute Value', 'attribute3' => 333];
138  $this->addCustomAttributesToModel($attributesAsArray, $this->model);
139  $this->assertEquals(
140  [],
141  $this->model->getCustomAttributes(),
142  'Custom attributes retrieved from the model using getCustomAttributes() are invalid.'
143  );
144  }
145 
147  {
148  $customAttributeCode = 'attribute_code';
149  $customAttributeValue = 'attribute_value';
150  $this->model->expects($this->any())->method('getCustomAttributesCodes')->willReturn([$customAttributeCode]);
151 
152  $this->assertEquals(
153  [],
154  $this->model->getCustomAttributes(),
155  "Empty array is expected as a result of getCustomAttributes() when custom attributes are not set."
156  );
157  $this->attributeValueFactoryMock->expects($this->once())
158  ->method('create')
159  ->willReturn($this->customAttribute);
160  $this->customAttribute->setAttributeCode($customAttributeCode)->setValue($customAttributeValue);
161  $this->model->setData($customAttributeCode, $customAttributeValue);
162  $this->assertEquals(
163  [$this->customAttribute],
164  $this->model->getCustomAttributes(),
165  "One custom attribute expected"
166  );
167  $this->assertNotNull($this->model->getCustomAttribute($customAttributeCode), 'customer attribute expected');
168  $this->assertEquals(
169  $customAttributeValue,
170  $this->model->getCustomAttribute($customAttributeCode)->getValue(),
171  "Custom attribute value is incorrect"
172  );
173  //unset the data
174  $this->model->unsetData($customAttributeCode);
175  $this->assertEquals(
176  [],
177  $this->model->getCustomAttributes(),
178  "Empty array is expected as a result of getCustomAttributes() when custom attributes are not set."
179  );
180  }
181 
186  {
187  $this->model->expects($this->any())->method('getCustomAttributesCodes')->willReturn([]);
188  $attributesAsArray = [
189  'attribute1' => true,
190  'attribute2' => 'Attribute Value',
191  'attribute3' => 333,
192  'invalid' => true,
193  ];
194  $modelData = ['key1' => 'value1', 'key2' => 222];
195  foreach ($modelData as $key => $value) {
196  $this->model->setData($key, $value);
197  }
198  $this->addCustomAttributesToModel($attributesAsArray, $this->model);
199  $this->assertEquals(
200  $modelData,
201  $this->model->getData(),
202  'All model data should be represented as a flat array, including custom attributes.'
203  );
204  foreach ($modelData as $field => $value) {
205  $this->assertEquals(
206  $value,
207  $this->model->getData($field),
208  "Model data item '{$field}' was retrieved incorrectly."
209  );
210  }
211  }
212 
217  {
218  $this->model->getData(\Magento\Framework\Api\CustomAttributesDataInterface::CUSTOM_ATTRIBUTES);
219  }
220 
222  {
223  $this->model->expects($this->any())->method('getCustomAttributesCodes')->willReturn([]);
224  $attributeCode = 'attribute2';
225  $attributeValue = 'attribute_value';
226  $attributeMock = $this->getMockBuilder(\Magento\Framework\Api\AttributeValue::class)
227  ->disableOriginalConstructor()
228  ->getMock();
229  $attributeMock->expects($this->never())
230  ->method('setAttributeCode')
231  ->with($attributeCode)
232  ->will($this->returnSelf());
233  $attributeMock->expects($this->never())
234  ->method('setValue')
235  ->with($attributeValue)
236  ->will($this->returnSelf());
237  $this->attributeValueFactoryMock->expects($this->never())->method('create')
238  ->willReturn($attributeMock);
239  $this->model->setData(
240  \Magento\Framework\Api\CustomAttributesDataInterface::CUSTOM_ATTRIBUTES,
241  [$attributeCode => $attributeValue]
242  );
243  }
244 
250  protected function addCustomAttributesToModel($attributesAsArray, $model)
251  {
252  $addedAttributes = [];
253  foreach ($attributesAsArray as $attributeCode => $attributeValue) {
254  $addedAttributes[$attributeCode] = new AttributeValue(
255  [
257  AttributeValue::VALUE => $attributeValue,
258  ]
259  );
260  }
261  $model->setData(
262  array_merge(
263  $model->getData(),
265  )
266  );
267  return $addedAttributes;
268  }
269 }
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
$extensionAttributesFactory