Skip to content

Commit a8de079

Browse files
committed
New tests, TagArrayToStringTransformer
1 parent ed7ce1a commit a8de079

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

src/AppBundle/Form/DataTransformer/TagArrayToStringTransformer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public function reverseTransform($string)
5454
return [];
5555
}
5656

57-
$names = explode(',', $string);
57+
$names = array_unique(array_map(function($name) {
58+
return trim($name);
59+
}, explode(',', $string)));
60+
5861

5962
// Get the current tags and find the new ones that should be created.
6063
$tags = $this->manager->getRepository(Tag::class)->findBy([
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace Tests\AppBundle\Form\DataTransformer;
4+
5+
use AppBundle\Entity\Tag;
6+
use AppBundle\Form\DataTransformer\TagArrayToStringTransformer;
7+
use Doctrine\Common\Persistence\ObjectManager;
8+
use Doctrine\ORM\EntityRepository;
9+
10+
class TagArrayToStringTransformerTest extends \PHPUnit\Framework\TestCase
11+
{
12+
13+
/**
14+
* Get a mocked instance of the TagArrayToStringTransformer
15+
* @return TagArrayToStringTransformer
16+
*/
17+
public function getMockedTransformer($findByReturn = [])
18+
{
19+
$tagRepository = $this->getMockBuilder(EntityRepository::class)
20+
->disableOriginalConstructor()
21+
->getMock();
22+
$tagRepository->expects($this->any())
23+
->method('findBy')
24+
->will($this->returnValue($findByReturn));
25+
26+
$entityManager = $this
27+
->getMockBuilder(ObjectManager::class)
28+
->disableOriginalConstructor()
29+
->getMock();
30+
$entityManager->expects($this->any())
31+
->method('getRepository')
32+
->will($this->returnValue($tagRepository));
33+
34+
return new TagArrayToStringTransformer($entityManager);
35+
}
36+
37+
/**
38+
* Creates a new TagEntity instance
39+
* @param $name
40+
* @return Tag
41+
*/
42+
public function createTag($name)
43+
{
44+
$tag = new Tag();
45+
$tag->setName($name);
46+
return $tag;
47+
}
48+
49+
/**
50+
* Ensures tags are created correctly
51+
*/
52+
public function testCreateTheRightAmountOfTags()
53+
{
54+
$tags = $this->getMockedTransformer()->reverseTransform('Hello, Demo, How');
55+
$this->assertCount(3, $tags);
56+
$this->assertEquals('Hello', $tags[0]->getName());
57+
}
58+
59+
/**
60+
* Spaces at the end (and begining) of a world shouldn't matter
61+
*/
62+
public function testTrimNames()
63+
{
64+
$tags = $this->getMockedTransformer()->reverseTransform(' Hello ');
65+
$this->assertEquals('Hello', $tags[0]->getName());
66+
}
67+
68+
/**
69+
* Duplicate tags shouldn't create new entities
70+
*/
71+
public function testDuplicateNames()
72+
{
73+
$tags = $this->getMockedTransformer()->reverseTransform('Hello, Hello, Hello');
74+
$this->assertCount(1, $tags);
75+
}
76+
77+
/**
78+
* This test ensure that the transformer uses tag already persisted in the Database
79+
*/
80+
public function testUsesAlreadyDefinedTags()
81+
{
82+
$persisted_tags = [
83+
$this->createTag('Hello'),
84+
$this->createTag('World')
85+
];
86+
$tags = $this->getMockedTransformer($persisted_tags)->reverseTransform('Hello, World, How, Are, You');
87+
$this->assertCount(5, $tags);
88+
$this->assertEquals($persisted_tags[0], $tags[0]);
89+
$this->assertEquals($persisted_tags[1], $tags[1]);
90+
}
91+
92+
/**
93+
* Tags should be transformed into a string
94+
*/
95+
public function testTransform () {
96+
$persisted_tags = [
97+
$this->createTag('Hello'),
98+
$this->createTag('World')
99+
];
100+
$transformed = $this->getMockedTransformer()->transform($persisted_tags);
101+
$this->assertEquals('Hello,World', $transformed);
102+
}
103+
104+
}

0 commit comments

Comments
 (0)