Merge pull request #1514 from Metabor/patch-2
check if collection is empty without loading it
This commit is contained in:
commit
2fa289edee
@ -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
|
||||
*
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user