Skip to content

Commit

Permalink
fix: Reorganize subscriber test
Browse files Browse the repository at this point in the history
  • Loading branch information
Nattfarinn committed Apr 25, 2024
1 parent 69acebf commit 84a16b0
Showing 1 changed file with 108 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,9 @@

final class ResolveVirtualFieldSubscriberTest extends TestCase
{
private function getContent(): Content
{
$versionInfo = $this->getVersionInfo();

$content = new Content();
$content->versionInfo = $versionInfo;
$content->fields = [];

return $content;
}

private function getVersionInfo(): VersionInfo
{
$versionInfo = new VersionInfo();
$versionInfo->versionNo = 123;

return $versionInfo;
}

public function testResolveVirtualField(): void
{
$converterRegistry = $this->createMock(ConverterRegistry::class);
$converterRegistry->method('getConverter')
->willReturn($this->createMock(Converter::class));
$converterRegistry = $this->getConverterRegistry();

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

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

$content = $this->getContent();
$fieldDefinition = new FieldDefinition([
'id' => 123,
'identifier' => 'example_field',
'fieldType' => 'some_type',
'defaultValue' => new Content\FieldValue(),
]);

$event = new ResolveMissingFieldEvent(
$content,
$fieldDefinition,
'eng-GB'
$event = $eventDispatcher->dispatch(
new ResolveMissingFieldEvent(
$this->getContent(),
new FieldDefinition([
'id' => 123,
'identifier' => 'example_field',
'fieldType' => 'some_type',
'defaultValue' => new Content\FieldValue(),
]),
'eng-GB'
)
);

$event = $eventDispatcher->dispatch($event);

$expected = new Content\Field([
'id' => null,
'fieldDefinitionId' => 123,
Expand Down Expand Up @@ -113,9 +86,7 @@ public function testResolveVirtualField(): void

public function testResolveVirtualExternalStorageField(): void
{
$converterRegistry = $this->createMock(ConverterRegistry::class);
$converterRegistry->method('getConverter')
->willReturn($this->createMock(Converter::class));
$converterRegistry = $this->getConverterRegistry();

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

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

$content = $this->getContent();
$fieldDefinition = new FieldDefinition([
'id' => 678,
'identifier' => 'example_external_field',
'fieldType' => 'external_type_virtual',
'defaultValue' => new Content\FieldValue(),
]);

$event = new ResolveMissingFieldEvent(
$content,
$fieldDefinition,
'eng-GB'
$event = $eventDispatcher->dispatch(
new ResolveMissingFieldEvent(
$this->getContent(),
new FieldDefinition([
'id' => 678,
'identifier' => 'example_external_field',
'fieldType' => 'external_type_virtual',
'defaultValue' => new Content\FieldValue(),
]),
'eng-GB'
)
);

$event = $eventDispatcher->dispatch($event);

$expected = new Content\Field([
'id' => null,
'fieldDefinitionId' => 678,
Expand Down Expand Up @@ -210,9 +175,7 @@ public function getIndexData(VersionInfo $versionInfo, Field $field, array $cont

public function testPersistEmptyExternalStorageField(): void
{
$converterRegistry = $this->createMock(ConverterRegistry::class);
$converterRegistry->method('getConverter')
->willReturn($this->createMock(Converter::class));
$converterRegistry = $this->getConverterRegistry();

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

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

$content = $this->getContent();
$fieldDefinition = new FieldDefinition([
'id' => 123,
'identifier' => 'example_field',
'fieldType' => 'external_type',
'defaultValue' => new Content\FieldValue(),
]);

$event = new ResolveMissingFieldEvent(
$content,
$fieldDefinition,
'eng-GB'
$event = $eventDispatcher->dispatch(
new ResolveMissingFieldEvent(
$this->getContent(),
new FieldDefinition([
'id' => 123,
'identifier' => 'example_field',
'fieldType' => 'external_type',
'defaultValue' => new Content\FieldValue(),
]),
'eng-GB'
)
);

$event = $eventDispatcher->dispatch($event);

$expected = new Content\Field([
'id' => 567,
'fieldDefinitionId' => 123,
Expand Down Expand Up @@ -291,9 +248,7 @@ public function testPersistEmptyExternalStorageField(): void

public function testPersistExternalStorageField(): void
{
$converterRegistry = $this->createMock(ConverterRegistry::class);
$converterRegistry->method('getConverter')
->willReturn($this->createMock(Converter::class));
$converterRegistry = $this->getConverterRegistry();

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

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

$content = $this->getContent();
$fieldDefinition = new FieldDefinition([
'id' => 123,
'identifier' => 'example_field',
'fieldType' => 'external_type',
'defaultValue' => new Content\FieldValue([
'data' => ['some_data' => 'to_be_stored'],
]),
]);

$event = new ResolveMissingFieldEvent(
$content,
$fieldDefinition,
'eng-GB'
$event = $eventDispatcher->dispatch(
new ResolveMissingFieldEvent(
$this->getContent(),
new FieldDefinition([
'id' => 123,
'identifier' => 'example_field',
'fieldType' => 'external_type',
'defaultValue' => new Content\FieldValue([
'data' => ['some_data' => 'to_be_stored'],
]),
]),
'eng-GB'
)
);

$event = $eventDispatcher->dispatch($event);

$expected = new Content\Field([
'id' => 456,
'fieldDefinitionId' => 123,
Expand Down Expand Up @@ -373,11 +322,51 @@ public function testPersistExternalStorageField(): void
);
}

private function getEventDispatcher(): TraceableEventDispatcher
private function getContent(): Content
{
$versionInfo = $this->getVersionInfo();

$content = new Content();
$content->versionInfo = $versionInfo;
$content->fields = [];

return $content;
}

private function getVersionInfo(): VersionInfo
{
$versionInfo = new VersionInfo();
$versionInfo->versionNo = 123;

return $versionInfo;
}

private function getEventDispatcher(
ConverterRegistry $converterRegistry,
StorageRegistry $storageRegistry,
ContentGateway $contentGateway
): TraceableEventDispatcher {
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber(
new ResolveVirtualFieldSubscriber(
$converterRegistry,
$storageRegistry,
$contentGateway,
)
);

return new TraceableEventDispatcher(
new EventDispatcher(),
$eventDispatcher,
new Stopwatch()
);
}

private function getConverterRegistry(): ConverterRegistry
{
$converterRegistry = $this->createMock(ConverterRegistry::class);
$converterRegistry->method('getConverter')
->willReturn($this->createMock(Converter::class));

return $converterRegistry;
}
}

0 comments on commit 84a16b0

Please sign in to comment.