Skip to content

Commit

Permalink
[Tests] Added test coverage for Field ValueObjectVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk committed Dec 7, 2023
1 parent c7fa4ab commit 747f725
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions tests/lib/Server/Output/ValueObjectVisitor/FieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

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

use Ibexa\Contracts\Core\Repository\Values\Content\Field as ApiField;
use Ibexa\Contracts\Rest\Output\Generator;
use Ibexa\Rest\Output\FieldTypeSerializer;
use Ibexa\Rest\Server\Output\ValueObjectVisitor\Field;
use Ibexa\Tests\Rest\Output\ValueObjectVisitorBaseTest;

/**
* @covers \Ibexa\Rest\Server\Output\ValueObjectVisitor\Field
*/
final class FieldTest extends ValueObjectVisitorBaseTest
{
/** @var \Ibexa\Rest\Output\FieldTypeSerializer&\PHPUnit\Framework\MockObject\MockObject */
private FieldTypeSerializer $fieldTypeSerializer;

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

$this->fieldTypeSerializer = $this->createMock(FieldTypeSerializer::class);
}

public function testVisit(): void
{
$visitor = $this->getVisitor();
$generator = $this->getGenerator();

$generator->startDocument(null);
$field = new ApiField(
[
'id' => 1,
'fieldDefIdentifier' => 'foo',
'value' => 'foo',
'languageCode' => 'eng-GB',
'fieldTypeIdentifier' => 'ezfoo',
]
);

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

$visitor->visit(
$this->getVisitorMock(),
$generator,
$field
);

$result = $generator->endDocument(null);

$this->assertContainsTag('field', $result);
$this->assertContainsTag('id', $result);
$this->assertContainsTag('fieldDefinitionIdentifier', $result);
$this->assertContainsTag('value', $result);
$this->assertContainsTag('languageCode', $result);
$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 assertContainsTag(
string $tag,
string $result
): void {
$this->assertXMLTag(
[
'tag' => $tag,
],
$result,
"Invalid <$tag> element.",
);
}

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

0 comments on commit 747f725

Please sign in to comment.