From a9d5a3e33bdd8af15fca34b16e41a7b039d440fa Mon Sep 17 00:00:00 2001 From: "Paul M. Jones" Date: Thu, 1 Apr 2021 09:17:28 -0500 Subject: [PATCH] add test for #16 --- src/Relationship/OneToMany.php | 2 ++ tests/Relationship/ManyToManyTest.php | 33 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Relationship/OneToMany.php b/src/Relationship/OneToMany.php index 272c3be..4ee7304 100644 --- a/src/Relationship/OneToMany.php +++ b/src/Relationship/OneToMany.php @@ -61,5 +61,7 @@ public function persistForeign( foreach ($foreignRecordSet as $foreignRecord) { $foreignMapper->persist($foreignRecord, $tracker); } + + $foreignRecordSet->detachDeleted(); } } diff --git a/tests/Relationship/ManyToManyTest.php b/tests/Relationship/ManyToManyTest.php index 7cb6bbd..cb2d58c 100644 --- a/tests/Relationship/ManyToManyTest.php +++ b/tests/Relationship/ManyToManyTest.php @@ -25,6 +25,10 @@ public function testForeignPersist_basic() ->select() ->fetchRecordSet(); + // make sure there are three taggings and tags + $this->assertCount(3, $before->tags); + $this->assertCount(3, $before->taggings); + // remove tag 'foo' $before->tags->detachOneBy(['name' => 'foo']); @@ -35,6 +39,10 @@ public function testForeignPersist_basic() // persist it $threadMapper->persist($before); + // make sure the taggings are detached properly + $this->assertCount(3, $before->tags); + $this->assertCount(3, $before->taggings); + // re-get the thread, with new taggings $after = $threadMapper->fetchRecord(1, [ 'tags' @@ -132,6 +140,31 @@ public function testForeignPersist_allNew() $this->assertEquals($expect, $actual); } + public function testForeignPersist_detachAll() + { + $threadMapper = $this->mapperLocator->get(Thread::CLASS); + + $before = $threadMapper->fetchRecord(1, [ + 'tags' + ]); + + $this->assertCount(3, $before->taggings); + $this->assertCount(3, $before->tags); + + $before->tags->detachAll(); + $threadMapper->persist($before); + + $this->assertCount(0, $before->tags); + $this->assertCount(0, $before->taggings); + + $after = $threadMapper->fetchRecord(1, [ + 'tags' + ]); + + $this->assertCount(0, $after->taggings); + $this->assertCount(0, $after->tags); + } + public function testJoinSelect() { $actual = $this->mapperLocator->get(Thread::CLASS)