Skip to content

New tests, TagArrayToStringTransformer #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 14, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* See http://symfony.com/doc/current/form/data_transformers.html
*
* @author Yonel Ceruto <[email protected]>
* @author Jonathan Boyer <[email protected]>
*/
class TagArrayToStringTransformer implements DataTransformerInterface
{
Expand Down Expand Up @@ -54,7 +55,7 @@ public function reverseTransform($string)
return [];
}

$names = explode(',', $string);
$names = array_filter(array_unique(array_map('trim', explode(',', $string))));
Copy link
Member

@yceruto yceruto Mar 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tagsinput plugins itself does not allows duplicated, empty tags, extra spaces, double comma (on client side), BUT it could be hacked :) so I'm 👍 to check this on server side as double validation. Maybe I would add strtolower also to keep consistency with current ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about strtolower, for instance I want my "JavaScript", "WordPress" tags to keep the upcases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could still add this transformation within the constructor

public function __construct(ObjectManager $manager, $forceStrToLower = false)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep things simple and let's not add the $forceStrToLower option.


// Get the current tags and find the new ones that should be created.
$tags = $this->manager->getRepository(Tag::class)->findBy([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing license docblock.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with the next commit

Copy link
Member

@yceruto yceruto Mar 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests\AppBundle\Form\DataTransformer;

use AppBundle\Entity\Tag;
use AppBundle\Form\DataTransformer\TagArrayToStringTransformer;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;

/**
* Tests that tags are transformed correctly using the data transformer.
*
* See http://symfony.com/doc/current/testing/database.html
*/
class TagArrayToStringTransformerTest extends \PHPUnit\Framework\TestCase
{
/**
* Ensures that tags are created correctly.
*/
public function testCreateTheRightAmountOfTags()
{
$tags = $this->getMockedTransformer()->reverseTransform('Hello, Demo, How');

$this->assertCount(3, $tags);
$this->assertSame('Hello', $tags[0]->getName());
}

/**
* Ensures that empty tags and errors in the number of commas are
* dealed correctly.
*/
public function testCreateTheRightAmountOfTagsWithTooManyCommas()
{
$transformer = $this->getMockedTransformer();

$this->assertCount(3, $transformer->reverseTransform('Hello, Demo,, How'));
$this->assertCount(3, $transformer->reverseTransform('Hello, Demo, How,'));
}

/**
* Ensures that leading/trailing spaces are ignored for tag names.
*/
public function testTrimNames()
{
$tags = $this->getMockedTransformer()->reverseTransform(' Hello ');

$this->assertSame('Hello', $tags[0]->getName());
}

/**
* Ensures that duplicated tag names are ignored.
*/
public function testDuplicateNames()
{
$tags = $this->getMockedTransformer()->reverseTransform('Hello, Hello, Hello');

$this->assertCount(1, $tags);
}

/**
* Ensures that the transformer uses tags already persisted in the database.
*/
public function testUsesAlreadyDefinedTags()
{
$persistedTags = [
$this->createTag('Hello'),
$this->createTag('World'),
];
$tags = $this->getMockedTransformer($persistedTags)->reverseTransform('Hello, World, How, Are, You');

$this->assertCount(5, $tags);
$this->assertSame($persistedTags[0], $tags[0]);
$this->assertSame($persistedTags[1], $tags[1]);
}

/**
* Ensures that the transformation from Tag instances to a simple string
* works as expected.
*/
public function testTransform()
{
$persistedTags = [
$this->createTag('Hello'),
$this->createTag('World'),
];
$transformed = $this->getMockedTransformer()->transform($persistedTags);

$this->assertSame('Hello,World', $transformed);
}

/**
* This helper method mocks the real TagArrayToStringTransformer class to
* simplify the tests. See https://phpunit.de/manual/current/en/test-doubles.html.
*
* @param array $findByReturnValues The values returned when calling to the findBy() method
*
* @return TagArrayToStringTransformer
*/
private function getMockedTransformer($findByReturnValues = [])
{
$tagRepository = $this->getMockBuilder(EntityRepository::class)
->disableOriginalConstructor()
->getMock();
$tagRepository->expects($this->any())
->method('findBy')
->will($this->returnValue($findByReturnValues));

$entityManager = $this
->getMockBuilder(ObjectManager::class)
->disableOriginalConstructor()
->getMock();
$entityManager->expects($this->any())
->method('getRepository')
->will($this->returnValue($tagRepository));

return new TagArrayToStringTransformer($entityManager);
}

/**
* This helper method creates a Tag instance for the given tag name.
*
* @param string $name
*
* @return Tag
*/
private function createTag($name)
{
$tag = new Tag();
$tag->setName($name);

return $tag;
}
}