Skip to content

Commit

Permalink
tests: better coverage of relationship countStored() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Apr 10, 2015
1 parent b8719d4 commit 33eefea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand All @@ -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);
}


Expand Down
23 changes: 12 additions & 11 deletions tests/cases/integration/Relationships/relationships.oneHasMany.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}


Expand Down
5 changes: 5 additions & 0 deletions tests/cases/unit/Mapper/Dbal/DbalMapperTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

}
Expand Down

0 comments on commit 33eefea

Please sign in to comment.