13
13
14
14
namespace Sulu \Bundle \ContentBundle \Tests \Functional \Content \Infrastructure \Doctrine ;
15
15
16
+ use Sulu \Bundle \ContactBundle \Entity \Contact ;
16
17
use Sulu \Bundle \ContentBundle \Content \Domain \Factory \TagFactoryInterface ;
17
18
use Sulu \Bundle \TagBundle \Tag \TagInterface ;
18
19
use Sulu \Bundle \TagBundle \Tag \TagRepositoryInterface ;
19
20
use Sulu \Bundle \TestBundle \Testing \SuluTestCase ;
20
21
21
22
class TagFactoryTest extends SuluTestCase
22
23
{
24
+ /**
25
+ * @var TagFactoryInterface
26
+ */
27
+ private $ tagFactory ;
28
+
23
29
protected function setUp (): void
24
30
{
25
31
self ::bootKernel ();
26
32
self ::purgeDatabase ();
27
- }
28
-
29
- /**
30
- * @param string[] $existTagNames
31
- */
32
- protected function createTagFactory (array $ existTagNames = []): TagFactoryInterface
33
- {
34
- /** @var TagRepositoryInterface $tagRepository */
35
- $ tagRepository = self ::$ container ->get ('sulu.repository.tag ' );
36
-
37
- foreach ($ existTagNames as $ existTagName ) {
38
- $ existTag = $ tagRepository ->createNew ();
39
- $ existTag ->setName ($ existTagName );
40
- self ::getEntityManager ()->persist ($ existTag );
41
- }
42
-
43
- if (\count ($ existTagNames )) {
44
- self ::getEntityManager ()->flush ();
45
- self ::getEntityManager ()->clear ();
46
- }
47
33
48
- return self ::$ container ->get ('sulu_content.tag_factory ' );
34
+ $ this -> tagFactory = self ::$ container ->get ('sulu_content.tag_factory ' );
49
35
}
50
36
51
37
/**
@@ -56,19 +42,49 @@ protected function createTagFactory(array $existTagNames = []): TagFactoryInterf
56
42
*/
57
43
public function testCreate (array $ tagNames , array $ existTags ): void
58
44
{
59
- $ tagFactory = $ this ->createTagFactory ($ existTags );
45
+ $ this ->createTags ($ existTags );
46
+
47
+ $ tags = $ this ->tagFactory ->create ($ tagNames );
60
48
61
49
$ this ->assertSame (
62
50
$ tagNames ,
63
51
array_map (
64
52
function (TagInterface $ tag ) {
65
53
return $ tag ->getName ();
66
54
},
67
- $ tagFactory -> create ( $ tagNames )
55
+ $ tags
68
56
)
69
57
);
70
58
}
71
59
60
+ public function testCreateSameTagTwice (): void
61
+ {
62
+ $ tags1 = $ this ->tagFactory ->create (['Tag 1 ' ]);
63
+ $ tags2 = $ this ->tagFactory ->create (['Tag 1 ' ]);
64
+
65
+ $ this ->assertSame ($ tags1 , $ tags2 );
66
+
67
+ $ this ->getEntityManager ()->flush ();
68
+ }
69
+
70
+ public function testCreateSameTagTwiceWithOtherEntityInUnitOfWork (): void
71
+ {
72
+ $ this ->getEntityManager ()->persist ($ this ->createOtherEntity ());
73
+
74
+ /** @var TagRepositoryInterface $tagRepository */
75
+ $ tagRepository = self ::$ container ->get ('sulu.repository.tag ' );
76
+ $ tag = $ tagRepository ->createNew ();
77
+ $ tag ->setName ('Other Tag ' );
78
+ $ this ->getEntityManager ()->persist ($ tag );
79
+
80
+ $ tags1 = $ this ->tagFactory ->create (['Tag 1 ' ]);
81
+ $ tags2 = $ this ->tagFactory ->create (['Tag 1 ' ]);
82
+
83
+ $ this ->assertSame ($ tags1 , $ tags2 );
84
+
85
+ $ this ->getEntityManager ()->flush ();
86
+ }
87
+
72
88
/**
73
89
* @return \Generator<mixed[]>
74
90
*/
@@ -100,6 +116,7 @@ public function dataProvider(): \Generator
100
116
],
101
117
[
102
118
'Exist Tag 1 ' ,
119
+ 'Other Exist 3 ' ,
103
120
],
104
121
];
105
122
@@ -114,4 +131,33 @@ public function dataProvider(): \Generator
114
131
],
115
132
];
116
133
}
134
+
135
+ /**
136
+ * @param string[] $existTagNames
137
+ */
138
+ private function createTags (array $ existTagNames = []): void
139
+ {
140
+ /** @var TagRepositoryInterface $tagRepository */
141
+ $ tagRepository = self ::$ container ->get ('sulu.repository.tag ' );
142
+
143
+ foreach ($ existTagNames as $ existTagName ) {
144
+ $ existTag = $ tagRepository ->createNew ();
145
+ $ existTag ->setName ($ existTagName );
146
+ self ::getEntityManager ()->persist ($ existTag );
147
+ }
148
+
149
+ if (\count ($ existTagNames )) {
150
+ self ::getEntityManager ()->flush ();
151
+ self ::getEntityManager ()->clear ();
152
+ }
153
+ }
154
+
155
+ private function createOtherEntity (): object
156
+ {
157
+ $ contact = new Contact ();
158
+ $ contact ->setFirstName ('Dummy ' );
159
+ $ contact ->setLastName ('Entity ' );
160
+
161
+ return $ contact ;
162
+ }
117
163
}
0 commit comments