24
24
class TagArrayToStringTransformerTest extends \PHPUnit \Framework \TestCase
25
25
{
26
26
/**
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.
68
28
*/
69
29
public function testCreateTheRightAmountOfTags ()
70
30
{
71
31
$ tags = $ this ->getMockedTransformer ()->reverseTransform ('Hello, Demo, How ' );
32
+
72
33
$ this ->assertCount (3 , $ tags );
73
34
$ this ->assertSame ('Hello ' , $ tags [0 ]->getName ());
74
35
}
75
36
76
37
/**
77
- * Too many commas.
38
+ * Ensures that empty tags and errors in the number of commas are
39
+ * dealed correctly.
78
40
*/
79
41
public function testCreateTheRightAmountOfTagsWithTooManyCommas ()
80
42
{
81
43
$ transformer = $ this ->getMockedTransformer ();
44
+
82
45
$ this ->assertCount (3 , $ transformer ->reverseTransform ('Hello, Demo,, How ' ));
83
46
$ this ->assertCount (3 , $ transformer ->reverseTransform ('Hello, Demo, How, ' ));
84
47
}
85
48
86
49
/**
87
- * Spaces at the end (and beginning) of a world shouldn't matter .
50
+ * Ensures that leading/trailing spaces are ignored for tag names .
88
51
*/
89
52
public function testTrimNames ()
90
53
{
91
54
$ tags = $ this ->getMockedTransformer ()->reverseTransform (' Hello ' );
55
+
92
56
$ this ->assertSame ('Hello ' , $ tags [0 ]->getName ());
93
57
}
94
58
95
59
/**
96
- * Duplicate tags shouldn't create new entities .
60
+ * Ensures that duplicated tag names are ignored .
97
61
*/
98
62
public function testDuplicateNames ()
99
63
{
100
64
$ tags = $ this ->getMockedTransformer ()->reverseTransform ('Hello, Hello, Hello ' );
65
+
101
66
$ this ->assertCount (1 , $ tags );
102
67
}
103
68
104
69
/**
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 .
106
71
*/
107
72
public function testUsesAlreadyDefinedTags ()
108
73
{
@@ -111,13 +76,15 @@ public function testUsesAlreadyDefinedTags()
111
76
$ this ->createTag ('World ' ),
112
77
];
113
78
$ tags = $ this ->getMockedTransformer ($ persistedTags )->reverseTransform ('Hello, World, How, Are, You ' );
79
+
114
80
$ this ->assertCount (5 , $ tags );
115
81
$ this ->assertSame ($ persistedTags [0 ], $ tags [0 ]);
116
82
$ this ->assertSame ($ persistedTags [1 ], $ tags [1 ]);
117
83
}
118
84
119
85
/**
120
- * Tags should be transformed into a string.
86
+ * Ensures that the transformation from Tag instances to a simple string
87
+ * works as expected.
121
88
*/
122
89
public function testTransform ()
123
90
{
@@ -126,6 +93,50 @@ public function testTransform()
126
93
$ this ->createTag ('World ' ),
127
94
];
128
95
$ transformed = $ this ->getMockedTransformer ()->transform ($ persistedTags );
96
+
129
97
$ this ->assertSame ('Hello,World ' , $ transformed );
130
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
+ }
131
142
}
0 commit comments