-
-
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 8 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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?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. |
||
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]> | ||
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.
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. Oh right misinterpreted the last review :) fixing it 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. I think the link should stay here to explain how mocks work. I remove the @author only right ? 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. 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); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.