Clear $this->collection even when empty, to reset indexes
This commit is contained in:
parent
b06dcb89b3
commit
fa09a95023
@ -542,6 +542,8 @@ final class PersistentCollection extends AbstractLazyCollection implements Selec
|
|||||||
public function clear()
|
public function clear()
|
||||||
{
|
{
|
||||||
if ($this->initialized && $this->isEmpty()) {
|
if ($this->initialized && $this->isEmpty()) {
|
||||||
|
$this->collection->clear();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,4 +107,29 @@ class PersistentCollectionTest extends OrmTestCase
|
|||||||
$this->assertSame("dummy", $this->collection->get(2));
|
$this->assertSame("dummy", $this->collection->get(2));
|
||||||
$this->assertSame(null, $this->collection->get(3));
|
$this->assertSame(null, $this->collection->get(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user