1
0
mirror of synced 2025-02-09 00:39:25 +03:00

Merge pull request #1514 from Metabor/patch-2

check if collection is empty without loading it
This commit is contained in:
Marco Pivetta 2015-11-05 14:46:15 -05:00
commit 2fa289edee
2 changed files with 45 additions and 0 deletions

View File

@ -82,6 +82,20 @@ class LazyCriteriaCollection extends AbstractLazyCollection implements Selectabl
return $this->count = $this->entityPersister->count($this->criteria);
}
/**
* check if collection is empty without loading it
*
* @return boolean TRUE if the collection is empty, FALSE otherwise.
*/
public function isEmpty()
{
if ($this->isInitialized()) {
return $this->collection->isEmpty();
}
return !$this->count();
}
/**
* Do an optimized search of an element
*

View File

@ -103,4 +103,35 @@ class LazyCriteriaCollectionTest extends PHPUnit_Framework_TestCase
$this->assertEquals(array($foo), $this->lazyCriteriaCollection->matching($criteria)->toArray());
}
public function testIsEmptyUsesCountWhenNotInitialized()
{
$this->persister->expects($this->once())->method('count')->with($this->criteria)->will($this->returnValue(0));
$this->assertTrue($this->lazyCriteriaCollection->isEmpty());
}
public function testIsEmptyIsFalseIfCountIsNotZero()
{
$this->persister->expects($this->once())->method('count')->with($this->criteria)->will($this->returnValue(1));
$this->assertFalse($this->lazyCriteriaCollection->isEmpty());
}
public function testIsEmptyUsesWrappedCollectionWhenInitialized()
{
$this
->persister
->expects($this->once())
->method('loadCriteria')
->with($this->criteria)
->will($this->returnValue(array('foo', 'bar', 'baz')));
// should never call the persister's count
$this->persister->expects($this->never())->method('count');
$this->assertSame(array('foo', 'bar', 'baz'), $this->lazyCriteriaCollection->toArray());
$this->assertFalse($this->lazyCriteriaCollection->isEmpty());
}
}