Skip to content

Commit 84a16b0

Browse files
committed
fix: Reorganize subscriber test
1 parent 69acebf commit 84a16b0

File tree

1 file changed

+108
-119
lines changed

1 file changed

+108
-119
lines changed

tests/lib/Persistence/Legacy/Content/Mapper/ResolveVirtualFieldSubscriberTest.php

+108-119
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,9 @@
2828

2929
final class ResolveVirtualFieldSubscriberTest extends TestCase
3030
{
31-
private function getContent(): Content
32-
{
33-
$versionInfo = $this->getVersionInfo();
34-
35-
$content = new Content();
36-
$content->versionInfo = $versionInfo;
37-
$content->fields = [];
38-
39-
return $content;
40-
}
41-
42-
private function getVersionInfo(): VersionInfo
43-
{
44-
$versionInfo = new VersionInfo();
45-
$versionInfo->versionNo = 123;
46-
47-
return $versionInfo;
48-
}
49-
5031
public function testResolveVirtualField(): void
5132
{
52-
$converterRegistry = $this->createMock(ConverterRegistry::class);
53-
$converterRegistry->method('getConverter')
54-
->willReturn($this->createMock(Converter::class));
33+
$converterRegistry = $this->getConverterRegistry();
5534

5635
$storageRegistry = $this->createMock(StorageRegistry::class);
5736
$storageRegistry->method('getStorage')
@@ -61,31 +40,25 @@ public function testResolveVirtualField(): void
6140
$contentGateway->expects($this->never())
6241
->method('insertNewField');
6342

64-
$eventDispatcher = $this->getEventDispatcher();
65-
$eventDispatcher->addSubscriber(
66-
new ResolveVirtualFieldSubscriber(
67-
$converterRegistry,
68-
$storageRegistry,
69-
$contentGateway
70-
)
43+
$eventDispatcher = $this->getEventDispatcher(
44+
$converterRegistry,
45+
$storageRegistry,
46+
$contentGateway
7147
);
7248

73-
$content = $this->getContent();
74-
$fieldDefinition = new FieldDefinition([
75-
'id' => 123,
76-
'identifier' => 'example_field',
77-
'fieldType' => 'some_type',
78-
'defaultValue' => new Content\FieldValue(),
79-
]);
80-
81-
$event = new ResolveMissingFieldEvent(
82-
$content,
83-
$fieldDefinition,
84-
'eng-GB'
49+
$event = $eventDispatcher->dispatch(
50+
new ResolveMissingFieldEvent(
51+
$this->getContent(),
52+
new FieldDefinition([
53+
'id' => 123,
54+
'identifier' => 'example_field',
55+
'fieldType' => 'some_type',
56+
'defaultValue' => new Content\FieldValue(),
57+
]),
58+
'eng-GB'
59+
)
8560
);
8661

87-
$event = $eventDispatcher->dispatch($event);
88-
8962
$expected = new Content\Field([
9063
'id' => null,
9164
'fieldDefinitionId' => 123,
@@ -113,9 +86,7 @@ public function testResolveVirtualField(): void
11386

11487
public function testResolveVirtualExternalStorageField(): void
11588
{
116-
$converterRegistry = $this->createMock(ConverterRegistry::class);
117-
$converterRegistry->method('getConverter')
118-
->willReturn($this->createMock(Converter::class));
89+
$converterRegistry = $this->getConverterRegistry();
11990

12091
$storageRegistry = $this->createMock(StorageRegistry::class);
12192
$storageRegistry->method('getStorage')
@@ -158,31 +129,25 @@ public function getIndexData(VersionInfo $versionInfo, Field $field, array $cont
158129
$contentGateway->expects($this->never())
159130
->method('insertNewField');
160131

161-
$eventDispatcher = $this->getEventDispatcher();
162-
$eventDispatcher->addSubscriber(
163-
new ResolveVirtualFieldSubscriber(
164-
$converterRegistry,
165-
$storageRegistry,
166-
$contentGateway
167-
)
132+
$eventDispatcher = $this->getEventDispatcher(
133+
$converterRegistry,
134+
$storageRegistry,
135+
$contentGateway
168136
);
169137

170-
$content = $this->getContent();
171-
$fieldDefinition = new FieldDefinition([
172-
'id' => 678,
173-
'identifier' => 'example_external_field',
174-
'fieldType' => 'external_type_virtual',
175-
'defaultValue' => new Content\FieldValue(),
176-
]);
177-
178-
$event = new ResolveMissingFieldEvent(
179-
$content,
180-
$fieldDefinition,
181-
'eng-GB'
138+
$event = $eventDispatcher->dispatch(
139+
new ResolveMissingFieldEvent(
140+
$this->getContent(),
141+
new FieldDefinition([
142+
'id' => 678,
143+
'identifier' => 'example_external_field',
144+
'fieldType' => 'external_type_virtual',
145+
'defaultValue' => new Content\FieldValue(),
146+
]),
147+
'eng-GB'
148+
)
182149
);
183150

184-
$event = $eventDispatcher->dispatch($event);
185-
186151
$expected = new Content\Field([
187152
'id' => null,
188153
'fieldDefinitionId' => 678,
@@ -210,9 +175,7 @@ public function getIndexData(VersionInfo $versionInfo, Field $field, array $cont
210175

211176
public function testPersistEmptyExternalStorageField(): void
212177
{
213-
$converterRegistry = $this->createMock(ConverterRegistry::class);
214-
$converterRegistry->method('getConverter')
215-
->willReturn($this->createMock(Converter::class));
178+
$converterRegistry = $this->getConverterRegistry();
216179

217180
$storage = $this->createMock(FieldStorage::class);
218181
$storage->expects($this->never())
@@ -235,31 +198,25 @@ public function testPersistEmptyExternalStorageField(): void
235198
->method('insertNewField')
236199
->willReturn(567);
237200

238-
$eventDispatcher = $this->getEventDispatcher();
239-
$eventDispatcher->addSubscriber(
240-
new ResolveVirtualFieldSubscriber(
241-
$converterRegistry,
242-
$storageRegistry,
243-
$contentGateway,
244-
)
201+
$eventDispatcher = $this->getEventDispatcher(
202+
$converterRegistry,
203+
$storageRegistry,
204+
$contentGateway
245205
);
246206

247-
$content = $this->getContent();
248-
$fieldDefinition = new FieldDefinition([
249-
'id' => 123,
250-
'identifier' => 'example_field',
251-
'fieldType' => 'external_type',
252-
'defaultValue' => new Content\FieldValue(),
253-
]);
254-
255-
$event = new ResolveMissingFieldEvent(
256-
$content,
257-
$fieldDefinition,
258-
'eng-GB'
207+
$event = $eventDispatcher->dispatch(
208+
new ResolveMissingFieldEvent(
209+
$this->getContent(),
210+
new FieldDefinition([
211+
'id' => 123,
212+
'identifier' => 'example_field',
213+
'fieldType' => 'external_type',
214+
'defaultValue' => new Content\FieldValue(),
215+
]),
216+
'eng-GB'
217+
)
259218
);
260219

261-
$event = $eventDispatcher->dispatch($event);
262-
263220
$expected = new Content\Field([
264221
'id' => 567,
265222
'fieldDefinitionId' => 123,
@@ -291,9 +248,7 @@ public function testPersistEmptyExternalStorageField(): void
291248

292249
public function testPersistExternalStorageField(): void
293250
{
294-
$converterRegistry = $this->createMock(ConverterRegistry::class);
295-
$converterRegistry->method('getConverter')
296-
->willReturn($this->createMock(Converter::class));
251+
$converterRegistry = $this->getConverterRegistry();
297252

298253
$storage = $this->createMock(FieldStorage::class);
299254
$storage->expects($this->once())
@@ -314,33 +269,27 @@ public function testPersistExternalStorageField(): void
314269
->method('insertNewField')
315270
->willReturn(456);
316271

317-
$eventDispatcher = $this->getEventDispatcher();
318-
$eventDispatcher->addSubscriber(
319-
new ResolveVirtualFieldSubscriber(
320-
$converterRegistry,
321-
$storageRegistry,
322-
$contentGateway,
323-
)
272+
$eventDispatcher = $this->getEventDispatcher(
273+
$converterRegistry,
274+
$storageRegistry,
275+
$contentGateway
324276
);
325277

326-
$content = $this->getContent();
327-
$fieldDefinition = new FieldDefinition([
328-
'id' => 123,
329-
'identifier' => 'example_field',
330-
'fieldType' => 'external_type',
331-
'defaultValue' => new Content\FieldValue([
332-
'data' => ['some_data' => 'to_be_stored'],
333-
]),
334-
]);
335-
336-
$event = new ResolveMissingFieldEvent(
337-
$content,
338-
$fieldDefinition,
339-
'eng-GB'
278+
$event = $eventDispatcher->dispatch(
279+
new ResolveMissingFieldEvent(
280+
$this->getContent(),
281+
new FieldDefinition([
282+
'id' => 123,
283+
'identifier' => 'example_field',
284+
'fieldType' => 'external_type',
285+
'defaultValue' => new Content\FieldValue([
286+
'data' => ['some_data' => 'to_be_stored'],
287+
]),
288+
]),
289+
'eng-GB'
290+
)
340291
);
341292

342-
$event = $eventDispatcher->dispatch($event);
343-
344293
$expected = new Content\Field([
345294
'id' => 456,
346295
'fieldDefinitionId' => 123,
@@ -373,11 +322,51 @@ public function testPersistExternalStorageField(): void
373322
);
374323
}
375324

376-
private function getEventDispatcher(): TraceableEventDispatcher
325+
private function getContent(): Content
377326
{
327+
$versionInfo = $this->getVersionInfo();
328+
329+
$content = new Content();
330+
$content->versionInfo = $versionInfo;
331+
$content->fields = [];
332+
333+
return $content;
334+
}
335+
336+
private function getVersionInfo(): VersionInfo
337+
{
338+
$versionInfo = new VersionInfo();
339+
$versionInfo->versionNo = 123;
340+
341+
return $versionInfo;
342+
}
343+
344+
private function getEventDispatcher(
345+
ConverterRegistry $converterRegistry,
346+
StorageRegistry $storageRegistry,
347+
ContentGateway $contentGateway
348+
): TraceableEventDispatcher {
349+
$eventDispatcher = new EventDispatcher();
350+
$eventDispatcher->addSubscriber(
351+
new ResolveVirtualFieldSubscriber(
352+
$converterRegistry,
353+
$storageRegistry,
354+
$contentGateway,
355+
)
356+
);
357+
378358
return new TraceableEventDispatcher(
379-
new EventDispatcher(),
359+
$eventDispatcher,
380360
new Stopwatch()
381361
);
382362
}
363+
364+
private function getConverterRegistry(): ConverterRegistry
365+
{
366+
$converterRegistry = $this->createMock(ConverterRegistry::class);
367+
$converterRegistry->method('getConverter')
368+
->willReturn($this->createMock(Converter::class));
369+
370+
return $converterRegistry;
371+
}
383372
}

0 commit comments

Comments
 (0)