Skip to content

Commit

Permalink
fix: Reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Nattfarinn committed Apr 23, 2024
1 parent 0f7966d commit 69acebf
Showing 1 changed file with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ public function testResolveVirtualField(): void
$contentGateway->expects($this->never())
->method('insertNewField');

$eventDispatcher = new TraceableEventDispatcher(
new EventDispatcher(),
new Stopwatch()
);

$eventDispatcher = $this->getEventDispatcher();
$eventDispatcher->addSubscriber(
new ResolveVirtualFieldSubscriber(
$converterRegistry,
Expand Down Expand Up @@ -123,6 +119,7 @@ public function testResolveVirtualExternalStorageField(): void

$storageRegistry = $this->createMock(StorageRegistry::class);
$storageRegistry->method('getStorage')
// Multiple interface mocks are deprecated in PHPUnit 9+
->willReturn(new class() implements FieldStorage, DefaultDataFieldStorage {
public function getDefaultFieldData(VersionInfo $versionInfo, Field $field): void
{
Expand All @@ -133,42 +130,47 @@ public function getDefaultFieldData(VersionInfo $versionInfo, Field $field): voi

public function storeFieldData(VersionInfo $versionInfo, Field $field, array $context): void
{
// Mock
}

public function getFieldData(VersionInfo $versionInfo, Field $field, array $context): void
{
// Mock
}

public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds, array $context): void
{
// Mock
}

public function hasFieldData(): void
{
// Mock
}

public function getIndexData(VersionInfo $versionInfo, Field $field, array $context): void
{
// Mock
}
});

$eventDispatcher = new TraceableEventDispatcher(
new EventDispatcher(),
new Stopwatch()
);
$contentGateway = $this->createMock(ContentGateway::class);
$contentGateway->expects($this->never())
->method('insertNewField');

$eventDispatcher = $this->getEventDispatcher();
$eventDispatcher->addSubscriber(
new ResolveVirtualFieldSubscriber(
$converterRegistry,
$storageRegistry,
$this->createMock(ContentGateway::class)
$contentGateway
)
);

$content = $this->getContent();
$fieldDefinition = new FieldDefinition([
'id' => 123,
'identifier' => 'example_field',
'id' => 678,
'identifier' => 'example_external_field',
'fieldType' => 'external_type_virtual',
'defaultValue' => new Content\FieldValue(),
]);
Expand All @@ -183,7 +185,7 @@ public function getIndexData(VersionInfo $versionInfo, Field $field, array $cont

$expected = new Content\Field([
'id' => null,
'fieldDefinitionId' => 123,
'fieldDefinitionId' => 678,
'type' => 'external_type_virtual',
'value' => new Content\FieldValue([
'externalData' => [
Expand Down Expand Up @@ -233,11 +235,7 @@ public function testPersistEmptyExternalStorageField(): void
->method('insertNewField')
->willReturn(567);

$eventDispatcher = new TraceableEventDispatcher(
new EventDispatcher(),
new Stopwatch()
);

$eventDispatcher = $this->getEventDispatcher();
$eventDispatcher->addSubscriber(
new ResolveVirtualFieldSubscriber(
$converterRegistry,
Expand Down Expand Up @@ -316,11 +314,7 @@ public function testPersistExternalStorageField(): void
->method('insertNewField')
->willReturn(456);

$eventDispatcher = new TraceableEventDispatcher(
new EventDispatcher(),
new Stopwatch()
);

$eventDispatcher = $this->getEventDispatcher();
$eventDispatcher->addSubscriber(
new ResolveVirtualFieldSubscriber(
$converterRegistry,
Expand Down Expand Up @@ -378,4 +372,12 @@ public function testPersistExternalStorageField(): void
array_column($eventDispatcher->getCalledListeners(), 'pretty')
);
}

private function getEventDispatcher(): TraceableEventDispatcher
{
return new TraceableEventDispatcher(
new EventDispatcher(),
new Stopwatch()
);
}
}

0 comments on commit 69acebf

Please sign in to comment.