Skip to content

Commit 15a8d39

Browse files
Minor changes
* Moved the helper methods at the end of the file and made them `private` * Minor changes to match the code style of the other tests
1 parent 0f14c57 commit 15a8d39

File tree

1 file changed

+57
-46
lines changed

1 file changed

+57
-46
lines changed

tests/AppBundle/Form/DataTransformer/TagArrayToStringTransformerTest.php

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,85 +24,50 @@
2424
class TagArrayToStringTransformerTest extends \PHPUnit\Framework\TestCase
2525
{
2626
/**
27-
* Get a mocked instance of the TagArrayToStringTransformer.
28-
*
29-
* @return TagArrayToStringTransformer
30-
*/
31-
public function getMockedTransformer($findByReturn = [])
32-
{
33-
$tagRepository = $this->getMockBuilder(EntityRepository::class)
34-
->disableOriginalConstructor()
35-
->getMock();
36-
$tagRepository->expects($this->any())
37-
->method('findBy')
38-
->will($this->returnValue($findByReturn));
39-
40-
$entityManager = $this
41-
->getMockBuilder(ObjectManager::class)
42-
->disableOriginalConstructor()
43-
->getMock();
44-
$entityManager->expects($this->any())
45-
->method('getRepository')
46-
->will($this->returnValue($tagRepository));
47-
48-
return new TagArrayToStringTransformer($entityManager);
49-
}
50-
51-
/**
52-
* Creates a new TagEntity instance.
53-
*
54-
* @param $name
55-
*
56-
* @return Tag
57-
*/
58-
public function createTag($name)
59-
{
60-
$tag = new Tag();
61-
$tag->setName($name);
62-
63-
return $tag;
64-
}
65-
66-
/**
67-
* Ensures tags are created correctly.
27+
* Ensures that tags are created correctly.
6828
*/
6929
public function testCreateTheRightAmountOfTags()
7030
{
7131
$tags = $this->getMockedTransformer()->reverseTransform('Hello, Demo, How');
32+
7233
$this->assertCount(3, $tags);
7334
$this->assertSame('Hello', $tags[0]->getName());
7435
}
7536

7637
/**
77-
* Too many commas.
38+
* Ensures that empty tags and errors in the number of commas are
39+
* dealed correctly.
7840
*/
7941
public function testCreateTheRightAmountOfTagsWithTooManyCommas()
8042
{
8143
$transformer = $this->getMockedTransformer();
44+
8245
$this->assertCount(3, $transformer->reverseTransform('Hello, Demo,, How'));
8346
$this->assertCount(3, $transformer->reverseTransform('Hello, Demo, How,'));
8447
}
8548

8649
/**
87-
* Spaces at the end (and beginning) of a world shouldn't matter.
50+
* Ensures that leading/trailing spaces are ignored for tag names.
8851
*/
8952
public function testTrimNames()
9053
{
9154
$tags = $this->getMockedTransformer()->reverseTransform(' Hello ');
55+
9256
$this->assertSame('Hello', $tags[0]->getName());
9357
}
9458

9559
/**
96-
* Duplicate tags shouldn't create new entities.
60+
* Ensures that duplicated tag names are ignored.
9761
*/
9862
public function testDuplicateNames()
9963
{
10064
$tags = $this->getMockedTransformer()->reverseTransform('Hello, Hello, Hello');
65+
10166
$this->assertCount(1, $tags);
10267
}
10368

10469
/**
105-
* This test ensure that the transformer uses tag already persisted in the Database.
70+
* Ensures that the transformer uses tags already persisted in the database.
10671
*/
10772
public function testUsesAlreadyDefinedTags()
10873
{
@@ -111,13 +76,15 @@ public function testUsesAlreadyDefinedTags()
11176
$this->createTag('World'),
11277
];
11378
$tags = $this->getMockedTransformer($persistedTags)->reverseTransform('Hello, World, How, Are, You');
79+
11480
$this->assertCount(5, $tags);
11581
$this->assertSame($persistedTags[0], $tags[0]);
11682
$this->assertSame($persistedTags[1], $tags[1]);
11783
}
11884

11985
/**
120-
* Tags should be transformed into a string.
86+
* Ensures that the transformation from Tag instances to a simple string
87+
* works as expected.
12188
*/
12289
public function testTransform()
12390
{
@@ -126,6 +93,50 @@ public function testTransform()
12693
$this->createTag('World'),
12794
];
12895
$transformed = $this->getMockedTransformer()->transform($persistedTags);
96+
12997
$this->assertSame('Hello,World', $transformed);
13098
}
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+
}
131142
}

0 commit comments

Comments
 (0)