-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from all commits
a8de079
55d5bb4
2ca2e9b
f69514a
3e891f5
72a0f85
b596f77
e3ab52c
75734c6
0f14c57
15a8d39
ae2391f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -54,7 +55,7 @@ public function reverseTransform($string) | |
return []; | ||
} | ||
|
||
$names = explode(',', $string); | ||
$names = array_filter(array_unique(array_map('trim', explode(',', $string)))); | ||
|
||
// Get the current tags and find the new ones that should be created. | ||
$tags = $this->manager->getRepository(Tag::class)->findBy([ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing license docblock. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed with the next commit There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.