From 3e26330c534b1784f023fd3b7bc4ef72cca3f422 Mon Sep 17 00:00:00 2001 From: Oliver Tischlinger Date: Wed, 23 Sep 2015 17:34:11 +0200 Subject: [PATCH 1/3] check if collection is empty without loading it Actually isEmpty() is always loading the collection in LazyCriteriaCollection. A lazy version should use the existing functionality of count() to check if there are no elements if the collection is not initialized. --- lib/Doctrine/ORM/LazyCriteriaCollection.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/Doctrine/ORM/LazyCriteriaCollection.php b/lib/Doctrine/ORM/LazyCriteriaCollection.php index 28e2709ac..876341c72 100644 --- a/lib/Doctrine/ORM/LazyCriteriaCollection.php +++ b/lib/Doctrine/ORM/LazyCriteriaCollection.php @@ -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() == 0; + } + /** * Do an optimized search of an element * From 03523c67d5bd6bc71bd13fba0e2d26a842f531c5 Mon Sep 17 00:00:00 2001 From: Oliver Tischlinger Date: Thu, 24 Sep 2015 11:39:14 +0200 Subject: [PATCH 2/3] add Unit Test for isEmpty change in LazyCriteriaCollection --- .../Tests/ORM/LazyCriteriaCollectionTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php index 2f228f98b..42965f268 100644 --- a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php @@ -103,4 +103,28 @@ 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 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()); + } } From 60beca376067caa7aa9e59db2d5946c5f78535a1 Mon Sep 17 00:00:00 2001 From: Oliver Tischlinger Date: Fri, 25 Sep 2015 10:44:30 +0200 Subject: [PATCH 3/3] changes after code review --- lib/Doctrine/ORM/LazyCriteriaCollection.php | 2 +- tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/LazyCriteriaCollection.php b/lib/Doctrine/ORM/LazyCriteriaCollection.php index 876341c72..0751bee41 100644 --- a/lib/Doctrine/ORM/LazyCriteriaCollection.php +++ b/lib/Doctrine/ORM/LazyCriteriaCollection.php @@ -93,7 +93,7 @@ class LazyCriteriaCollection extends AbstractLazyCollection implements Selectabl return $this->collection->isEmpty(); } - return $this->count() == 0; + return !$this->count(); } /** diff --git a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php index 42965f268..92da34cda 100644 --- a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php @@ -111,6 +111,13 @@ class LazyCriteriaCollectionTest extends PHPUnit_Framework_TestCase $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