Skip to content

Commit

Permalink
symfony 3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleMinotto committed Dec 8, 2015
1 parent ae3b8a8 commit d95b106
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 26 deletions.
6 changes: 5 additions & 1 deletion Form/DataTransformer/SimpleArrayTransformer.php
Expand Up @@ -74,9 +74,13 @@ public function reverseTransform($value)
$value = '';
}

if (!is_array($value)) {
$value = explode(',', $value);
}

// 1. Split the string with commas
// 2. Remove whitespaces around the values
// 3. Remove empty elements (like in "tag1,tag2, ,,tag3,tag4")
return array_filter(array_map('trim', explode(',', $value)));
return array_filter(array_map('trim', $value));
}
}
2 changes: 1 addition & 1 deletion Form/DoctrineOrmTypeGuesser.php
Expand Up @@ -14,7 +14,7 @@ class DoctrineOrmTypeGuesser extends BaseDoctrineOrmTypeGuesser
* @param string $class The fully qualified class name.
* @param string $property The name of the property to guess for.
*
* @return Guess\TypeGuess|null A guess for the field's type and options.
* @return TypeGuess|null A guess for the field's type and options.
*/
public function guessType($class, $property)
{
Expand Down
6 changes: 4 additions & 2 deletions Form/Type/SimpleArrayType.php
Expand Up @@ -17,7 +17,7 @@ class SimpleArrayType extends AbstractType
* @param FormBuilderInterface $builder The form builder.
* @param array $options The options.
*
* @see FormTypeExtensionInterface::buildForm()
* @see FormTypeExtensionInterface::buildForm()
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
Expand All @@ -35,7 +35,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
*/
public function getParent()
{
return 'text';
return method_exists(__CLASS__, 'getBlockPrefix')
? 'Symfony\Component\Form\Extension\Core\Type\TextType'
: 'text';
}

/**
Expand Down
22 changes: 0 additions & 22 deletions Tests/DataTransdormer/SimpleArrayTransformerTest.php

This file was deleted.

34 changes: 34 additions & 0 deletions Tests/DataTransformer/SimpleArrayTransformerTest.php
@@ -0,0 +1,34 @@
<?php

namespace EmanueleMinotto\SimpleArrayBundle\Tests\Form\DataTransformer;

use EmanueleMinotto\SimpleArrayBundle\Form\DataTransformer\SimpleArrayTransformer;
use PHPUnit_Framework_TestCase;

class SimpleArrayTransformerTest extends PHPUnit_Framework_TestCase
{
/**
* @var SimpleArrayTransformer
*/
protected $object;

public function setUp()
{
$this->object = new SimpleArrayTransformer();
}

public function testTransform()
{
$value = range('a', 'z');
shuffle($value);

$transformation = $this->object->transform($value);
$this->assertSame(implode(', ', $value), $transformation);
}

public function testTransformNull()
{
$transformation = $this->object->transform(null);
$this->assertSame('', $transformation);
}
}
28 changes: 28 additions & 0 deletions Tests/Type/SimpleArrayTypeTest.php
@@ -0,0 +1,28 @@
<?php

namespace EmanueleMinotto\SimpleArrayBundle\Tests\Form\Type;

use EmanueleMinotto\SimpleArrayBundle\Form\Type\SimpleArrayType;
use Symfony\Component\Form\Test\TypeTestCase;

class SimpleArrayTypeTest extends TypeTestCase
{
public function testSubmitValidData()
{
$formData = [
'test' => 'test',
'test2' => 'test2',
];

$type = new SimpleArrayType();
$form = $this->factory->create(
method_exists($type, 'getBlockPrefix') ? get_class($type) : $type
);

// submit the data to the form directly
$form->submit($formData);

$this->assertTrue($form->isSynchronized());
$this->assertEquals($formData, $form->getData());
}
}
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -6,6 +6,9 @@
}
],
"autoload": {
"exclude-from-classmap": [
"/Tests/"
],
"psr-4": {
"EmanueleMinotto\\SimpleArrayBundle\\": "."
}
Expand Down

0 comments on commit d95b106

Please sign in to comment.