Skip to content

Commit 09e62b9

Browse files
committed
Make PersistentCollection implement Selectable
Implementing ReadableCollection without Selectable is deprecated doctrine/collections#518
1 parent 0370e17 commit 09e62b9

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/PersistentCollection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\ODM\MongoDB;
66

77
use Doctrine\Common\Collections\Collection as BaseCollection;
8+
use Doctrine\Common\Collections\Selectable;
89
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface;
910
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionTrait;
1011

src/PersistentCollection/PersistentCollectionInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\ODM\MongoDB\PersistentCollection;
66

77
use Doctrine\Common\Collections\Collection;
8+
use Doctrine\Common\Collections\Selectable;
89
use Doctrine\ODM\MongoDB\DocumentManager;
910
use Doctrine\ODM\MongoDB\MongoDBException;
1011
use Doctrine\ODM\MongoDB\UnitOfWork;
@@ -21,8 +22,9 @@
2122
* @template TKey of array-key
2223
* @template T of object
2324
* @template-extends Collection<TKey, T>
25+
* @template-extends Selectable<TKey, T>
2426
*/
25-
interface PersistentCollectionInterface extends Collection
27+
interface PersistentCollectionInterface extends Collection, Selectable
2628
{
2729
/**
2830
* Sets the document manager and unit of work (used during merge operations).

src/PersistentCollection/PersistentCollectionTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
use BadMethodCallException;
88
use Closure;
99
use Doctrine\Common\Collections\Collection as BaseCollection;
10+
use Doctrine\Common\Collections\Criteria;
11+
use Doctrine\Common\Collections\Selectable;
1012
use Doctrine\ODM\MongoDB\DocumentManager;
1113
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
1214
use Doctrine\ODM\MongoDB\MongoDBException;
1315
use Doctrine\ODM\MongoDB\UnitOfWork;
1416
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
17+
use LogicException;
1518
use ReturnTypeWillChange;
1619
use Traversable;
1720

@@ -775,4 +778,15 @@ public function reduce(Closure $func, $initial = null)
775778

776779
return $this->coll->reduce($func, $initial);
777780
}
781+
782+
public function matching(Criteria $criteria)
783+
{
784+
$this->initialize();
785+
786+
if (! $this->coll instanceof Selectable) {
787+
throw new LogicException('The backed collection must implement Selectable to use matching().');
788+
}
789+
790+
return $this->coll->matching($criteria);
791+
}
778792
}

tests/Tests/PersistentCollectionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Closure;
88
use Doctrine\Common\Collections\ArrayCollection;
99
use Doctrine\Common\Collections\Collection;
10+
use Doctrine\Common\Collections\Criteria;
1011
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
1112
use Doctrine\ODM\MongoDB\MongoDBException;
1213
use Doctrine\ODM\MongoDB\PersistentCollection;
@@ -332,6 +333,27 @@ public function testIsEmptyUsesCountWhenCollectionIsNotInitialized(): void
332333
self::assertTrue($pcoll->isEmpty());
333334
}
334335

336+
public function testMatchingIsForwarded(): void
337+
{
338+
$criteria = new Criteria();
339+
$criteria->where($criteria->expr()->eq('field', 'value'));
340+
341+
$expectedResult = new ArrayCollection([new stdClass()]);
342+
343+
// ArrayCollection implements both Collection and Selectable
344+
// When doctrine/collections 2.6+ is required, we can mock Collection directly
345+
$collection = $this->createMock(ArrayCollection::class);
346+
$collection->expects($this->once())
347+
->method('matching')
348+
->with($criteria)
349+
->willReturn($expectedResult);
350+
351+
$pcoll = new PersistentCollection($collection, $this->dm, $this->uow);
352+
$result = $pcoll->matching($criteria);
353+
354+
self::assertSame($expectedResult, $result);
355+
}
356+
335357
/** @return Collection<int, object>&MockObject */
336358
private function getMockCollection()
337359
{

0 commit comments

Comments
 (0)