Skip to content

Commit 73d50dd

Browse files
committed
feature #501 New tests, TagArrayToStringTransformer (Grafikart, javiereguiluz)
This PR was merged into the master branch. Discussion ---------- New tests, TagArrayToStringTransformer Added some tests to demonstrate how to mock a Repository and test things without the database Commits ------- ae2391f Fixed code syntax issue reported by Travis CI 15a8d39 Minor changes 0f14c57 Added the missing license header and removed the author info 75734c6 Added Jonathan to the list of authors e3ab52c Fixed more typos b596f77 Fixed typo 72a0f85 Simplified code 3e891f5 Created a new case f69514a Merge remote-tracking branch 'origin/tag-test' into tag-test 2ca2e9b Fixed some typos 55d5bb4 Simplified some code a8de079 New tests, TagArrayToStringTransformer
2 parents ed7ce1a + ae2391f commit 73d50dd

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

src/AppBundle/Form/DataTransformer/TagArrayToStringTransformer.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* See http://symfony.com/doc/current/form/data_transformers.html
2323
*
2424
* @author Yonel Ceruto <[email protected]>
25+
* @author Jonathan Boyer <[email protected]>
2526
*/
2627
class TagArrayToStringTransformer implements DataTransformerInterface
2728
{
@@ -54,7 +55,7 @@ public function reverseTransform($string)
5455
return [];
5556
}
5657

57-
$names = explode(',', $string);
58+
$names = array_filter(array_unique(array_map('trim', explode(',', $string))));
5859

5960
// Get the current tags and find the new ones that should be created.
6061
$tags = $this->manager->getRepository(Tag::class)->findBy([
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\AppBundle\Form\DataTransformer;
13+
14+
use AppBundle\Entity\Tag;
15+
use AppBundle\Form\DataTransformer\TagArrayToStringTransformer;
16+
use Doctrine\Common\Persistence\ObjectManager;
17+
use Doctrine\ORM\EntityRepository;
18+
19+
/**
20+
* Tests that tags are transformed correctly using the data transformer.
21+
*
22+
* See http://symfony.com/doc/current/testing/database.html
23+
*/
24+
class TagArrayToStringTransformerTest extends \PHPUnit\Framework\TestCase
25+
{
26+
/**
27+
* Ensures that tags are created correctly.
28+
*/
29+
public function testCreateTheRightAmountOfTags()
30+
{
31+
$tags = $this->getMockedTransformer()->reverseTransform('Hello, Demo, How');
32+
33+
$this->assertCount(3, $tags);
34+
$this->assertSame('Hello', $tags[0]->getName());
35+
}
36+
37+
/**
38+
* Ensures that empty tags and errors in the number of commas are
39+
* dealed correctly.
40+
*/
41+
public function testCreateTheRightAmountOfTagsWithTooManyCommas()
42+
{
43+
$transformer = $this->getMockedTransformer();
44+
45+
$this->assertCount(3, $transformer->reverseTransform('Hello, Demo,, How'));
46+
$this->assertCount(3, $transformer->reverseTransform('Hello, Demo, How,'));
47+
}
48+
49+
/**
50+
* Ensures that leading/trailing spaces are ignored for tag names.
51+
*/
52+
public function testTrimNames()
53+
{
54+
$tags = $this->getMockedTransformer()->reverseTransform(' Hello ');
55+
56+
$this->assertSame('Hello', $tags[0]->getName());
57+
}
58+
59+
/**
60+
* Ensures that duplicated tag names are ignored.
61+
*/
62+
public function testDuplicateNames()
63+
{
64+
$tags = $this->getMockedTransformer()->reverseTransform('Hello, Hello, Hello');
65+
66+
$this->assertCount(1, $tags);
67+
}
68+
69+
/**
70+
* Ensures that the transformer uses tags already persisted in the database.
71+
*/
72+
public function testUsesAlreadyDefinedTags()
73+
{
74+
$persistedTags = [
75+
$this->createTag('Hello'),
76+
$this->createTag('World'),
77+
];
78+
$tags = $this->getMockedTransformer($persistedTags)->reverseTransform('Hello, World, How, Are, You');
79+
80+
$this->assertCount(5, $tags);
81+
$this->assertSame($persistedTags[0], $tags[0]);
82+
$this->assertSame($persistedTags[1], $tags[1]);
83+
}
84+
85+
/**
86+
* Ensures that the transformation from Tag instances to a simple string
87+
* works as expected.
88+
*/
89+
public function testTransform()
90+
{
91+
$persistedTags = [
92+
$this->createTag('Hello'),
93+
$this->createTag('World'),
94+
];
95+
$transformed = $this->getMockedTransformer()->transform($persistedTags);
96+
97+
$this->assertSame('Hello,World', $transformed);
98+
}
99+
100+
/**
101+
* This helper method mocks the real TagArrayToStringTransformer class to
102+
* simplify the tests. See https://phpunit.de/manual/current/en/test-doubles.html.
103+
*
104+
* @param array $findByReturnValues The values returned when calling to the findBy() method
105+
*
106+
* @return TagArrayToStringTransformer
107+
*/
108+
private function getMockedTransformer($findByReturnValues = [])
109+
{
110+
$tagRepository = $this->getMockBuilder(EntityRepository::class)
111+
->disableOriginalConstructor()
112+
->getMock();
113+
$tagRepository->expects($this->any())
114+
->method('findBy')
115+
->will($this->returnValue($findByReturnValues));
116+
117+
$entityManager = $this
118+
->getMockBuilder(ObjectManager::class)
119+
->disableOriginalConstructor()
120+
->getMock();
121+
$entityManager->expects($this->any())
122+
->method('getRepository')
123+
->will($this->returnValue($tagRepository));
124+
125+
return new TagArrayToStringTransformer($entityManager);
126+
}
127+
128+
/**
129+
* This helper method creates a Tag instance for the given tag name.
130+
*
131+
* @param string $name
132+
*
133+
* @return Tag
134+
*/
135+
private function createTag($name)
136+
{
137+
$tag = new Tag();
138+
$tag->setName($name);
139+
140+
return $tag;
141+
}
142+
}

0 commit comments

Comments
 (0)