-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-902: Refactored GroupedFields as a separate service (#46)
* IBX-902: Refactored GroupedFields as a separate service. https://issues.ibexa.co/browse/IBX-902 * Fixed method name with signature * Changes regarding CR * Changes regarding CR
- Loading branch information
Showing
9 changed files
with
182 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/contracts/Content/Form/Provider/GroupedContentFormFieldsProviderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems 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\Contracts\ContentForms\Content\Form\Provider; | ||
|
||
interface GroupedContentFormFieldsProviderInterface | ||
{ | ||
/** | ||
* @param \Symfony\Component\Form\FormInterface[] $fieldsDataForm | ||
* @phpstan-return array<string, array<int, string>> Array of fieldGroupIdentifier grouped by fieldGroupName. | ||
*/ | ||
public function getGroupedFields(array $fieldsDataForm): array; | ||
} |
40 changes: 40 additions & 0 deletions
40
src/lib/Content/Form/Provider/GroupedContentFormFieldsProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems 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\ContentForms\Content\Form\Provider; | ||
|
||
use eZ\Publish\Core\Helper\FieldsGroups\FieldsGroupsList; | ||
use Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface; | ||
|
||
final class GroupedContentFormFieldsProvider implements GroupedContentFormFieldsProviderInterface | ||
{ | ||
/** @var \eZ\Publish\Core\Helper\FieldsGroups\FieldsGroupsList */ | ||
private $fieldsGroupsList; | ||
|
||
public function __construct(FieldsGroupsList $fieldsGroupsList) | ||
{ | ||
$this->fieldsGroupsList = $fieldsGroupsList; | ||
} | ||
|
||
public function getGroupedFields(array $fieldsDataForm): array | ||
{ | ||
$fieldsGroups = $this->fieldsGroupsList->getGroups(); | ||
$groupedFields = []; | ||
|
||
foreach ($fieldsDataForm as $fieldForm) { | ||
/** @var \EzSystems\EzPlatformContentForms\Data\Content\FieldData $fieldData */ | ||
$fieldData = $fieldForm->getViewData(); | ||
$fieldGroupIdentifier = $this->fieldsGroupsList->getFieldGroup($fieldData->fieldDefinition); | ||
$fieldGroupName = $fieldsGroups[$fieldGroupIdentifier] ?? $this->fieldsGroupsList->getDefaultGroup(); | ||
|
||
$groupedFields[$fieldGroupName][] = $fieldForm->getName(); | ||
} | ||
|
||
return $groupedFields; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
tests/lib/Content/Form/Provider/GroupedContentFormFieldsProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems 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 eZ\Publish\API\Repository\Values\Content\Field; | ||
use eZ\Publish\Core\FieldType\TextLine\Value; | ||
use eZ\Publish\Core\Helper\FieldsGroups\FieldsGroupsList; | ||
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition; | ||
use EzSystems\EzPlatformContentForms\Data\Content\FieldData; | ||
use Ibexa\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProvider; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Form\FormInterface; | ||
|
||
final class GroupedContentFormFieldsProviderTest extends TestCase | ||
{ | ||
public function testGetGroupedFields(): void | ||
{ | ||
$fieldsGroupsListMock = $this->createMock(FieldsGroupsList::class); | ||
$fieldsGroupsListMock | ||
->expects($this->exactly(3)) | ||
->method('getFieldGroup') | ||
->withConsecutive() | ||
->willReturnOnConsecutiveCalls('group_1', 'group_2', 'group_2'); | ||
|
||
$fieldsGroupsListMock | ||
->expects($this->once()) | ||
->method('getGroups') | ||
->willReturn([ | ||
'group_1' => 'Group 1', | ||
'group_2' => 'Group 2', | ||
]); | ||
|
||
$subject = new GroupedContentFormFieldsProvider($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', | ||
); | ||
|
||
$result = $subject->getGroupedFields([$form1, $form2, $form3]); | ||
|
||
$expected = [ | ||
"Group 1" => [ | ||
0 => "first_field", | ||
], | ||
"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; | ||
} | ||
} |