Skip to content

Commit

Permalink
[TASK] Refactor FormDataTransformerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Aug 1, 2023
1 parent faa1f84 commit e4129b5
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions Tests/Unit/Form/Transformation/FormDataTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,6 @@ class FormDataTransformerTest extends AbstractTestCase

protected function setUp(): void
{
if (!class_exists(FrontendUser::class)) {
$this->markTestSkipped('Skipping test with FrontendUser dependency');
}

parent::setUp();

$this->frontendUser = $this->getMockBuilder(FrontendUser::class)
->disableOriginalConstructor()
->getMock();

$this->frontendUserRepository = $this->getMockBuilder(FrontendUserRepository::class)
->setMethods(['findByUid'])
->disableOriginalConstructor()
->getMock();
$this->frontendUserRepository->method('findByUid')->willReturn($this->frontendUser);

$this->singletonInstances[FrontendUserRepository::class] = $this->frontendUserRepository;
$this->singletonInstances[Repository::class] = $this->getMockBuilder(Repository::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -65,6 +48,22 @@ protected function setUp(): void
parent::setUp();
}

private function initializeFrontendUserFixtures(): void
{

$this->frontendUser = $this->getMockBuilder(FrontendUser::class)
->disableOriginalConstructor()
->getMock();

$this->frontendUserRepository = $this->getMockBuilder(FrontendUserRepository::class)
->setMethods(['findByUid'])
->disableOriginalConstructor()
->getMock();
$this->frontendUserRepository->method('findByUid')->willReturn($this->frontendUser);

$this->singletonInstances[FrontendUserRepository::class] = $this->frontendUserRepository;
}

public function fixtureTransformToFooString(): string
{
return 'foo';
Expand Down Expand Up @@ -98,9 +97,37 @@ public function getValuesAndTransformations(): array
['123,321', 'InvalidClass', '123'],
[date('Ymd'), 'DateTime', new \DateTime(date('Ymd'))],
['1', 'boolean', true],
['1,2', ObjectStorage::class . '<' . FrontendUser::class . '>', null],
['1,2', ObjectStorage::class . '<\\Invalid>', null],
['bar', self::class . '->fixtureTransformToFooString', 'foo'],
];
}

/**
* @dataProvider getValuesAndTransformationsForDomainObjects
* @param mixed $value
* @param mixed $expected
*/
public function testTransformationOfDomainObjects($value, string $transformation, $expected): void
{
if (!class_exists(FrontendUser::class)) {
$this->markTestSkipped('Skipping test with FrontendUser dependency');
}

$this->subject->method('loadObjectsFromRepository')->willReturn([]);
$form = $this->getMockBuilder(Form::class)->setMethods(['dummy'])->getMock();
$form->createField(Form\Field\Input::class, 'field')->setTransform($transformation);
$transformed = $this->subject->transformAccordingToConfiguration(['field' => $value], $form);
$this->assertNotSame(
$expected,
$transformed,
'Transformation type ' . $transformation . ' failed; values are still identical'
);
}

public function getValuesAndTransformationsForDomainObjects(): array
{
return [
['1,2', ObjectStorage::class . '<' . FrontendUser::class . '>', null],
['1', FrontendUser::class, $this->frontendUser],
];
}
Expand Down Expand Up @@ -157,6 +184,9 @@ public function getTransformWithFileTargetTypesTestValues(): array

public function testSupportsFindByIdentifiers(): void
{
if (!class_exists(FrontendUser::class)) {
$this->markTestSkipped('Skipping test with FrontendUser dependency');
}
$repository = $this->getMockBuilder(FrontendUserGroupRepository::class)
->setMethods(['findByUid'])
->disableOriginalConstructor()
Expand Down

0 comments on commit e4129b5

Please sign in to comment.