From 7cb40ed9153961742e6f26144da36ceef547c936 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 17 May 2014 18:45:33 +0200 Subject: [PATCH 1/7] Mocking up tests for the lazy-criteria-collection --- .../Tests/ORM/LazyCriteriaCollectionTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php diff --git a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php new file mode 100644 index 000000000..3c8ed473c --- /dev/null +++ b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php @@ -0,0 +1,22 @@ + + * + * @covers \Doctrine\ORM\LazyCriteriaCollection + */ +class LazyCriteriaCollectionTest extends PHPUnit_Framework_TestCase +{ + /** + * {@inheritDoc} + */ + protected function setUp() + { + + } +} From 81fbb049a53d7d4ed5f69805be1381face1a721c Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 17 May 2014 18:48:25 +0200 Subject: [PATCH 2/7] LazyCriteriaCollection test `setUp` --- .../Tests/ORM/LazyCriteriaCollectionTest.php | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php index 3c8ed473c..59fd543f6 100644 --- a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php @@ -2,6 +2,8 @@ namespace Doctrine\Tests\ORM; +use Doctrine\Common\Collections\Criteria; +use Doctrine\ORM\LazyCriteriaCollection; use Doctrine\Tests\Mocks\ConnectionMock; use PHPUnit_Framework_TestCase; @@ -12,11 +14,28 @@ use PHPUnit_Framework_TestCase; */ class LazyCriteriaCollectionTest extends PHPUnit_Framework_TestCase { + /** + * @var \Doctrine\ORM\Persisters\EntityPersister|\PHPUnit_Framework_MockObject_MockObject + */ + private $persister; + + /** + * @var Criteria + */ + private $criteria; + + /** + * @var LazyCriteriaCollection + */ + private $lazyCriteriaCollection; + /** * {@inheritDoc} */ protected function setUp() { - + $this->persister = $this->getMock('Doctrine\ORM\Persisters\EntityPersister'); + $this->criteria = new Criteria(); + $this->$lazyCriteriaCollection = new LazyCriteriaCollection($this->persister, $this->criteria); } } From c46b63f6b4ae294794f129566bc273da502ed127 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 17 May 2014 18:51:01 +0200 Subject: [PATCH 3/7] Verifying that count on the lazy criteria collection is cached --- .../Tests/ORM/LazyCriteriaCollectionTest.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php index 59fd543f6..93cdc01d5 100644 --- a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php @@ -34,8 +34,17 @@ class LazyCriteriaCollectionTest extends PHPUnit_Framework_TestCase */ protected function setUp() { - $this->persister = $this->getMock('Doctrine\ORM\Persisters\EntityPersister'); - $this->criteria = new Criteria(); - $this->$lazyCriteriaCollection = new LazyCriteriaCollection($this->persister, $this->criteria); + $this->persister = $this->getMock('Doctrine\ORM\Persisters\EntityPersister'); + $this->criteria = new Criteria(); + $this->lazyCriteriaCollection = new LazyCriteriaCollection($this->persister, $this->criteria); + } + + public function testCountIsCached() + { + $this->persister->expects($this->once())->method('count')->with($this->criteria)->will($this->returnValue(10)); + + $this->assertSame(10, $this->lazyCriteriaCollection->count()); + $this->assertSame(10, $this->lazyCriteriaCollection->count()); + $this->assertSame(10, $this->lazyCriteriaCollection->count()); } } From 546bb53ef981d66881ec13970bd440ca074d0d71 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 17 May 2014 18:51:33 +0200 Subject: [PATCH 4/7] Verifying that count on the lazy criteria collection is cached even with `0` count --- tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php index 93cdc01d5..2d5209cf4 100644 --- a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php @@ -47,4 +47,13 @@ class LazyCriteriaCollectionTest extends PHPUnit_Framework_TestCase $this->assertSame(10, $this->lazyCriteriaCollection->count()); $this->assertSame(10, $this->lazyCriteriaCollection->count()); } + + public function testCountIsCachedEvenWithZeroResult() + { + $this->persister->expects($this->once())->method('count')->with($this->criteria)->will($this->returnValue(0)); + + $this->assertSame(0, $this->lazyCriteriaCollection->count()); + $this->assertSame(0, $this->lazyCriteriaCollection->count()); + $this->assertSame(0, $this->lazyCriteriaCollection->count()); + } } From d2174a893a44accca837b0b8fb35a2eadd4e76b5 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 17 May 2014 18:56:42 +0200 Subject: [PATCH 5/7] Verifying that `count` is not called on the persister when the collection is initialized --- .../Tests/ORM/LazyCriteriaCollectionTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php index 2d5209cf4..5f274a7b0 100644 --- a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php @@ -56,4 +56,21 @@ class LazyCriteriaCollectionTest extends PHPUnit_Framework_TestCase $this->assertSame(0, $this->lazyCriteriaCollection->count()); $this->assertSame(0, $this->lazyCriteriaCollection->count()); } + + public function testCountUsesWrappedCollectionWhenInitialized() + { + $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->assertSame(3, $this->lazyCriteriaCollection->count()); + } } From c68ed4c20498aa6386df5492672df353d48b1547 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 17 May 2014 19:06:16 +0200 Subject: [PATCH 6/7] Verifying that the persister is used only once when matching on a lazy criteria collection --- .../Tests/ORM/LazyCriteriaCollectionTest.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php index 5f274a7b0..ef9d72b26 100644 --- a/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/LazyCriteriaCollectionTest.php @@ -5,6 +5,7 @@ namespace Doctrine\Tests\ORM; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\LazyCriteriaCollection; use Doctrine\Tests\Mocks\ConnectionMock; +use stdClass; use PHPUnit_Framework_TestCase; /** @@ -73,4 +74,33 @@ class LazyCriteriaCollectionTest extends PHPUnit_Framework_TestCase $this->assertSame(3, $this->lazyCriteriaCollection->count()); } + + public function testMatchingUsesThePersisterOnlyOnce() + { + $foo = new stdClass(); + $bar = new stdClass(); + $baz = new stdClass(); + + $foo->val = 'foo'; + $bar->val = 'bar'; + $baz->val = 'baz'; + + $this + ->persister + ->expects($this->once()) + ->method('loadCriteria') + ->with($this->criteria) + ->will($this->returnValue(array($foo, $bar, $baz))); + + $criteria = new Criteria(); + + $criteria->andWhere($criteria->expr()->eq('val', 'foo')); + + $filtered = $this->lazyCriteriaCollection->matching($criteria); + + $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $filtered); + $this->assertEquals(array($foo), $filtered->toArray()); + + $this->assertEquals(array($foo), $this->lazyCriteriaCollection->matching($criteria)->toArray()); + } } From a2591381803364190b6413add6d372d19eb85ba4 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 17 May 2014 19:08:25 +0200 Subject: [PATCH 7/7] The `count` in a `LazyCriteriaCollection` is `null` on initialization --- lib/Doctrine/ORM/LazyCriteriaCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/LazyCriteriaCollection.php b/lib/Doctrine/ORM/LazyCriteriaCollection.php index bb1219696..1aad8482e 100644 --- a/lib/Doctrine/ORM/LazyCriteriaCollection.php +++ b/lib/Doctrine/ORM/LazyCriteriaCollection.php @@ -49,7 +49,7 @@ class LazyCriteriaCollection extends AbstractLazyCollection implements Selectabl protected $criteria; /** - * @var integer + * @var integer|null */ private $count;