1
0
mirror of synced 2025-02-02 13:31:45 +03:00

Clear $this->collection even when empty, to reset indexes

This commit is contained in:
Steevan BARBOYON 2016-10-31 12:03:19 +01:00 committed by Marco Pivetta
parent 9b36947a48
commit 3dadfa49d5
2 changed files with 27 additions and 0 deletions

View File

@ -536,6 +536,8 @@ final class PersistentCollection extends AbstractLazyCollection implements Selec
public function clear()
{
if ($this->initialized && $this->isEmpty()) {
$this->collection->clear();
return;
}

View File

@ -83,4 +83,29 @@ class PersistentCollectionTest extends OrmTestCase
$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()));
}
}