Skip to content

Commit 9f347c6

Browse files
committed
Add tests
1 parent e2d5feb commit 9f347c6

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DateTimeImmutable;
88
use Doctrine\ODM\MongoDB\Aggregation\Stage;
99
use Doctrine\ODM\MongoDB\Iterator\Iterator;
10+
use Doctrine\ODM\MongoDB\Iterator\UnrewindableIterator;
1011
use Doctrine\ODM\MongoDB\Tests\BaseTest;
1112
use Documents\Article;
1213
use Documents\BlogPost;
@@ -352,12 +353,29 @@ public function testBuilderWithOutStageReturnsNoData()
352353
public function testBuilderWithIndexStatsStageDoesNotApplyFilters()
353354
{
354355
$builder = $this->dm
355-
->createAggregationBuilder(Project::class)
356+
->createAggregationBuilder(BlogPost::class)
356357
->indexStats();
357358

358359
$this->assertSame('$indexStats', array_keys($builder->getPipeline()[0])[0]);
359360
}
360361

362+
/**
363+
* @expectedException \LogicException
364+
*/
365+
public function testNonRewindableBuilder()
366+
{
367+
$builder = $this->dm
368+
->createAggregationBuilder(Project::class)
369+
->match()
370+
->rewindable(false);
371+
372+
$iterator = $builder->execute();
373+
$this->assertInstanceOf(UnrewindableIterator::class, $iterator);
374+
iterator_to_array($iterator);
375+
iterator_to_array($iterator);
376+
377+
}
378+
361379
private function insertTestData()
362380
{
363381
$baseballTag = new Tag('baseball');

tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\ODM\MongoDB\Tests\Query;
66

77
use DateTime;
8+
use Doctrine\ODM\MongoDB\Iterator\UnrewindableIterator;
89
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
910
use Doctrine\ODM\MongoDB\Query\Builder;
1011
use Doctrine\ODM\MongoDB\Query\Expr;
@@ -806,6 +807,20 @@ public function testBitXorUpdateQuery()
806807
$this->assertEquals($expected, $qb->getNewObj());
807808
}
808809

810+
/**
811+
* @expectedException \LogicException
812+
*/
813+
public function testNonRewindable()
814+
{
815+
$query = $this->getTestQueryBuilder()
816+
->setRewindable(false)
817+
->getQuery();
818+
819+
$this->assertInstanceOf(UnrewindableIterator::class, $query->execute());
820+
iterator_to_array($query->execute());
821+
iterator_to_array($query->execute());
822+
}
823+
809824
private function getTestQueryBuilder()
810825
{
811826
return new Builder($this->dm, User::class);

tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\ODM\MongoDB\Tests;
66

77
use Doctrine\ODM\MongoDB\Iterator\Iterator;
8+
use Doctrine\ODM\MongoDB\Iterator\UnrewindableIterator;
89
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
910
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
1011
use Doctrine\ODM\MongoDB\Query\Query;
@@ -516,6 +517,33 @@ public function testFindOptionInheritance()
516517
$this->assertInstanceOf(Traversable::class, $query->execute());
517518
}
518519

520+
/**
521+
* @expectedException \LogicException
522+
*/
523+
public function testNonRewindable(): void
524+
{
525+
$cursor = $this->createMock(Traversable::class);
526+
527+
$collection = $this->getMockCollection();
528+
$collection->expects($this->once())
529+
->method('find')
530+
->with([], [])
531+
->will($this->returnValue($cursor));
532+
533+
$queryArray = [
534+
'type' => Query::TYPE_FIND,
535+
'query' => [],
536+
];
537+
$query = new Query($this->dm, new ClassMetadata(User::class), $collection, $queryArray);
538+
$query->setRewindable(false);
539+
540+
$iterator = $query->execute();
541+
542+
$this->assertInstanceOf(UnrewindableIterator::class, $iterator);
543+
iterator_to_array($iterator);
544+
iterator_to_array($iterator);
545+
}
546+
519547
private function getMockCollection()
520548
{
521549
return $this->getMockBuilder(Collection::class)

0 commit comments

Comments
 (0)