Skip to content

Commit 33642a4

Browse files
committed
Expand tests to add revisions to null out relations
1 parent 1458f03 commit 33642a4

File tree

3 files changed

+71
-14
lines changed

3 files changed

+71
-14
lines changed

tests/Gedmo/Revisionable/Fixture/Entity/Author.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@
2323
class Author implements Revisionable
2424
{
2525
/**
26-
* @ORM\Column(name="name", type="string")
26+
* @ORM\Column(name="name", type="string", nullable=true)
2727
*
2828
* @Gedmo\Versioned
2929
*/
30-
#[ORM\Column(name: 'name', type: Types::STRING)]
30+
#[ORM\Column(name: 'name', type: Types::STRING, nullable: true)]
3131
#[Gedmo\Versioned]
3232
private ?string $name = null;
3333

3434
/**
35-
* @ORM\Column(name="email", type="string")
35+
* @ORM\Column(name="email", type="string", nullable=true)
3636
*
3737
* @Gedmo\Versioned
3838
*/
39-
#[ORM\Column(name: 'email', type: Types::STRING)]
39+
#[ORM\Column(name: 'email', type: Types::STRING, nullable: true)]
4040
#[Gedmo\Versioned]
4141
private ?string $email = null;
4242

tests/Gedmo/Revisionable/RevisionableDocumentTest.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,26 @@ public function testVersionLifecycle(): void
123123

124124
static::assertInstanceOf(RevisionRepository::class, $commentRevisionRepository);
125125

126-
$comment = $commentRepository->findOneBy(['message' => 'm-v5']);
126+
$comment = $commentRepository->findOneBy(['message' => 'm-v7']);
127127

128-
static::assertSame('m-v5', $comment->getMessage());
128+
static::assertInstanceOf(Comment::class, $comment);
129+
static::assertSame('m-v7', $comment->getMessage());
129130
static::assertSame('s-v3', $comment->getSubject());
130131
static::assertSame('2024-06-24 23:30:00', $comment->getWrittenAt()->format('Y-m-d H:i:s'));
131132
static::assertSame('a2-t-v1', $comment->getArticle()->getTitle());
132133
static::assertSame('Jane Doe', $comment->getAuthor()->getName());
133134
static::assertSame('[email protected]', $comment->getAuthor()->getEmail());
134135

135-
// test revert
136+
// test revert single version
137+
$commentRevisionRepository->revert($comment, 6);
138+
139+
static::assertSame('s-v3', $comment->getSubject());
140+
static::assertSame('m-v6', $comment->getMessage());
141+
static::assertSame('2024-06-24 23:30:00', $comment->getWrittenAt()->format('Y-m-d H:i:s'));
142+
static::assertSame('a2-t-v1', $comment->getArticle()->getTitle());
143+
static::assertNull($comment->getAuthor());
144+
145+
// test revert multiple versions
136146
$commentRevisionRepository->revert($comment, 3);
137147

138148
static::assertSame('s-v3', $comment->getSubject());
@@ -148,7 +158,7 @@ public function testVersionLifecycle(): void
148158
// test fetch revisions
149159
$revisions = $commentRevisionRepository->getRevisions($comment);
150160

151-
static::assertCount(6, $revisions);
161+
static::assertCount(8, $revisions);
152162

153163
$latest = array_shift($revisions);
154164

@@ -202,6 +212,7 @@ private function populate(): void
202212
$author->setName('John Doe');
203213
$author->setEmail('[email protected]');
204214

215+
// version 1
205216
$comment = new Comment();
206217
$comment->setArticle($article);
207218
$comment->setAuthor($author);
@@ -213,11 +224,13 @@ private function populate(): void
213224
$this->dm->persist($comment);
214225
$this->dm->flush();
215226

227+
// version 2
216228
$comment->setMessage('m-v2');
217229

218230
$this->dm->persist($comment);
219231
$this->dm->flush();
220232

233+
// version 3
221234
$comment->setSubject('s-v3');
222235

223236
$this->dm->persist($comment);
@@ -231,15 +244,31 @@ private function populate(): void
231244
$author2->setName('Jane Doe');
232245
$author2->setEmail('[email protected]');
233246

247+
// version 4
234248
$comment->setAuthor($author2);
235249
$comment->setArticle($article2);
236250

237251
$this->dm->persist($article2);
238252
$this->dm->persist($comment);
239253
$this->dm->flush();
240254

255+
// version 5
241256
$comment->setMessage('m-v5');
242257

258+
$this->dm->persist($comment);
259+
$this->dm->flush();
260+
261+
// version 6
262+
$comment->setMessage('m-v6');
263+
$comment->setAuthor(null);
264+
265+
$this->dm->persist($comment);
266+
$this->dm->flush();
267+
268+
// version 7
269+
$comment->setMessage('m-v7');
270+
$comment->setAuthor($author2);
271+
243272
$this->dm->persist($comment);
244273
$this->dm->flush();
245274
$this->dm->clear();

tests/Gedmo/Revisionable/RevisionableEntityTest.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,27 @@ public function testVersionLifecycle(): void
200200

201201
static::assertInstanceOf(RevisionRepository::class, $commentRevisionRepository);
202202

203-
$comment = $commentRepository->findOneBy(['message' => 'm-v5']);
203+
$comment = $commentRepository->findOneBy(['message' => 'm-v7']);
204204

205-
static::assertNotNull($comment);
206-
207-
static::assertSame('m-v5', $comment->getMessage());
205+
static::assertInstanceOf(Comment::class, $comment);
206+
static::assertSame('m-v7', $comment->getMessage());
208207
static::assertSame('s-v3', $comment->getSubject());
209208
static::assertSame('2024-06-24 23:30:00', $comment->getWrittenAt()->format('Y-m-d H:i:s'));
210209
static::assertSame('a2-t-v1', $comment->getArticle()->getTitle());
211210
static::assertSame('Jane Doe', $comment->getAuthor()->getName());
212211
static::assertSame('[email protected]', $comment->getAuthor()->getEmail());
213212

214-
// test revert
213+
// test revert single version
214+
$commentRevisionRepository->revert($comment, 6);
215+
216+
static::assertSame('s-v3', $comment->getSubject());
217+
static::assertSame('m-v6', $comment->getMessage());
218+
static::assertSame('2024-06-24 23:30:00', $comment->getWrittenAt()->format('Y-m-d H:i:s'));
219+
static::assertSame('a2-t-v1', $comment->getArticle()->getTitle());
220+
static::assertNull($comment->getAuthor()->getName());
221+
static::assertNull($comment->getAuthor()->getEmail());
222+
223+
// test revert multiple versions
215224
$commentRevisionRepository->revert($comment, 3);
216225

217226
static::assertSame('s-v3', $comment->getSubject());
@@ -227,7 +236,7 @@ public function testVersionLifecycle(): void
227236
// test fetch revisions
228237
$revisions = $commentRevisionRepository->getRevisions($comment);
229238

230-
static::assertCount(6, $revisions);
239+
static::assertCount(8, $revisions);
231240

232241
$latest = array_shift($revisions);
233242

@@ -451,6 +460,7 @@ private function populate(): void
451460
$author->setName('John Doe');
452461
$author->setEmail('[email protected]');
453462

463+
// version 1
454464
$comment = new Comment();
455465
$comment->setArticle($article);
456466
$comment->setAuthor($author);
@@ -462,11 +472,13 @@ private function populate(): void
462472
$this->em->persist($comment);
463473
$this->em->flush();
464474

475+
// version 2
465476
$comment->setMessage('m-v2');
466477

467478
$this->em->persist($comment);
468479
$this->em->flush();
469480

481+
// version 3
470482
$comment->setSubject('s-v3');
471483

472484
$this->em->persist($comment);
@@ -480,15 +492,31 @@ private function populate(): void
480492
$author2->setName('Jane Doe');
481493
$author2->setEmail('[email protected]');
482494

495+
// version 4
483496
$comment->setAuthor($author2);
484497
$comment->setArticle($article2);
485498

486499
$this->em->persist($article2);
487500
$this->em->persist($comment);
488501
$this->em->flush();
489502

503+
// version 5
490504
$comment->setMessage('m-v5');
491505

506+
$this->em->persist($comment);
507+
$this->em->flush();
508+
509+
// version 6
510+
$comment->setMessage('m-v6');
511+
$comment->setAuthor(null);
512+
513+
$this->em->persist($comment);
514+
$this->em->flush();
515+
516+
// version 7
517+
$comment->setMessage('m-v7');
518+
$comment->setAuthor($author2);
519+
492520
$this->em->persist($comment);
493521
$this->em->flush();
494522
$this->em->clear();

0 commit comments

Comments
 (0)