From a42191eecfa20822d784f911f115c4d9b5970fb1 Mon Sep 17 00:00:00 2001 From: Cyril PASCAL Date: Fri, 19 Jul 2019 15:38:13 +0200 Subject: [PATCH] Add functional test for ArrayCollection::clear() bug --- .../ORM/Functional/Ticket/GH7761Test.php | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/GH7761Test.php diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7761Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7761Test.php new file mode 100644 index 00000000000..53215cf0546 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7761Test.php @@ -0,0 +1,91 @@ +setUpEntitySchema([ + GH7761Entity::class, + GH7761ChildEntity::class, + ]); + + $parent = new GH7761Entity(); + $child = new GH7761ChildEntity(); + $parent->children->add($child); + + $this->_em->persist($parent); + $this->_em->persist($child); + $this->_em->flush(); + $this->_em->clear(); + } + + public function testCollectionClearDoesNotClearIfNotPersisted() : void + { + /** @var GH7761Entity $entity */ + $entity = $this->_em->find(GH7761Entity::class, 1); + $entity->children->clear(); + $this->_em->persist(new GH7761Entity()); + $this->_em->flush(); + + $this->_em->clear(); + + $entity = $this->_em->find(GH7761Entity::class, 1); + self::assertCount(1, $entity->children); + + $this->_em->clear(); + } +} + +/** + * @Entity + * @ChangeTrackingPolicy("DEFERRED_EXPLICIT") + */ +class GH7761Entity +{ + /** + * @Id + * @Column(type="integer") + * @GeneratedValue + */ + public $id; + + /** + * @ManyToMany(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\GH7761ChildEntity", cascade={"all"}) + * @JoinTable(name="gh7761_to_child", + * joinColumns={@JoinColumn(name="entity_id")}, + * inverseJoinColumns={@JoinColumn(name="child_id")} + * ) + */ + public $children; + + public function __construct() + { + $this->children = new ArrayCollection(); + } +} + +/** + * @Entity + * @ChangeTrackingPolicy("DEFERRED_EXPLICIT") + */ +class GH7761ChildEntity +{ + /** + * @Id + * @Column(type="integer") + * @GeneratedValue + */ + public $id; +}