Skip to content

Commit 5fa1c7d

Browse files
committed
Tests
1 parent d2d9d80 commit 5fa1c7d

File tree

2 files changed

+380
-0
lines changed

2 files changed

+380
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\MongoDB\Tests\State;
15+
16+
use ApiPlatform\Doctrine\Odm\Extension\AggregationCollectionExtensionInterface;
17+
use ApiPlatform\Doctrine\Odm\Extension\AggregationResultCollectionExtensionInterface;
18+
use ApiPlatform\Doctrine\Odm\State\CollectionProvider;
19+
use ApiPlatform\Doctrine\Odm\Tests\Fixtures\Document\ProviderDocument;
20+
use ApiPlatform\Metadata\Exception\RuntimeException;
21+
use ApiPlatform\Metadata\GetCollection;
22+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23+
use Doctrine\ODM\MongoDB\Aggregation\Builder;
24+
use Doctrine\ODM\MongoDB\DocumentManager;
25+
use Doctrine\ODM\MongoDB\Iterator\Iterator;
26+
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
27+
use Doctrine\Persistence\ManagerRegistry;
28+
use Doctrine\Persistence\ObjectRepository;
29+
use PHPUnit\Framework\TestCase;
30+
use Prophecy\PhpUnit\ProphecyTrait;
31+
use Prophecy\Prophecy\ObjectProphecy;
32+
33+
class CollectionProviderTest extends TestCase
34+
{
35+
use ProphecyTrait;
36+
37+
private ObjectProphecy $managerRegistryProphecy;
38+
private ObjectProphecy $resourceMetadataFactoryProphecy;
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
protected function setUp(): void
44+
{
45+
$this->managerRegistryProphecy = $this->prophesize(ManagerRegistry::class);
46+
$this->resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
47+
}
48+
49+
public function testGetCollection(): void
50+
{
51+
$iterator = $this->prophesize(Iterator::class)->reveal();
52+
53+
$aggregationBuilderProphecy = $this->prophesize(Builder::class);
54+
$aggregationBuilderProphecy->hydrate(ProviderDocument::class)->willReturn($aggregationBuilderProphecy)->shouldBeCalled();
55+
$aggregationBuilderProphecy->execute([])->willReturn($iterator)->shouldBeCalled();
56+
$aggregationBuilder = $aggregationBuilderProphecy->reveal();
57+
58+
$repositoryProphecy = $this->prophesize(DocumentRepository::class);
59+
$repositoryProphecy->createAggregationBuilder()->willReturn($aggregationBuilder)->shouldBeCalled();
60+
61+
$managerProphecy = $this->prophesize(DocumentManager::class);
62+
$managerProphecy->getRepository(ProviderDocument::class)->willReturn($repositoryProphecy->reveal())->shouldBeCalled();
63+
64+
$this->managerRegistryProphecy->getManagerForClass(ProviderDocument::class)->willReturn($managerProphecy->reveal())->shouldBeCalled();
65+
66+
$operation = (new GetCollection())->withName('foo')->withClass(ProviderDocument::class);
67+
68+
$extensionProphecy = $this->prophesize(AggregationCollectionExtensionInterface::class);
69+
$extensionProphecy->applyToCollection($aggregationBuilder, ProviderDocument::class, $operation, [])->shouldBeCalled();
70+
71+
$dataProvider = new CollectionProvider($this->resourceMetadataFactoryProphecy->reveal(), $this->managerRegistryProphecy->reveal(), [$extensionProphecy->reveal()]);
72+
$this->assertSame($iterator, $dataProvider->provide($operation, []));
73+
}
74+
75+
public function testGetCollectionWithExecuteOptions(): void
76+
{
77+
$iterator = $this->prophesize(Iterator::class)->reveal();
78+
79+
$aggregationBuilderProphecy = $this->prophesize(Builder::class);
80+
$aggregationBuilderProphecy->hydrate(ProviderDocument::class)->willReturn($aggregationBuilderProphecy)->shouldBeCalled();
81+
$aggregationBuilderProphecy->execute(['allowDiskUse' => true])->willReturn($iterator)->shouldBeCalled();
82+
$aggregationBuilder = $aggregationBuilderProphecy->reveal();
83+
84+
$repositoryProphecy = $this->prophesize(DocumentRepository::class);
85+
$repositoryProphecy->createAggregationBuilder()->willReturn($aggregationBuilder)->shouldBeCalled();
86+
87+
$managerProphecy = $this->prophesize(DocumentManager::class);
88+
$managerProphecy->getRepository(ProviderDocument::class)->willReturn($repositoryProphecy->reveal())->shouldBeCalled();
89+
90+
$this->managerRegistryProphecy->getManagerForClass(ProviderDocument::class)->willReturn($managerProphecy->reveal())->shouldBeCalled();
91+
92+
$operation = (new GetCollection())->withExtraProperties(['doctrine_mongodb' => ['execute_options' => ['allowDiskUse' => true]]])->withName('foo')->withClass(ProviderDocument::class);
93+
94+
$extensionProphecy = $this->prophesize(AggregationCollectionExtensionInterface::class);
95+
$extensionProphecy->applyToCollection($aggregationBuilder, ProviderDocument::class, $operation, [])->shouldBeCalled();
96+
97+
$dataProvider = new CollectionProvider($this->resourceMetadataFactoryProphecy->reveal(), $this->managerRegistryProphecy->reveal(), [$extensionProphecy->reveal()]);
98+
$this->assertSame($iterator, $dataProvider->provide($operation, []));
99+
}
100+
101+
public function testAggregationResultExtension(): void
102+
{
103+
$aggregationBuilderProphecy = $this->prophesize(Builder::class);
104+
$aggregationBuilder = $aggregationBuilderProphecy->reveal();
105+
106+
$repositoryProphecy = $this->prophesize(DocumentRepository::class);
107+
$repositoryProphecy->createAggregationBuilder()->willReturn($aggregationBuilder)->shouldBeCalled();
108+
109+
$managerProphecy = $this->prophesize(DocumentManager::class);
110+
$managerProphecy->getRepository(ProviderDocument::class)->willReturn($repositoryProphecy->reveal())->shouldBeCalled();
111+
112+
$this->managerRegistryProphecy->getManagerForClass(ProviderDocument::class)->willReturn($managerProphecy->reveal())->shouldBeCalled();
113+
114+
$operation = (new GetCollection())->withName('foo')->withClass(ProviderDocument::class);
115+
116+
$extensionProphecy = $this->prophesize(AggregationResultCollectionExtensionInterface::class);
117+
$extensionProphecy->applyToCollection($aggregationBuilder, ProviderDocument::class, $operation, [])->shouldBeCalled();
118+
$extensionProphecy->supportsResult(ProviderDocument::class, $operation, [])->willReturn(true)->shouldBeCalled();
119+
$extensionProphecy->getResult($aggregationBuilder, ProviderDocument::class, $operation, [])->willReturn([])->shouldBeCalled();
120+
121+
$dataProvider = new CollectionProvider($this->resourceMetadataFactoryProphecy->reveal(), $this->managerRegistryProphecy->reveal(), [$extensionProphecy->reveal()]);
122+
$this->assertEquals([], $dataProvider->provide($operation, []));
123+
}
124+
125+
public function testCannotCreateAggregationBuilder(): void
126+
{
127+
$this->expectException(RuntimeException::class);
128+
$this->expectExceptionMessage('The repository for "ApiPlatform\Doctrine\Odm\Tests\Fixtures\Document\ProviderDocument" must be an instance of "Doctrine\ODM\MongoDB\Repository\DocumentRepository".');
129+
130+
$repositoryProphecy = $this->prophesize(ObjectRepository::class);
131+
132+
$managerProphecy = $this->prophesize(DocumentManager::class);
133+
$managerProphecy->getRepository(ProviderDocument::class)->willReturn($repositoryProphecy->reveal())->shouldBeCalled();
134+
135+
$this->managerRegistryProphecy->getManagerForClass(ProviderDocument::class)->willReturn($managerProphecy->reveal())->shouldBeCalled();
136+
137+
$dataProvider = new CollectionProvider($this->resourceMetadataFactoryProphecy->reveal(), $this->managerRegistryProphecy->reveal());
138+
$operation = (new GetCollection())->withName('foo')->withClass(ProviderDocument::class);
139+
$this->assertEquals([], $dataProvider->provide($operation, []));
140+
}
141+
142+
public function testOperationNotFound(): void
143+
{
144+
$iterator = $this->prophesize(Iterator::class)->reveal();
145+
146+
$aggregationBuilderProphecy = $this->prophesize(Builder::class);
147+
$aggregationBuilderProphecy->hydrate(ProviderDocument::class)->willReturn($aggregationBuilderProphecy)->shouldBeCalled();
148+
$aggregationBuilderProphecy->execute([])->willReturn($iterator)->shouldBeCalled();
149+
$aggregationBuilder = $aggregationBuilderProphecy->reveal();
150+
151+
$repositoryProphecy = $this->prophesize(DocumentRepository::class);
152+
$repositoryProphecy->createAggregationBuilder()->willReturn($aggregationBuilder)->shouldBeCalled();
153+
154+
$managerProphecy = $this->prophesize(DocumentManager::class);
155+
$managerProphecy->getRepository(ProviderDocument::class)->willReturn($repositoryProphecy->reveal())->shouldBeCalled();
156+
157+
$this->managerRegistryProphecy->getManagerForClass(ProviderDocument::class)->willReturn($managerProphecy->reveal())->shouldBeCalled();
158+
159+
$operation = new GetCollection(name: 'bar', class: ProviderDocument::class);
160+
161+
$extensionProphecy = $this->prophesize(AggregationCollectionExtensionInterface::class);
162+
$extensionProphecy->applyToCollection($aggregationBuilder, ProviderDocument::class, $operation, [])->shouldBeCalled();
163+
164+
$dataProvider = new CollectionProvider($this->resourceMetadataFactoryProphecy->reveal(), $this->managerRegistryProphecy->reveal(), [$extensionProphecy->reveal()]);
165+
$this->assertSame($iterator, $dataProvider->provide($operation, []));
166+
}
167+
}

0 commit comments

Comments
 (0)