From 33eefea91a25fc7430f4f1adf3b33935b3e5c822 Mon Sep 17 00:00:00 2001 From: hrach Date: Fri, 10 Apr 2015 11:55:24 +0200 Subject: [PATCH] tests: better coverage of relationship countStored() methods --- .../relationships.manyHasMany.phpt | 14 +++++++++-- .../relationships.oneHasMany.phpt | 23 ++++++++++--------- .../unit/Mapper/Dbal/DbalMapperTest.phpt | 5 ++++ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/tests/cases/integration/Relationships/relationships.manyHasMany.phpt b/tests/cases/integration/Relationships/relationships.manyHasMany.phpt index 1531e185..28968e3b 100644 --- a/tests/cases/integration/Relationships/relationships.manyHasMany.phpt +++ b/tests/cases/integration/Relationships/relationships.manyHasMany.phpt @@ -9,6 +9,7 @@ namespace NextrasTests\Orm\Integration\Relationships; use Mockery; use Nextras\Orm\Collection\ICollection; +use NextrasTests\Orm\Book; use NextrasTests\Orm\DataTestCase; use Tester\Assert; @@ -24,10 +25,12 @@ class RelationshipManyHasManyTest extends DataTestCase $collection = $book->tags->get()->findBy(['name!' => 'Tag 1'])->orderBy('id'); Assert::equal(1, $collection->count()); + Assert::equal(1, $collection->countStored()); Assert::equal('Tag 2', $collection->fetch()->name); $collection = $book->tags->get()->findBy(['name!' => 'Tag 3'])->orderBy('id'); Assert::equal(2, $collection->count()); + Assert::equal(2, $collection->countStored()); Assert::equal('Tag 1', $collection->fetch()->name); Assert::equal('Tag 2', $collection->fetch()->name); } @@ -39,17 +42,24 @@ class RelationshipManyHasManyTest extends DataTestCase $book->tags->add(3); $this->orm->books->persistAndFlush($book); - $tags = []; /** @var Book[] $books */ $books = $this->orm->books->findAll()->orderBy('id'); + $tags = []; + $counts = []; + $countsStored = []; foreach ($books as $book) { - foreach ($book->tags->get()->limitBy(2)->orderBy('name', ICollection::DESC) as $tag) { + $limitedTags = $book->tags->get()->limitBy(2)->orderBy('name', ICollection::DESC); + foreach ($limitedTags as $tag) { $tags[] = $tag->id; } + $counts[] = $limitedTags->count(); + $countsStored[] = $limitedTags->countStored(); } Assert::same([3, 2, 3, 2, 3], $tags); + Assert::same([2, 2, 1, 0], $counts); + Assert::same([2, 2, 1, 0], $countsStored); } diff --git a/tests/cases/integration/Relationships/relationships.oneHasMany.phpt b/tests/cases/integration/Relationships/relationships.oneHasMany.phpt index fc1625c1..17380964 100644 --- a/tests/cases/integration/Relationships/relationships.oneHasMany.phpt +++ b/tests/cases/integration/Relationships/relationships.oneHasMany.phpt @@ -26,15 +26,18 @@ class RelationshipOneHasManyTest extends DataTestCase $collection = $author->books->get()->findBy(['title!' => 'Book 1']); Assert::equal(1, $collection->count()); + Assert::equal(1, $collection->countStored()); Assert::equal('Book 2', $collection->fetch()->title); $collection = $author->books->get()->findBy(['title!' => 'Book 3']); Assert::equal(2, $collection->count()); + Assert::equal(2, $collection->countStored()); Assert::equal('Book 2', $collection->fetch()->title); Assert::equal('Book 1', $collection->fetch()->title); $collection = $author->books->get()->toCollection(TRUE)->findBy(['title!' => 'Book 3'])->orderBy('id'); Assert::equal(2, $collection->count()); + Assert::equal(2, $collection->countStored()); Assert::equal('Book 1', $collection->fetch()->title); Assert::equal('Book 2', $collection->fetch()->title); } @@ -104,26 +107,24 @@ class RelationshipOneHasManyTest extends DataTestCase $book->publisher = 1; $this->orm->books->persistAndFlush($book); - $books = []; /** @var Author[] $authors */ $authors = $this->orm->authors->findAll()->orderBy('id'); + $books = []; + $counts = []; + $countsStored = []; foreach ($authors as $author) { - foreach ($author->books->get()->limitBy(2)->orderBy('title', ICollection::DESC) as $book) { + $booksLimited = $author->books->get()->limitBy(2)->orderBy('title', ICollection::DESC); + foreach ($booksLimited as $book) { $books[] = $book->id; } + $counts[] = $booksLimited->count(); + $countsStored[] = $booksLimited->countStored(); } Assert::same([5, 2, 4, 3], $books); - } - - - public function testCollectionCountWithLimit() - { - $author = $this->orm->authors->getById(1); - $collection = $author->books->get(); - $collection = $collection->limitBy(1, 1); - Assert::same(1, $collection->count()); + Assert::same([2, 2], $counts); + Assert::same([2, 2], $countsStored); } diff --git a/tests/cases/unit/Mapper/Dbal/DbalMapperTest.phpt b/tests/cases/unit/Mapper/Dbal/DbalMapperTest.phpt index 4f190319..9f581547 100644 --- a/tests/cases/unit/Mapper/Dbal/DbalMapperTest.phpt +++ b/tests/cases/unit/Mapper/Dbal/DbalMapperTest.phpt @@ -85,6 +85,11 @@ class DbalMapperTest extends TestCase Assert::equal((object) ['id' => 1], $data[0]); Assert::equal((object) ['id' => 2], $data[1]); Assert::equal((object) ['id' => 3], $data[2]); + + + Assert::throws(function() use ($mapper) { + $mapper->toCollection(new ArrayCollection([])); + }, 'Nextras\Orm\InvalidArgumentException'); } }