Skip to content

Commit

Permalink
Merge branch 'fix/#6110-collection-clear-should-also-clear-keys-2.5' …
Browse files Browse the repository at this point in the history
…into 2.5

Close #6110
  • Loading branch information
Ocramius committed Nov 26, 2016
2 parents 9b36947 + 1486c8f commit 4d69978
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/Doctrine/ORM/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ public function isEmpty()
public function clear()
{
if ($this->initialized && $this->isEmpty()) {
$this->collection->clear();

return;
}

Expand Down
41 changes: 40 additions & 1 deletion tests/Doctrine/Tests/ORM/PersistentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,4 +82,44 @@ public function testNextInitializesCollection()
$this->collection->next();
$this->assertTrue($this->collection->isInitialized());
}

/**
* @group 6110
*/
public function testRemovingElementsAlsoRemovesKeys()
{
$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()));
}

/**
* @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();

$this->collection->add('dummy');
$this->collection->removeElement('dummy');
$this->collection->clear();
$this->collection->add('dummy');
$this->assertEquals([0], array_keys($this->collection->toArray()));
}
}

0 comments on commit 4d69978

Please sign in to comment.