Skip to content

Commit f7fd472

Browse files
committed
Merge branch 'ACP2E-3492' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-12-05-2024
2 parents 2329a14 + 812f9a3 commit f7fd472

File tree

2 files changed

+188
-3
lines changed

2 files changed

+188
-3
lines changed

app/code/Magento/CustomerGraphQl/Model/Resolver/Cache/Customer/ModelHydrator.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -10,12 +10,13 @@
1010
use Magento\Customer\Model\Data\Customer;
1111
use Magento\Customer\Model\Data\CustomerFactory;
1212
use Magento\Framework\EntityManager\HydratorPool;
13+
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
1314
use Magento\GraphQlResolverCache\Model\Resolver\Result\HydratorInterface;
1415

1516
/**
1617
* Customer resolver data hydrator to rehydrate propagated model.
1718
*/
18-
class ModelHydrator implements HydratorInterface
19+
class ModelHydrator implements HydratorInterface, ResetAfterRequestInterface
1920
{
2021
/**
2122
* @var CustomerFactory
@@ -59,4 +60,14 @@ public function hydrate(array &$resolverData): void
5960
$resolverData['model'] = $this->customerModels[$resolverData['model_id']];
6061
}
6162
}
63+
64+
/**
65+
* Reset customerModels
66+
*
67+
* @return void
68+
*/
69+
public function _resetState(): void
70+
{
71+
$this->customerModels = [];
72+
}
6273
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe.
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Test\Unit\Model\Resolver\Cache\Customer;
9+
10+
use Magento\Customer\Model\Data\Customer;
11+
use Magento\Customer\Model\Data\CustomerFactory;
12+
use Magento\CustomerGraphQl\Model\Resolver\Cache\Customer\ModelHydrator;
13+
use Magento\Framework\EntityManager\HydratorInterface;
14+
use Magento\Framework\EntityManager\HydratorPool;
15+
use PHPUnit\Framework\MockObject\Exception;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class ModelHydratorTest extends TestCase
20+
{
21+
/**
22+
* @var CustomerFactory|MockObject
23+
*/
24+
private $customerFactory;
25+
26+
/**
27+
* @var HydratorPool|MockObject
28+
*/
29+
private $hydratorPool;
30+
31+
/**
32+
* @var HydratorInterface|MockObject
33+
*/
34+
private $hydrator;
35+
36+
/**
37+
* @var ModelHydrator
38+
*/
39+
private $modelHydrator;
40+
41+
protected function setUp(): void
42+
{
43+
$this->customerFactory = $this->createMock(CustomerFactory::class);
44+
$this->hydratorPool = $this->createMock(HydratorPool::class);
45+
$this->hydrator = $this->createMock(HydratorInterface::class);
46+
47+
$this->modelHydrator = new ModelHydrator(
48+
$this->customerFactory,
49+
$this->hydratorPool
50+
);
51+
}
52+
53+
/**
54+
* Test hydrate method with existing model
55+
*
56+
* @return void
57+
* @throws Exception
58+
*/
59+
60+
public function testHydrateWithExistingModel()
61+
{
62+
$customer = $this->createMock(Customer::class);
63+
$resolverData = [
64+
'model_id' => 1,
65+
'model_entity_type' => 'customer',
66+
'model_data' => ['id' => 1]
67+
];
68+
69+
$this->customerFactory
70+
->method('create')
71+
->willReturn($customer);
72+
$this->hydrator
73+
->method('hydrate')
74+
->with($customer, $resolverData['model_data'])
75+
->willReturnSelf();
76+
$this->hydratorPool
77+
->method('getHydrator')
78+
->with('customer')
79+
->willReturn($this->hydrator);
80+
81+
$this->modelHydrator->hydrate($resolverData);
82+
$this->modelHydrator->hydrate($resolverData);
83+
$this->assertSame($customer, $resolverData['model']);
84+
}
85+
86+
/**
87+
* Test hydrate method with new model
88+
*
89+
* @return void
90+
* @throws Exception
91+
*/
92+
93+
public function testHydrateWithNewModel()
94+
{
95+
$customer = $this->createMock(Customer::class);
96+
$resolverData = [
97+
'model_id' => 1,
98+
'model_entity_type' => 'customer',
99+
'model_data' => ['id' => 1]
100+
];
101+
102+
$this->customerFactory
103+
->method('create')
104+
->willReturn($customer);
105+
$this->hydratorPool
106+
->method('getHydrator')
107+
->willReturn($this->hydrator);
108+
$this->hydrator->expects($this->once())
109+
->method('hydrate')
110+
->with($customer, $resolverData['model_data']);
111+
112+
$this->modelHydrator->hydrate($resolverData);
113+
$this->assertSame($customer, $resolverData['model']);
114+
}
115+
116+
/**
117+
* Test that resetState method resets the state of the modelHydrator
118+
*
119+
* @return void
120+
* @throws Exception
121+
*
122+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
123+
*/
124+
public function testResetState()
125+
{
126+
$customer1 = $this->createMock(Customer::class);
127+
$customer2 = $this->createMock(Customer::class);
128+
129+
$resolverData1 = [
130+
'model_id' => 1,
131+
'model_entity_type' => 'customer',
132+
'model_data' => ['id' => 1]
133+
];
134+
135+
$resolverData2 = [
136+
'model_id' => 2,
137+
'model_entity_type' => 'customer',
138+
'model_data' => ['id' => 2]
139+
];
140+
141+
$this->customerFactory
142+
->method('create')
143+
->willReturnOnConsecutiveCalls($customer1, $customer2);
144+
$this->hydratorPool
145+
->method('getHydrator')
146+
->willReturn($this->hydrator);
147+
148+
$matcher = $this->exactly(2);
149+
$expected1 = $resolverData1['model_data'];
150+
$expected2 = $resolverData2['model_data'];
151+
152+
$this->hydrator
153+
->expects($matcher)
154+
->method('hydrate')
155+
->willReturnCallback(function ($model, $data) use ($matcher, $expected1, $expected2) {
156+
match ($matcher->numberOfInvocations()) {
157+
1 => $this->assertEquals($expected1, $data),
158+
2 => $this->assertEquals($expected2, $data),
159+
};
160+
});
161+
162+
$this->modelHydrator->hydrate($resolverData1);
163+
164+
$this->assertArrayHasKey('model', $resolverData1);
165+
$this->assertEquals(1, $resolverData1['model_id']);
166+
167+
$this->modelHydrator->_resetState();
168+
169+
$this->modelHydrator->hydrate($resolverData2);
170+
171+
$this->assertArrayHasKey('model', $resolverData2);
172+
$this->assertEquals(2, $resolverData2['model_id']);
173+
}
174+
}

0 commit comments

Comments
 (0)