From 3dadfa49d5fb4e2e3866eaa535d63a6ac44d8e4e Mon Sep 17 00:00:00 2001 From: Steevan BARBOYON Date: Mon, 31 Oct 2016 12:03:19 +0100 Subject: [PATCH 1/2] Clear $this->collection even when empty, to reset indexes --- lib/Doctrine/ORM/PersistentCollection.php | 2 ++ .../Tests/ORM/PersistentCollectionTest.php | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/Doctrine/ORM/PersistentCollection.php b/lib/Doctrine/ORM/PersistentCollection.php index 83eab8cf1ab..c2e11dfbe30 100644 --- a/lib/Doctrine/ORM/PersistentCollection.php +++ b/lib/Doctrine/ORM/PersistentCollection.php @@ -536,6 +536,8 @@ public function isEmpty() public function clear() { if ($this->initialized && $this->isEmpty()) { + $this->collection->clear(); + return; } diff --git a/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php b/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php index e77f898d139..757483a97a4 100644 --- a/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php @@ -83,4 +83,29 @@ public function testNextInitializesCollection() $this->collection->next(); $this->assertTrue($this->collection->isInitialized()); } + + /** + * Test that PersistentCollection::clear() clear elements, and reset keys + */ + public function testClear() + { + $this->setUpPersistentCollection(); + + $this->collection->add('dummy'); + $this->assertEquals([0], array_keys($this->collection->toArray())); + + $this->collection->removeElement('dummy'); + $this->assertEquals([], array_keys($this->collection->toArray())); + + $this->collection->add('dummy'); + $this->collection->clear(); + $this->assertEquals([], array_keys($this->collection->toArray())); + + // test fix clear doesn't reset collection keys when collection is empty + $this->collection->add('dummy'); + $this->collection->removeElement('dummy'); + $this->collection->clear(); + $this->collection->add('dummy'); + $this->assertEquals([0], array_keys($this->collection->toArray())); + } } From 1486c8f8e22c774605ab9c142da579556f0598e5 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 26 Nov 2016 06:02:16 +0100 Subject: [PATCH 2/2] split test into multiple sub-scenarios involving `PersistentCollection` key checking #6110 --- .../Tests/ORM/PersistentCollectionTest.php | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php b/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php index 757483a97a4..190fe2ef1cf 100644 --- a/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php @@ -3,7 +3,6 @@ namespace Doctrine\Tests\ORM; use Doctrine\Common\Collections\ArrayCollection; -use Doctrine\Common\Collections\Collection; use Doctrine\ORM\PersistentCollection; use Doctrine\Tests\Mocks\ConnectionMock; use Doctrine\Tests\Mocks\DriverMock; @@ -85,9 +84,9 @@ public function testNextInitializesCollection() } /** - * Test that PersistentCollection::clear() clear elements, and reset keys + * @group 6110 */ - public function testClear() + public function testRemovingElementsAlsoRemovesKeys() { $this->setUpPersistentCollection(); @@ -96,12 +95,27 @@ public function testClear() $this->collection->removeElement('dummy'); $this->assertEquals([], array_keys($this->collection->toArray())); + } + + /** + * @group 6110 + */ + public function testClearWillAlsoClearKeys() + { + $this->setUpPersistentCollection(); $this->collection->add('dummy'); $this->collection->clear(); $this->assertEquals([], array_keys($this->collection->toArray())); + } + + /** + * @group 6110 + */ + public function testClearWillAlsoResetKeyPositions() + { + $this->setUpPersistentCollection(); - // test fix clear doesn't reset collection keys when collection is empty $this->collection->add('dummy'); $this->collection->removeElement('dummy'); $this->collection->clear();