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
67 $this->registryMock = $this->createMock(\
Magento\Framework\Registry::class);
68 $this->resourceMock = $this->createPartialMock(\
Magento\Framework\Model\
ResourceModel\Db\AbstractDb::class, [
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)
82 $this->metadataServiceMock
83 ->expects($this->any())
84 ->method(
'getCustomAttributesMetadata')
93 ->setMethods([
'extractExtensionAttributes'])
94 ->disableOriginalConstructor()
97 ->method(
'extractExtensionAttributes')
98 ->willReturnArgument(1);
99 $this->attributeValueFactoryMock = $this->getMockBuilder(\
Magento\Framework\Api\AttributeValueFactory::class)
100 ->disableOriginalConstructor()
102 $this->model = $this->getMockForAbstractClass(
103 \
Magento\Framework\Model\AbstractExtensibleModel::class,
108 $this->attributeValueFactoryMock,
110 $this->resourceCollectionMock
116 [
'getCustomAttributesCodes']
126 $this->model->expects($this->any())->method(
'getCustomAttributesCodes')->willReturn([]);
129 $this->model->getCustomAttributes(),
130 "Empty array is expected as a result of getCustomAttributes() when custom attributes are not set." 134 $this->model->getCustomAttribute(
'not_existing_custom_attribute'),
135 "Null is expected as a result of getCustomAttribute(\$code) when custom attribute is not set." 137 $attributesAsArray = [
'attribute1' =>
true,
'attribute2' =>
'Attribute Value',
'attribute3' => 333];
141 $this->model->getCustomAttributes(),
142 'Custom attributes retrieved from the model using getCustomAttributes() are invalid.' 148 $customAttributeCode =
'attribute_code';
149 $customAttributeValue =
'attribute_value';
150 $this->model->expects($this->any())->method(
'getCustomAttributesCodes')->willReturn([$customAttributeCode]);
154 $this->model->getCustomAttributes(),
155 "Empty array is expected as a result of getCustomAttributes() when custom attributes are not set." 157 $this->attributeValueFactoryMock->expects($this->once())
159 ->willReturn($this->customAttribute);
160 $this->customAttribute->setAttributeCode($customAttributeCode)->setValue($customAttributeValue);
161 $this->model->setData($customAttributeCode, $customAttributeValue);
163 [$this->customAttribute],
164 $this->model->getCustomAttributes(),
165 "One custom attribute expected" 167 $this->assertNotNull($this->model->getCustomAttribute($customAttributeCode),
'customer attribute expected');
169 $customAttributeValue,
170 $this->model->getCustomAttribute($customAttributeCode)->getValue(),
171 "Custom attribute value is incorrect" 174 $this->model->unsetData($customAttributeCode);
177 $this->model->getCustomAttributes(),
178 "Empty array is expected as a result of getCustomAttributes() when custom attributes are not set." 187 $this->model->expects($this->any())->method(
'getCustomAttributesCodes')->willReturn([]);
188 $attributesAsArray = [
189 'attribute1' =>
true,
190 'attribute2' =>
'Attribute Value',
194 $modelData = [
'key1' =>
'value1',
'key2' => 222];
195 foreach ($modelData as $key =>
$value) {
196 $this->model->setData($key,
$value);
201 $this->model->getData(),
202 'All model data should be represented as a flat array, including custom attributes.' 204 foreach ($modelData as $field =>
$value) {
207 $this->model->getData($field),
208 "Model data item '{$field}' was retrieved incorrectly." 218 $this->model->getData(\
Magento\Framework\Api\CustomAttributesDataInterface::CUSTOM_ATTRIBUTES);
223 $this->model->expects($this->any())->method(
'getCustomAttributesCodes')->willReturn([]);
225 $attributeValue =
'attribute_value';
226 $attributeMock = $this->getMockBuilder(\
Magento\Framework\Api\AttributeValue::class)
227 ->disableOriginalConstructor()
229 $attributeMock->expects($this->never())
230 ->method(
'setAttributeCode')
232 ->will($this->returnSelf());
233 $attributeMock->expects($this->never())
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,
252 $addedAttributes = [];
253 foreach ($attributesAsArray as
$attributeCode => $attributeValue) {
267 return $addedAttributes;
testCustomAttributesWithEmptyCustomAttributes()
testGetDataWithCustomAttributes()
addCustomAttributesToModel($attributesAsArray, $model)
$attributeValueFactoryMock
testRestrictedCustomAttributesGet()
testCustomAttributesWithNonEmptyCustomAttributes()
$extensionAttributesFactory
testSetCustomAttributesAsLiterals()