1818use Doctrine \ODM \MongoDB \Mapping \ClassMetadata as ODMClassMetadata ;
1919use Doctrine \ORM \EntityManagerInterface ;
2020use Doctrine \ORM \Mapping \ClassMetadata as ORMClassMetadata ;
21- use Doctrine \Persistence \Mapping \ ClassMetadata as ClassMetadataInterface ;
21+ use Doctrine \Persistence \ObjectManager ;
2222use InvalidArgumentException ;
23+ use PHPUnit \Framework \Constraint \Callback ;
24+ use PHPUnit \Framework \Constraint \IsInstanceOf ;
25+ use PHPUnit \Framework \MockObject \MockObject ;
2326use PHPUnit \Framework \TestCase ;
2427use RuntimeException ;
2528use Tests \Orbitale \Component \ArrayFixture \Fixtures \CustomNumberOfFlushesFixtureStub ;
2932use Tests \Orbitale \Component \ArrayFixture \Fixtures \PostSelfReferenceFixtureStub ;
3033use Tests \Orbitale \Component \ArrayFixture \Fixtures \PostTitleFixtureStub ;
3134use Tests \Orbitale \Component \ArrayFixture \Fixtures \ToStringPrefixFixtureStub ;
32- use Tests \Orbitale \Component \ArrayFixture \Stubs \DocumentManagerStub ;
33- use Tests \Orbitale \Component \ArrayFixture \Stubs \EntityManagerStub ;
34- use Tests \Orbitale \Component \ArrayFixture \Stubs \ObjectManagerStub ;
3535use Tests \Orbitale \Component \ArrayFixture \Stubs \PostStub ;
3636use Tests \Orbitale \Component \ArrayFixture \Stubs \ReferenceRepositoryStub ;
3737
@@ -41,34 +41,51 @@ public function test post title fixture(): void
4141 {
4242 $ manager = $ this ->getObjectManager ();
4343
44- (new PostTitleFixtureStub ())->load ($ manager );
44+ $ manager ->expects ($ this ->once ())->method ('flush ' );
45+ $ manager ->expects ($ this ->once ())->method ('persist ' )->with (
46+ new Callback (function ($ stub ) {
47+ return is_object ($ stub ) && $ stub instanceof PostStub && $ stub ->getTitle () === 'Default title ' ;
48+ }),
49+ );
4550
46- self :: assertSame ( 1 , $ manager -> getFlushed ());
51+ //
4752
48- $ entities = $ manager ->getPersisted ();
49- self ::assertCount (1 , $ entities );
50- self ::assertInstanceOf (PostStub::class, $ entities [0 ]);
51- self ::assertSame ('Default title ' , $ entities [0 ]->getTitle ());
53+ (new PostTitleFixtureStub ())->load ($ manager );
5254 }
5355
5456 public function test post self reference fixture (): void
5557 {
5658 $ manager = $ this ->getObjectManager ();
57- $ refs = new ReferenceRepositoryStub ($ manager );
59+
60+ $ titles = [
61+ 'Default title ' ,
62+ 'Second title ' ,
63+ ];
64+ /** @var null|PostStub $lastStub */
65+ $ lastStub = null ;
66+
67+ $ manager ->expects ($ this ->once ())->method ('flush ' );
68+ $ manager ->expects ($ this ->exactly (2 ))->method ('persist ' )->with (
69+ new Callback (function ($ stub ) use (&$ titles , &$ lastStub ) {
70+ if (!count ($ titles )) {
71+ self ::fail ('Persist was called too many times ' );
72+ }
73+ $ title = array_shift ($ titles );
74+ $ result = is_object ($ stub )
75+ && $ stub instanceof PostStub
76+ && $ stub ->getTitle () === $ title
77+ && ($ lastStub ? $ stub ->getParent () === $ lastStub : true )
78+ ;
79+ $ lastStub = $ stub ;
80+ return $ result ;
81+ }),
82+ );
83+
84+ //
5885
5986 $ fixture = new PostSelfReferenceFixtureStub ();
60- $ fixture ->setReferenceRepository ($ refs );
87+ $ fixture ->setReferenceRepository (new ReferenceRepositoryStub ( $ manager ) );
6188 $ fixture ->load ($ manager );
62-
63- self ::assertSame (1 , $ manager ->getFlushed ());
64-
65- $ entities = $ manager ->getPersisted ();
66- self ::assertCount (2 , $ entities );
67- self ::assertInstanceOf (PostStub::class, $ entities [0 ]);
68- self ::assertInstanceOf (PostStub::class, $ entities [1 ]);
69- self ::assertSame ('Default title ' , $ entities [0 ]->getTitle ());
70- self ::assertSame ('Second title ' , $ entities [1 ]->getTitle ());
71- self ::assertSame ($ entities [0 ], $ entities [1 ]->getParent ());
7289 }
7390
7491 public function test inexistent property (): void
@@ -78,6 +95,8 @@ public function test inexistent property(): void
7895 $ this ->expectException (InvalidArgumentException::class);
7996 $ this ->expectExceptionMessage ('Cannot set property "does_not_exist" to "Tests\Orbitale\Component\ArrayFixture\Stubs\PostStub" object since this property does not exist. ' );
8097
98+ //
99+
81100 (new InexistentPropertyFixtureStub ())->load ($ manager );
82101 }
83102
@@ -86,19 +105,31 @@ public function test toString for prefix(): void
86105 $ manager = $ this ->getObjectManager ();
87106 $ refsRepo = new ReferenceRepositoryStub ($ manager );
88107
108+ /** @var PostStub $persistedStub */
109+ $ persistedStub = null ;
110+
111+ $ manager ->expects ($ this ->once ())->method ('flush ' );
112+ $ manager ->expects ($ this ->once ())->method ('persist ' )->with (
113+ new Callback (function ($ stub ) use (&$ persistedStub ) {
114+ $ result = is_object ($ stub ) && $ stub instanceof PostStub && $ stub ->getTitle () === 'Default title ' ;
115+ if ($ result ) {
116+ $ persistedStub = $ stub ;
117+ }
118+ return $ result ;
119+ }),
120+ );
121+
122+ //
123+
89124 $ fixture = new ToStringPrefixFixtureStub ();
90125 $ fixture ->setReferenceRepository ($ refsRepo );
91126 $ fixture ->load ($ manager );
92127
93- self :: assertSame ( 1 , $ manager -> getFlushed ());
128+ //
94129
95- $ entities = $ manager ->getPersisted ();
96- self ::assertCount (1 , $ entities );
97- self ::assertInstanceOf (PostStub::class, $ entities [0 ]);
98- self ::assertSame ('Default title ' , $ entities [0 ]->getTitle ());
99130 $ refs = $ refsRepo ->getReferences ();
100131 self ::assertArrayHasKey ('post-Default title ' , $ refs );
101- self ::assertSame ($ refs ['post-Default title ' ], $ entities [ 0 ] );
132+ self ::assertSame ($ refs ['post-Default title ' ], $ persistedStub );
102133 }
103134
104135 public function test inexistent prefix method (): void
@@ -108,68 +139,135 @@ public function test inexistent prefix method(): void
108139 $ this ->expectException (RuntimeException::class);
109140 $ this ->expectExceptionMessage ('If you want to specify a reference with prefix "post-", method "getInexistentField" or "__toString()" must exist in the class, or you can override the "getMethodNameForReference" method and add your own. ' );
110141
142+ //
143+
111144 (new InexistentMethodPrefixFixtureStub ())->load ($ manager );
112145 }
113146
114147 public function test custom number of flushes (): void
115148 {
116149 $ manager = $ this ->getObjectManager ();
117150
118- $ fixture = new CustomNumberOfFlushesFixtureStub ();
119- $ fixture ->load ($ manager );
120-
121- self ::assertSame (5 , $ manager ->getFlushed ());
122- self ::assertCount (20 , $ manager ->getPersisted ());
151+ $ manager ->expects ($ this ->exactly (5 ))->method ('flush ' );
152+ $ manager ->expects ($ this ->exactly (20 ))->method ('persist ' );
153+
154+ //
155+
156+ $ manager ->expects ($ this ->exactly (5 ))->method ('flush ' );
157+ $ manager ->expects ($ this ->exactly (20 ))->method ('persist ' )->with (
158+ new Callback (function ($ stub ) use (&$ persistedStubs ) {
159+ $ result = is_object ($ stub ) && $ stub instanceof PostStub;
160+ if ($ result ) {
161+ $ persistedStubs [] = $ stub ;
162+ }
163+ return $ result ;
164+ }),
165+ );
166+
167+ //
168+
169+ (new CustomNumberOfFlushesFixtureStub ())->load ($ manager );
170+ self ::assertCount (20 , $ persistedStubs );
171+ for ($ i = 0 ; $ i < 20 ; $ i ++) {
172+ self ::assertSame ('Title ' .$ i , $ persistedStubs [$ i ]->getTitle ());
173+ self ::assertNull ($ persistedStubs [$ i ]->getParent ());
174+ }
123175 }
124176
125177 public function test entities with ids (): void
126178 {
127179 $ entityManager = $ this ->getEntityManager ();
128180
129- $ fixture = new ObjectsWithIdsStub ();
130- $ fixture ->load ($ entityManager );
131-
132- self ::assertSame (1 , $ entityManager ->getFlushed ());
133- self ::assertCount (2 , $ entityManager ->getPersisted ());
181+ /** @var PostStub[] $persistedStubs */
182+ $ persistedStubs = [];
183+
184+ $ entityManager ->expects ($ this ->once ())->method ('flush ' )->withAnyParameters ();
185+ $ entityManager ->expects ($ this ->exactly (2 ))->method ('persist ' )->with (
186+ new Callback (function ($ stub ) use (&$ persistedStubs ) {
187+ $ result = is_object ($ stub ) && $ stub instanceof PostStub;
188+ if ($ result ) {
189+ $ persistedStubs [] = $ stub ;
190+ }
191+ return $ result ;
192+ }),
193+ );
194+
195+ //
196+
197+ (new ObjectsWithIdsStub ())->load ($ entityManager );
198+ self ::assertSame ('Default title ' , $ persistedStubs [0 ]->getTitle ());
199+ self ::assertNull ($ persistedStubs [0 ]->getParent ());
200+ self ::assertSame ('Another title ' , $ persistedStubs [1 ]->getTitle ());
201+ self ::assertNull ($ persistedStubs [1 ]->getParent ());
134202 }
135203
136204 public function test documents with ids (): void
137205 {
138206 $ documentManager = $ this ->getDocumentManager ();
139207
140- $ fixture = new ObjectsWithIdsStub ();
141- $ fixture ->load ($ documentManager );
208+ /** @var PostStub[] $persistedStubs */
209+ $ persistedStubs = [];
210+
211+ $ documentManager ->expects ($ this ->once ())->method ('flush ' )->withAnyParameters ();
212+ $ documentManager ->expects ($ this ->exactly (2 ))->method ('persist ' )->with (
213+ new Callback (function ($ stub ) use (&$ persistedStubs ) {
214+ $ result = is_object ($ stub ) && $ stub instanceof PostStub;
215+ if ($ result ) {
216+ $ persistedStubs [] = $ stub ;
217+ }
218+ return $ result ;
219+ }),
220+ );
221+
142222
143- self ::assertSame (1 , $ documentManager ->getFlushed ());
144- self ::assertCount (2 , $ documentManager ->getPersisted ());
223+ //
224+
225+ (new ObjectsWithIdsStub ())->load ($ documentManager );
226+ self ::assertSame ('Default title ' , $ persistedStubs [0 ]->getTitle ());
227+ self ::assertNull ($ persistedStubs [0 ]->getParent ());
228+ self ::assertSame ('Another title ' , $ persistedStubs [1 ]->getTitle ());
229+ self ::assertNull ($ persistedStubs [1 ]->getParent ());
145230 }
146231
147- private function getObjectManager (): ObjectManagerStub
232+ /**
233+ * @return ObjectManager&MockObject
234+ */
235+ private function getObjectManager (): ObjectManager
148236 {
149237 $ metadata = $ this ->createMock (ORMClassMetadata::class);
150238 $ metadata ->method ('getIdentifierFieldNames ' )->willReturn (['id ' ]); // Default to "id" since composite are not supported yet.
151239
152- return new ObjectManagerStub ($ metadata );
240+ $ om = $ this ->createMock (ObjectManager::class);
241+ $ om ->method ('getClassMetadata ' )->willReturn ($ metadata );
242+
243+ return $ om ;
153244 }
154245
246+ /**
247+ * @return EntityManagerInterface&MockObject
248+ */
155249 private function getEntityManager (): EntityManagerInterface
156250 {
157251 $ metadata = $ this ->createMock (ORMClassMetadata::class);
158252 $ metadata ->method ('getIdentifierFieldNames ' )->willReturn (['id ' ]); // Default to "id" since composite are not supported yet.
159253
160- $ driver = $ this ->createMock (Driver::class);
254+ $ em = $ this ->createMock (EntityManagerInterface::class);
255+ $ em ->method ('getClassMetadata ' )->willReturn ($ metadata );
161256
162- $ stub = new EntityManagerStub ($ metadata );
163- $ stub ->setDriver ($ driver );
164-
165- return $ stub ;
257+ return $ em ;
166258 }
167259
260+ /**
261+ * @return DocumentManager&MockObject
262+ */
168263 private function getDocumentManager (): DocumentManager
169264 {
170265 $ metadata = $ this ->createMock (ODMClassMetadata::class);
171266 $ metadata ->method ('getIdentifierFieldNames ' )->willReturn (['id ' ]); // Default to "id" since composite are not supported yet.
172267
173- return new DocumentManagerStub ($ metadata );
268+ $ dm = $ this ->createStub (DocumentManager::class);
269+ $ dm ->method ('getClassMetadata ' )->willReturn ($ metadata );
270+
271+ return $ dm ;
174272 }
175273}
0 commit comments