Skip to content

Commit

Permalink
[Tests] Fixed failing Field Value Object Visitor test
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Mar 28, 2024
1 parent f317269 commit 3c6e078
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions tests/lib/Server/Output/ValueObjectVisitor/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

namespace Ibexa\Tests\Rest\Server\Output\ValueObjectVisitor;

use Ibexa\Contracts\Core\Repository\FieldType;
use Ibexa\Contracts\Core\Repository\FieldTypeService;
use Ibexa\Contracts\Core\Repository\Values\Content\Field as ApiField;
use Ibexa\Contracts\Rest\Output\Generator;
use Ibexa\Rest\FieldTypeProcessorRegistry;
use Ibexa\Rest\Output\FieldTypeSerializer;
use Ibexa\Rest\Server\Output\ValueObjectVisitor\Field;
use Ibexa\Tests\Rest\Output\ValueObjectVisitorBaseTest;
Expand All @@ -19,14 +21,18 @@
*/
final class FieldTest extends ValueObjectVisitorBaseTest
{
/** @var \Ibexa\Rest\Output\FieldTypeSerializer&\PHPUnit\Framework\MockObject\MockObject */
private FieldTypeSerializer $fieldTypeSerializer;
/** @var \Ibexa\Contracts\Core\Repository\FieldTypeService&\PHPUnit\Framework\MockObject\MockObject */
private FieldTypeService $fieldTypeService;

/** @var \Ibexa\Rest\FieldTypeProcessorRegistry&\PHPUnit\Framework\MockObject\MockObject */
private FieldTypeProcessorRegistry $fieldTypeProcessorRegistry;

protected function setUp(): void
{
parent::setUp();

$this->fieldTypeSerializer = $this->createMock(FieldTypeSerializer::class);
$this->fieldTypeService = $this->createMock(FieldTypeService::class);
$this->fieldTypeProcessorRegistry = $this->createMock(FieldTypeProcessorRegistry::class);
}

public function testVisit(): void
Expand All @@ -41,15 +47,11 @@ public function testVisit(): void
'fieldDefIdentifier' => 'foo',
'value' => 'foo',
'languageCode' => 'eng-GB',
'fieldTypeIdentifier' => 'ezfoo',
'fieldTypeIdentifier' => 'foo_field_type',
]
);

$this->mockFieldTypeSerializerSerializeContentFieldValue(
$generator,
$field,
'<value>foo</value>'
);
$this->mockSerializingFieldValue($field);

$visitor->visit(
$this->getVisitorMock(),
Expand All @@ -67,25 +69,24 @@ public function testVisit(): void
$this->assertContainsTag('fieldTypeIdentifier', $result);
}

private function mockFieldTypeSerializerSerializeContentFieldValue(
Generator $generator,
ApiField $field,
string $value
): void {
$this->fieldTypeSerializer
->method('serializeContentFieldValue')
->with(
$generator,
$field
)
->willReturn($value);
private function mockSerializingFieldValue(ApiField $field): void
{
$fieldTypeMock = $this->createMock(FieldType::class);
$fieldTypeIdentifier = $field->getFieldTypeIdentifier();

$fieldTypeMock->method('getFieldTypeIdentifier')->willReturn($fieldTypeIdentifier);
$fieldTypeMock->method('toHash')->with('foo')->willReturn(['value' => 'foo']);

$this->fieldTypeProcessorRegistry->method('hasProcessor')->with($fieldTypeIdentifier)->willReturn(false);

$this->fieldTypeService->method('getFieldType')->with($fieldTypeIdentifier)->willReturn($fieldTypeMock);
}

private function assertContainsTag(
string $tag,
string $result
): void {
$this->assertXMLTag(
self::assertXMLTag(
[
'tag' => $tag,
],
Expand All @@ -96,6 +97,11 @@ private function assertContainsTag(

protected function internalGetVisitor(): Field
{
return new Field($this->fieldTypeSerializer);
return new Field(
new FieldTypeSerializer(
$this->fieldTypeService,
$this->fieldTypeProcessorRegistry
)
);
}
}

0 comments on commit 3c6e078

Please sign in to comment.