Skip to content

Commit 63e1804

Browse files
committed
Merge remote-tracking branch 'magento-chaika/MC-19013' into MPI-PR
# Conflicts: # dev/tests/integration/testsuite/Magento/Sales/_files/quotes.php
2 parents c9c9e51 + b42dfb0 commit 63e1804

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\Product;
9+
10+
use Magento\Framework\EntityManager\HydratorInterface;
11+
12+
/**
13+
* Class is used to extract data and populate entity with data
14+
*/
15+
class Hydrator implements HydratorInterface
16+
{
17+
/**
18+
* @inheritdoc
19+
*/
20+
public function extract($entity)
21+
{
22+
return $entity->getData();
23+
}
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
public function hydrate($entity, array $data)
29+
{
30+
$lockedAttributes = $entity->getLockedAttributes();
31+
$entity->unlockAttributes();
32+
$entity->setData(array_merge($entity->getData(), $data));
33+
foreach ($lockedAttributes as $attribute) {
34+
$entity->lockAttribute($attribute);
35+
}
36+
37+
return $entity;
38+
}
39+
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@
867867
<argument name="hydrators" xsi:type="array">
868868
<item name="Magento\Catalog\Api\Data\CategoryInterface" xsi:type="string">Magento\Framework\EntityManager\AbstractModelHydrator</item>
869869
<item name="Magento\Catalog\Api\Data\CategoryTreeInterface" xsi:type="string">Magento\Framework\EntityManager\AbstractModelHydrator</item>
870-
<item name="Magento\Catalog\Api\Data\ProductInterface" xsi:type="string">Magento\Framework\EntityManager\AbstractModelHydrator</item>
870+
<item name="Magento\Catalog\Api\Data\ProductInterface" xsi:type="string">Magento\Catalog\Model\Product\Hydrator</item>
871871
</argument>
872872
</arguments>
873873
</type>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model;
7+
8+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Framework\EntityManager\HydratorInterface;
10+
use Magento\Framework\EntityManager\HydratorPool;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
/**
14+
* Test Product Hydrator
15+
*/
16+
class ProductHydratorTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var Bootstrap
20+
*/
21+
private $objectManager;
22+
23+
/**
24+
* @var HydratorPool
25+
*/
26+
private $hydratorPool;
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
protected function setUp()
32+
{
33+
$this->objectManager = Bootstrap::getObjectManager();
34+
$this->hydratorPool = $this->objectManager->create(HydratorPool::class);
35+
}
36+
37+
/**
38+
* Test that Hydrator correctly populates entity with data
39+
*/
40+
public function testProductHydrator()
41+
{
42+
$addAttributes = [
43+
'sku' => 'product_updated',
44+
'name' => 'Product (Updated)',
45+
'type_id' => 'simple',
46+
'status' => 1,
47+
];
48+
49+
/** @var Product $product */
50+
$product = $this->objectManager->create(Product::class);
51+
$product->setId(42)
52+
->setSku('product')
53+
->setName('Product')
54+
->setPrice(10)
55+
->setQty(123);
56+
$product->lockAttribute('sku');
57+
$product->lockAttribute('type_id');
58+
$product->lockAttribute('price');
59+
60+
/** @var HydratorInterface $hydrator */
61+
$hydrator = $this->hydratorPool->getHydrator(ProductInterface::class);
62+
$hydrator->hydrate($product, $addAttributes);
63+
64+
$expected = [
65+
'entity_id' => 42,
66+
'sku' => 'product_updated',
67+
'name' => 'Product (Updated)',
68+
'type_id' => 'simple',
69+
'status' => 1,
70+
'price' => 10,
71+
'qty' => 123,
72+
];
73+
$this->assertEquals($expected, $product->getData());
74+
}
75+
}

0 commit comments

Comments
 (0)