Skip to content

Commit

Permalink
added new test case for new provider and refactored existing one
Browse files Browse the repository at this point in the history
  • Loading branch information
Sztig committed Jan 21, 2025
1 parent a6b7d2a commit 731f31c
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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\ContentForms\Content\Form\Provider;

use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Core\FieldType\TextLine\Value;
use Ibexa\Core\Helper\FieldsGroups\FieldsGroupsList;
use Ibexa\Core\Repository\Values\ContentType\FieldDefinition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormInterface;

abstract class AbstractGroupedContentFormFieldsProviderTest extends TestCase
{
protected function getFieldsGroupsListMock(): FieldsGroupsList
{
$mock = $this->createMock(FieldsGroupsList::class);
$mock
->expects($this->exactly(3))
->method('getFieldGroup')
->withConsecutive()
->willReturnOnConsecutiveCalls('group_1', 'group_2', 'group_2');

return $mock;
}

protected function getFormMockWithFieldData(
string $fieldDefIdentifier,
string $fieldTypeIdentifier
): FormInterface {
$formMock = $this
->getMockBuilder(FormInterface::class)
->disableOriginalConstructor()
->getMock();

$formMock
->expects($this->once())
->method('getViewData')
->willReturn(new FieldData([
'field' => new Field(['fieldDefIdentifier' => $fieldDefIdentifier]),
'fieldDefinition' => new FieldDefinition(['fieldTypeIdentifier' => $fieldTypeIdentifier]),
'value' => new Value('value'),
]));

$formMock
->expects($this->once())
->method('getName')
->willReturn($fieldDefIdentifier);

return $formMock;
}

protected function getTestForms(): array
{
return [
$this->getFormMockWithFieldData('first_field', 'first_field_type'),
$this->getFormMockWithFieldData('second_field', 'second_field_type'),
$this->getFormMockWithFieldData('third_field', 'third_field_type'),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,34 @@

namespace Ibexa\Tests\ContentForms\Content\Form\Provider;

use Ibexa\ContentForms\Content\Form\Provider\NonLocalizedGroupedContentFormFieldsProvider;
use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Core\FieldType\TextLine\Value;
use Ibexa\Core\Helper\FieldsGroups\FieldsGroupsList;
use Ibexa\Core\Repository\Values\ContentType\FieldDefinition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormInterface;
use Ibexa\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProvider;

final class GroupedContentFormFieldsProviderTest extends TestCase
final class GroupedContentFormFieldsProviderTest extends AbstractGroupedContentFormFieldsProviderTest
{
public function testGetGroupedFields(): void
{
$fieldsGroupsListMock = $this->createMock(FieldsGroupsList::class);
$fieldsGroupsListMock = $this->getFieldsGroupsListMock();
$fieldsGroupsListMock
->expects($this->exactly(3))
->method('getFieldGroup')
->withConsecutive()
->willReturnOnConsecutiveCalls('group_1', 'group_2', 'group_2');

$subject = new NonLocalizedGroupedContentFormFieldsProvider($fieldsGroupsListMock);

$form1 = $this->getFormMockWithFieldData(
'first_field',
'first_field_type',
);

$form2 = $this->getFormMockWithFieldData(
'second_field',
'second_field_type',
);

$form3 = $this->getFormMockWithFieldData(
'third_field',
'third_field_type',
);
->expects($this->once())
->method('getGroups')
->willReturn([
'group_1' => 'Group 1',
'group_2' => 'Group 2',
]);

$result = $subject->getGroupedFields([$form1, $form2, $form3]);
$subject = new GroupedContentFormFieldsProvider($fieldsGroupsListMock);
$result = $subject->getGroupedFields($this->getTestForms());

$expected = [
'group_1' => [
'Group 1' => [
0 => 'first_field',
],
'group_2' => [
'Group 2' => [
0 => 'second_field',
1 => 'third_field',
],
];

$this->assertEquals($expected, $result);
}

/**
* @return \Symfony\Component\Form\FormInterface
*/
private function getFormMockWithFieldData(
string $fieldDefIdentifier,
string $fieldTypeIdentifier
) {
$formMock = $this
->getMockBuilder(FormInterface::class)
->disableOriginalConstructor()
->getMock();
$formMock
->expects($this->once())
->method('getViewData')
->willReturn(new FieldData([
'field' => new Field(['fieldDefIdentifier' => $fieldDefIdentifier]),
'fieldDefinition' => new FieldDefinition(['fieldTypeIdentifier' => $fieldTypeIdentifier]),
'value' => new Value('value'),
]));
$formMock
->expects($this->once())
->method('getName')
->willReturn($fieldDefIdentifier);

return $formMock;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\ContentForms\Content\Form\Provider;

use Ibexa\ContentForms\Content\Form\Provider\NonLocalizedGroupedContentFormFieldsProvider;

class NonLocalizedGroupedContentFormFieldsProviderTest extends AbstractGroupedContentFormFieldsProviderTest
{
public function testGetGroupedFields(): void
{
$fieldsGroupsListMock = $this->getFieldsGroupsListMock();

$subject = new NonLocalizedGroupedContentFormFieldsProvider($fieldsGroupsListMock);
$result = $subject->getGroupedFields($this->getTestForms());

$expected = [
'group_1' => [
0 => 'first_field',
],
'group_2' => [
0 => 'second_field',
1 => 'third_field',
],
];

$this->assertEquals($expected, $result);
}
}

0 comments on commit 731f31c

Please sign in to comment.