- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitOfWorkTest.php
More file actions
172 lines (139 loc) · 5.31 KB
/
UnitOfWorkTest.php
File metadata and controls
172 lines (139 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
declare(strict_types=1);
namespace Larium\ODM;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Larium\ODM\Document\User;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use ReflectionProperty;
class UnitOfWorkTest extends TestCase
{
use FirestoreHelperTrait;
private Configuration $config;
protected function setUp(): void
{
$loader = require __DIR__ . '/../vendor/autoload.php';
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
$this->config = new Configuration();
$this->config->setDocumentsPaths([
__DIR__ . '/Document',
]);
$this->config->setClientFactory($this->createMockClientFactory());
}
public function testFlushCallsUpdateWhenManagedEntityDiffersFromOriginal(): void
{
$collection = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->onlyMethods(['update'])
->getMock();
$collection->expects($this->once())->method('update')->with($this->callback(function (Document $d): bool {
return $d->getId() === 'u1'
&& ($d->getData()['first_name'] ?? null) === 'Changed';
}));
$client = $this->createMock(Client::class);
$client->method('getCollection')->with('users')->willReturn($collection);
$config = new Configuration();
$config->setDocumentsPaths([__DIR__ . '/Document']);
$config->setClientFactory(new class($client) implements ClientFactory {
public function __construct(private Client $client)
{
}
public function createClient(): Client
{
return $this->client;
}
});
$dm = new DocumentManager($config);
$uow = $dm->getUnitOfWork();
$user = $this->userWithId('u1', 'John', 'Doe', 1970);
$doc = new Document('u1', [
'first_name' => 'John',
'last_name' => 'Doe',
'born' => 1970,
]);
$uow->registerClean('u1', $user);
$uow->registerOriginal($doc, $user);
$user->changeName('Changed');
$dm->flush();
}
public function testFlushDoesNotUpdateWhenEntityMatchesOriginal(): void
{
$collection = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->onlyMethods(['update'])
->getMock();
$collection->expects($this->never())->method('update');
$client = $this->createMock(Client::class);
$client->method('getCollection')->with('users')->willReturn($collection);
$config = new Configuration();
$config->setDocumentsPaths([__DIR__ . '/Document']);
$config->setClientFactory(new class($client) implements ClientFactory {
public function __construct(private Client $client)
{
}
public function createClient(): Client
{
return $this->client;
}
});
$dm = new DocumentManager($config);
$uow = $dm->getUnitOfWork();
$user = $this->userWithId('u2', 'Same', 'Value', 1970);
$doc = new Document('u2', [
'first_name' => 'Same',
'last_name' => 'Value',
'born' => 1970,
]);
$uow->registerClean('u2', $user);
$uow->registerOriginal($doc, $user);
$dm->flush();
}
public function testHasChangeSetDetectsChangeWhenFieldNamesDifferFromPropertyNames(): void
{
$dm = new DocumentManager($this->config);
$uow = $dm->getUnitOfWork();
$user = $this->userWithId('id-1', 'Jane', 'Doe', 1970);
$doc = new Document('id-1', [
'first_name' => 'John',
'last_name' => 'Doe',
'born' => 1970,
]);
$invoke = $this->hasChangeSetInvoker($uow);
$this->assertTrue($invoke($doc, $user), 'first_name differs from snapshot');
$user2 = $this->userWithId('id-2', 'Same', 'Value', 1970);
$doc2 = new Document('id-2', [
'first_name' => 'Same',
'last_name' => 'Value',
'born' => 1970,
]);
$this->assertFalse($invoke($doc2, $user2), 'snapshot matches entity');
}
public function testHasChangeSetTreatsMissingFieldAsNull(): void
{
$dm = new DocumentManager($this->config);
$uow = $dm->getUnitOfWork();
$user = $this->userWithId('id-3', 'Only', 'First', 2000);
$doc = new Document('id-3', [
'last_name' => 'First',
]);
$invoke = $this->hasChangeSetInvoker($uow);
$this->assertTrue($invoke($doc, $user), 'first_name missing in snapshot but set on entity');
}
/**
* @return callable(Document, object): bool
*/
private function hasChangeSetInvoker(UnitOfWork $uow): callable
{
$m = new ReflectionMethod(UnitOfWork::class, 'hasChangeSet');
$m->setAccessible(true);
return static fn (Document $doc, object $object): bool => (bool) $m->invoke($uow, $doc, $object);
}
private function userWithId(string $id, string $first, string $last, int $born): User
{
$user = new User($first, $last, $born);
$r = new ReflectionProperty(User::class, 'id');
$r->setAccessible(true);
$r->setValue($user, $id);
return $user;
}
}