From 003fa77438b13ceb9ceda245d6ad257801453d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Mon, 12 Oct 2015 21:43:24 +0200 Subject: Add tests for the StringToListTransformer class --- .../DataTransformer/StringToListTransformer.php | 14 +++++- .../StringToListTransformerTest.php | 50 ++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Tests/Form/DataTransformer/StringToListTransformerTest.php diff --git a/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php b/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php index 332a91b8..23488d35 100644 --- a/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php +++ b/src/Wallabag/CoreBundle/Form/DataTransformer/StringToListTransformer.php @@ -6,10 +6,20 @@ use Doctrine\Common\Persistence\ObjectManager; use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\Exception\TransformationFailedException; +/** + * Transforms a comma-separated list to a proper PHP array. + * Example: the string "foo, bar" will become the array ["foo", "bar"] + */ class StringToListTransformer implements DataTransformerInterface { + /** + * @var string + */ private $separator; + /** + * @param string $separator The separator used in the list. + */ public function __construct($separator = ',') { $this->separator = $separator; @@ -40,10 +50,10 @@ class StringToListTransformer implements DataTransformerInterface */ public function reverseTransform($string) { - if (!$string) { + if ($string === null) { return null; } - return array_filter(array_map('trim', explode($this->separator, $string))); + return array_values(array_filter(array_map('trim', explode($this->separator, $string)))); } } diff --git a/src/Wallabag/CoreBundle/Tests/Form/DataTransformer/StringToListTransformerTest.php b/src/Wallabag/CoreBundle/Tests/Form/DataTransformer/StringToListTransformerTest.php new file mode 100644 index 00000000..d114e5f3 --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/Form/DataTransformer/StringToListTransformerTest.php @@ -0,0 +1,50 @@ +assertSame($expectedResult, $transformer->transform($inputData)); + } + + public function transformProvider() + { + return array( + array( null, '' ), + array( array(), '' ), + array( array('single value'), 'single value' ), + array( array('first value', 'second value'), 'first value,second value' ), + ); + } + + /** + * @dataProvider reverseTransformProvider + */ + public function testReverseTransformWithValidData($inputData, $expectedResult) + { + $transformer = new StringToListTransformer(); + + $this->assertSame($expectedResult, $transformer->reverseTransform($inputData)); + } + + public function reverseTransformProvider() + { + return array( + array( null, null ), + array( '', array() ), + array( 'single value', array('single value') ), + array( 'first value,second value', array('first value', 'second value') ), + array( 'first value, second value', array('first value', 'second value') ), + array( 'first value, , second value', array('first value', 'second value') ), + ); + } +} -- cgit v1.2.3