forked from FluidTYPO3/flux
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Improve test coverage for transformation suite
- Loading branch information
1 parent
13052d1
commit 155c7ee
Showing
12 changed files
with
486 additions
and
18 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
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
48
Tests/Unit/Form/Transformation/DataTransformerRegistryTest.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,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'); | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
Tests/Unit/Form/Transformation/Transformer/ArrayTransformerTest.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,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'])], | ||
]; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
Tests/Unit/Form/Transformation/Transformer/BooleanTransformerTest.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,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'], | ||
]; | ||
} | ||
} |
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
Oops, something went wrong.