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
33
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
-
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,43 @@ 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
+ $ tags1 = $ this ->tagFactory ->create (['Tag 1 ' ]);
75
+ $ tags2 = $ this ->tagFactory ->create (['Tag 1 ' ]);
76
+
77
+ $ this ->assertSame ($ tags1 , $ tags2 );
78
+
79
+ $ this ->getEntityManager ()->flush ();
80
+ }
81
+
72
82
/**
73
83
* @return \Generator<mixed[]>
74
84
*/
@@ -114,4 +124,33 @@ public function dataProvider(): \Generator
114
124
],
115
125
];
116
126
}
127
+
128
+ /**
129
+ * @param string[] $existTagNames
130
+ */
131
+ private function createTags (array $ existTagNames = []): void
132
+ {
133
+ /** @var TagRepositoryInterface $tagRepository */
134
+ $ tagRepository = self ::$ container ->get ('sulu.repository.tag ' );
135
+
136
+ foreach ($ existTagNames as $ existTagName ) {
137
+ $ existTag = $ tagRepository ->createNew ();
138
+ $ existTag ->setName ($ existTagName );
139
+ self ::getEntityManager ()->persist ($ existTag );
140
+ }
141
+
142
+ if (\count ($ existTagNames )) {
143
+ self ::getEntityManager ()->flush ();
144
+ self ::getEntityManager ()->clear ();
145
+ }
146
+ }
147
+
148
+ private function createOtherEntity (): object
149
+ {
150
+ $ contact = new Contact ();
151
+ $ contact ->setFirstName ('Dummy ' );
152
+ $ contact ->setLastName ('Entity ' );
153
+
154
+ return $ contact ;
155
+ }
117
156
}
0 commit comments