Skip to content

Commit

Permalink
[TASK] Improve test coverage for transformation suite
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Jul 12, 2024
1 parent 13052d1 commit 155c7ee
Show file tree
Hide file tree
Showing 12 changed files with 486 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Classes/Form/Transformation/FormDataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ protected function transform(Form $form, string $path, $value)
]
)['value'];
if ($value === $originalValue) {
$value = $this->registry->resolveDataTransformerByType($transformType)
->transform($object, $transformType, $value);
$transformer = $this->registry->resolveDataTransformerByType($transformType);
$value = $transformer->transform($object, $transformType, $value);
}
$value = HookHandler::trigger(
HookHandler::VALUE_AFTER_TRANSFORM,
Expand Down
14 changes: 12 additions & 2 deletions Classes/Form/Transformation/Transformer/ArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use FluidTYPO3\Flux\Attribute\DataTransformer;
use FluidTYPO3\Flux\Form\FormInterface;
use FluidTYPO3\Flux\Form\Transformation\DataTransformerInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Array Transformer
Expand All @@ -30,11 +31,20 @@ public function getPriority(): int
}

/**
* @param string $value
* @param string|array $value
* @return array|null
*/
public function transform(FormInterface $component, string $type, $value)
{
return explode(',', (string) $value) ?: null;
if (is_array($value)) {
return $value;
}
if (is_string($value)) {
return GeneralUtility::trimExplode(',', $value, true);
}
if (is_iterable($value)) {
return iterator_to_array($value);
}
return (array) $value;
}
}
21 changes: 21 additions & 0 deletions Tests/Unit/Attribute/DataTransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace FluidTYPO3\Flux\Tests\Unit\Attribute;

/*
* This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Attribute\DataTransformer;
use PHPUnit\Framework\TestCase;

class DataTransformerTest extends TestCase
{
public function testSetsIdentifierInConstructor(): void
{
$subject = new DataTransformer('ident');
self::assertSame('ident', $subject->identifier);
}
}
48 changes: 48 additions & 0 deletions Tests/Unit/Form/Transformation/DataTransformerRegistryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace FluidTYPO3\Flux\Tests\Unit\Form\Transformation;

/*
* This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Form\Transformation\DataTransformerRegistry;
use FluidTYPO3\Flux\Form\Transformation\Transformer\FloatTransformer;
use FluidTYPO3\Flux\Form\Transformation\Transformer\IntegerTransformer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ServiceLocator;

class DataTransformerRegistryTest extends TestCase
{
private DataTransformerRegistry $subject;

protected function setUp(): void
{
$locator = $this->getMockBuilder(ServiceLocator::class)
->onlyMethods(['getProvidedServices', 'get'])
->disableOriginalConstructor()
->getMock();
$locator->method('getProvidedServices')->willReturn(['a' => true, 'b' => true]);
$locator->method('get')->willReturnMap(
[
['a', new FloatTransformer()],
['b', new IntegerTransformer()],
]
);
$this->subject = new DataTransformerRegistry($locator);
parent::setUp();
}

public function testResolveDataTransformerByTypeReturnsMatchedType(): void
{
self::assertInstanceOf(IntegerTransformer::class, $this->subject->resolveDataTransformerByType('int'));
}

public function testResolveDataTransformerByTypeThrowsExceptionForUnmatchedType(): void
{
self::expectExceptionCode(1720346755);
$this->subject->resolveDataTransformerByType('unknown');
}
}
39 changes: 26 additions & 13 deletions Tests/Unit/Form/Transformation/FormDataTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use FluidTYPO3\Flux\Form\Transformation\Transformer\BooleanTransformer;
use FluidTYPO3\Flux\Form\Transformation\Transformer\FloatTransformer;
use FluidTYPO3\Flux\Form\Transformation\Transformer\IntegerTransformer;
use FluidTYPO3\Flux\Form\Transformation\Transformer\ObjectTransformer;
use FluidTYPO3\Flux\Tests\Fixtures\Data\Xml;
use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase;
use Symfony\Component\DependencyInjection\ServiceLocator;
Expand All @@ -42,6 +43,7 @@ protected function setUp(): void
'flux.datatransformer.boolean' => BooleanTransformer::class,
'flux.datatransformer.integer' => IntegerTransformer::class,
'flux.datatransformer.float' => FloatTransformer::class,
'flux.datatransformer.object' => ObjectTransformer::class,
]
);
$serviceLocator->method('get')->willReturnMap(
Expand All @@ -50,6 +52,7 @@ protected function setUp(): void
['flux.datatransformer.boolean', new BooleanTransformer()],
['flux.datatransformer.integer', new IntegerTransformer()],
['flux.datatransformer.float', new FloatTransformer()],
['flux.datatransformer.object', new ObjectTransformer()],
]
);

Expand All @@ -58,13 +61,8 @@ protected function setUp(): void
->disableOriginalConstructor()
->getMock();

$this->registry = $this->getMockBuilder(DataTransformerRegistry::class)
->setConstructorArgs([$serviceLocator])
->getMock();

$this->subject = $this->getMockBuilder(FormDataTransformer::class)
->setConstructorArgs($this->getConstructorArguments())
->getMock();
$this->registry = new DataTransformerRegistry($serviceLocator);
$this->subject = new FormDataTransformer($this->flexFormService, $this->registry);

parent::setUp();
}
Expand Down Expand Up @@ -158,13 +156,28 @@ public function getValuesAndTransformations(): array
['0', 'integer', 0],
['0.12', 'float', 0.12],
['1,2,3', 'array', [1, 2, 3]],
['123,321', 'InvalidClass', '123'],
['1', 'boolean', true],
/*
[date('Ymd'), 'DateTime', new \DateTime(date('Ymd'))],
['1,2', ObjectStorage::class . '<\\Invalid>', null],
['bar', self::class . '->fixtureTransformToFooString', 'foo'],
*/
];
}

public function testTransformationSectionObject(): void
{
$form = $this->getMockBuilder(Form::class)->setMethods(['dummy'])->getMock();
$section = $form->createContainer(Form\Container\Section::class, 'section');
$object = $section->createContainer(Form\Container\SectionObject::class, 'object');
$object->setTransform(\ArrayObject::class);
$object->createField(Form\Field\Input::class, 'foo');
$object->createField(Form\Field\Input::class, 'baz');

$data = ['section' => ['abcdef123456' => ['object' => ['foo' => 'bar', 'baz' => 'test']]]];

$transformed = $this->subject->transformAccordingToConfiguration($data, $form);
$expected = $transformed;
$expected['section']['abcdef123456']['object'] = new \ArrayObject(
$expected['section']['abcdef123456']['object']
);

self::assertEquals($expected, $transformed);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
namespace FluidTYPO3\Flux\Tests\Unit\Form\Transformation\Transformer;

/*
* This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Form;
use FluidTYPO3\Flux\Form\Transformation\Transformer\ArrayTransformer;
use PHPUnit\Framework\TestCase;

class ArrayTransformerTest extends TestCase
{
private ArrayTransformer $subject;

public function setUp(): void
{
$this->subject = new ArrayTransformer();

parent::setUp();
}

public function testGetPriority(): void
{
self::assertSame(0, $this->subject->getPriority());
}

/**
* @dataProvider getCanTransformToTypeTestValues
*/
public function testCanTransformToType(bool $expected, string $type): void
{
self::assertSame($expected, $this->subject->canTransformToType($type));
}

public function getCanTransformToTypeTestValues(): array
{
return [
'supports array' => [true, 'array'],
'does not support bool' => [false, 'bool'],
'does not support string' => [false, 'string'],
];
}

/**
* @dataProvider getTransformTestValues
* @param mixed $input
*/
public function testTransform(array $expected, $input): void
{
$field = Form::create()->createField(Form\Field\Input::class, 'test');
$output = $this->subject->transform($field, 'array', $input);
self::assertSame($expected, $output);
}

public function getTransformTestValues(): array
{
return [
'already array' => [['foo'], ['foo']],
'string CSV' => [['foo', 'bar'], 'foo,bar'],
'integer' => [[1], 1],
'iterator' => [['foo', 'bar'], new \ArrayIterator(['foo', 'bar'])],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
namespace FluidTYPO3\Flux\Tests\Unit\Form\Transformation\Transformer;

/*
* This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Form;
use FluidTYPO3\Flux\Form\Transformation\Transformer\BooleanTransformer;
use PHPUnit\Framework\TestCase;

class BooleanTransformerTest extends TestCase
{
private BooleanTransformer $subject;

public function setUp(): void
{
$this->subject = new BooleanTransformer();

parent::setUp();
}

public function testGetPriority(): void
{
self::assertSame(0, $this->subject->getPriority());
}

/**
* @dataProvider getCanTransformToTypeTestValues
*/
public function testCanTransformToType(bool $expected, string $type): void
{
self::assertSame($expected, $this->subject->canTransformToType($type));
}

public function getCanTransformToTypeTestValues(): array
{
return [
'supports bool' => [true, 'bool'],
'supports boolean' => [true, 'boolean'],
'does not support array' => [false, 'array'],
'does not support string' => [false, 'string'],
];
}

/**
* @dataProvider getTransformTestValues
* @param mixed $input
*/
public function testTransform(bool $expected, $input): void
{
$field = Form::create()->createField(Form\Field\Input::class, 'test');
$output = $this->subject->transform($field, 'bool', $input);
self::assertSame($expected, $output);
}

public function getTransformTestValues(): array
{
return [
'already boolean' => [true, true],
'string true-ish' => [true, '1'],
'string false-ish' => [false, '0'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class FileTransformerTest extends AbstractTestCase
{
private ConnectionPool $connectionPool;
private ResourceFactory $resourceFactory;
private FileTransformer $subject;

protected function setUp(): void
{
Expand All @@ -42,6 +43,11 @@ protected function setUp(): void
parent::setUp();
}

public function testGetPriority(): void
{
self::assertSame(0, $this->subject->getPriority());
}

/**
* @dataProvider getTransformWithFileTargetTypesTestValues
* @param mixed $expected
Expand Down
Loading

0 comments on commit 155c7ee

Please sign in to comment.