Skip to content

Commit a441af8

Browse files
committed
ACP2E-3492: added test coverage
1 parent f5c61f9 commit a441af8

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
public function testResetState()
123+
{
124+
$customer1 = $this->createMock(Customer::class);
125+
$customer2 = $this->createMock(Customer::class);
126+
127+
$resolverData1 = [
128+
'model_id' => 1,
129+
'model_entity_type' => 'customer',
130+
'model_data' => ['id' => 1]
131+
];
132+
133+
$resolverData2 = [
134+
'model_id' => 2,
135+
'model_entity_type' => 'customer',
136+
'model_data' => ['id' => 2]
137+
];
138+
139+
$this->customerFactory
140+
->method('create')
141+
->willReturnOnConsecutiveCalls($customer1, $customer2);
142+
$this->hydratorPool
143+
->method('getHydrator')
144+
->willReturn($this->hydrator);
145+
146+
$matcher = $this->exactly(2);
147+
$expected1 = $resolverData1['model_data'];
148+
$expected2 = $resolverData2['model_data'];
149+
150+
$this->hydrator
151+
->expects($matcher)
152+
->method('hydrate')
153+
->willReturnCallback(function ($model, $data) use ($matcher, $expected1, $expected2) {
154+
match ($matcher->numberOfInvocations()) {
155+
1 => $this->assertEquals($expected1, $data),
156+
2 => $this->assertEquals($expected2, $data),
157+
};
158+
});
159+
160+
$this->modelHydrator->hydrate($resolverData1);
161+
162+
$this->assertArrayHasKey('model', $resolverData1);
163+
$this->assertEquals(1, $resolverData1['model_id']);
164+
165+
$this->modelHydrator->_resetState();
166+
167+
$this->modelHydrator->hydrate($resolverData2);
168+
169+
$this->assertArrayHasKey('model', $resolverData2);
170+
$this->assertEquals(2, $resolverData2['model_id']);
171+
}
172+
}

0 commit comments

Comments
 (0)