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 @@ -54,7 +54,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,124 @@
<?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.

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
*
* @author Jonathan Boyer <[email protected]>
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.

@author AFAIK it's not common in tests classes, instead you can move it to TagArrayToStringTransformer class ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh right misinterpreted the last review :) fixing it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the link should stay here to explain how mocks work. I remove the @author only right ?

Copy link
Member

Choose a reason for hiding this comment

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

That's right.

*/
class TagArrayToStringTransformerTest extends \PHPUnit\Framework\TestCase
{
/**
* Get a mocked instance of the TagArrayToStringTransformer.
*
* @return TagArrayToStringTransformer
*/
public function getMockedTransformer($findByReturn = [])
{
$tagRepository = $this->getMockBuilder(EntityRepository::class)
->disableOriginalConstructor()
->getMock();
$tagRepository->expects($this->any())
->method('findBy')
->will($this->returnValue($findByReturn));

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

return new TagArrayToStringTransformer($entityManager);
}

/**
* Creates a new TagEntity instance.
*
* @param $name
*
* @return Tag
*/
public function createTag($name)
{
$tag = new Tag();
$tag->setName($name);

return $tag;
}

/**
* Ensures tags are created correctly.
*/
public function testCreateTheRightAmountOfTags()
{
$tags = $this->getMockedTransformer()->reverseTransform('Hello, Demo, How');
$this->assertCount(3, $tags);
$this->assertSame('Hello', $tags[0]->getName());
}

/**
* Too many commas.
*/
public function testCreateTheRightAmountOfTagsWithTooManyCommas()
{
$transformer = $this->getMockedTransformer();
$this->assertCount(3, $transformer->reverseTransform('Hello, Demo,, How'));
$this->assertCount(3, $transformer->reverseTransform('Hello, Demo, How,'));
}

/**
* Spaces at the end (and beginning) of a world shouldn't matter.
*/
public function testTrimNames()
{
$tags = $this->getMockedTransformer()->reverseTransform(' Hello ');
$this->assertSame('Hello', $tags[0]->getName());
}

/**
* Duplicate tags shouldn't create new entities.
*/
public function testDuplicateNames()
{
$tags = $this->getMockedTransformer()->reverseTransform('Hello, Hello, Hello');
$this->assertCount(1, $tags);
}

/**
* This test ensure that the transformer uses tag 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]);
}

/**
* Tags should be transformed into a string.
*/
public function testTransform()
{
$persistedTags = [
$this->createTag('Hello'),
$this->createTag('World'),
];
$transformed = $this->getMockedTransformer()->transform($persistedTags);
$this->assertSame('Hello,World', $transformed);
}
}