• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 13203378522

07 Feb 2025 03:56PM UTC coverage: 8.501% (+0.7%) from 7.837%
13203378522

push

github

soyuka
Merge 4.1

111 of 490 new or added lines in 51 files covered. (22.65%)

5590 existing lines in 163 files now uncovered.

13345 of 156987 relevant lines covered (8.5%)

22.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Doctrine/Odm/Tests/State/CollectionProviderTest.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Doctrine\Odm\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\IterableResult;
26
use Doctrine\ODM\MongoDB\Iterator\Iterator;
27
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
28
use Doctrine\Persistence\ManagerRegistry;
29
use Doctrine\Persistence\ObjectRepository;
30
use PHPUnit\Framework\TestCase;
31
use Prophecy\PhpUnit\ProphecyTrait;
32
use Prophecy\Prophecy\ObjectProphecy;
33

34
class CollectionProviderTest extends TestCase
35
{
36
    use ProphecyTrait;
37

38
    private ObjectProphecy $managerRegistryProphecy;
39
    private ObjectProphecy $resourceMetadataFactoryProphecy;
40

41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function setUp(): void
45
    {
46
        $this->managerRegistryProphecy = $this->prophesize(ManagerRegistry::class);
×
UNCOV
47
        $this->resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
48
    }
49

50
    public function testGetCollection(): void
51
    {
UNCOV
52
        $iterator = $this->prophesize(Iterator::class)->reveal();
×
53

54
        $aggregationProphecy = $this->prophesize(IterableResult::class);
×
55
        $aggregationProphecy->getIterator()->willReturn($iterator);
×
56

UNCOV
57
        $aggregationBuilderProphecy = $this->prophesize(Builder::class);
×
58
        $aggregationBuilderProphecy->hydrate(ProviderDocument::class)->willReturn($aggregationBuilderProphecy)->shouldBeCalled();
×
59
        $aggregationBuilderProphecy->getAggregation([])->willReturn($aggregationProphecy)->shouldBeCalled();
×
UNCOV
60
        $aggregationBuilder = $aggregationBuilderProphecy->reveal();
×
61

62
        $repositoryProphecy = $this->prophesize(DocumentRepository::class);
×
UNCOV
63
        $repositoryProphecy->createAggregationBuilder()->willReturn($aggregationBuilder)->shouldBeCalled();
×
64

UNCOV
65
        $managerProphecy = $this->prophesize(DocumentManager::class);
×
66
        $managerProphecy->getRepository(ProviderDocument::class)->willReturn($repositoryProphecy->reveal())->shouldBeCalled();
×
67

68
        $this->managerRegistryProphecy->getManagerForClass(ProviderDocument::class)->willReturn($managerProphecy->reveal())->shouldBeCalled();
×
69

UNCOV
70
        $operation = (new GetCollection())->withName('foo')->withClass(ProviderDocument::class);
×
71

72
        $extensionProphecy = $this->prophesize(AggregationCollectionExtensionInterface::class);
×
UNCOV
73
        $extensionProphecy->applyToCollection($aggregationBuilder, ProviderDocument::class, $operation, [])->shouldBeCalled();
×
74

UNCOV
75
        $dataProvider = new CollectionProvider($this->resourceMetadataFactoryProphecy->reveal(), $this->managerRegistryProphecy->reveal(), [$extensionProphecy->reveal()]);
×
UNCOV
76
        $this->assertSame($iterator, $dataProvider->provide($operation, []));
×
77
    }
78

79
    public function testGetCollectionWithExecuteOptions(): void
80
    {
81
        $iterator = $this->prophesize(Iterator::class)->reveal();
×
82

UNCOV
83
        $aggregationProphecy = $this->prophesize(IterableResult::class);
×
84
        $aggregationProphecy->getIterator()->willReturn($iterator);
×
85

UNCOV
86
        $aggregationBuilderProphecy = $this->prophesize(Builder::class);
×
87
        $aggregationBuilderProphecy->hydrate(ProviderDocument::class)->willReturn($aggregationBuilderProphecy)->shouldBeCalled();
×
88
        $aggregationBuilderProphecy->getAggregation(['allowDiskUse' => true])->willReturn($aggregationProphecy)->shouldBeCalled();
×
UNCOV
89
        $aggregationBuilder = $aggregationBuilderProphecy->reveal();
×
90

UNCOV
91
        $repositoryProphecy = $this->prophesize(DocumentRepository::class);
×
92
        $repositoryProphecy->createAggregationBuilder()->willReturn($aggregationBuilder)->shouldBeCalled();
×
93

94
        $managerProphecy = $this->prophesize(DocumentManager::class);
×
95
        $managerProphecy->getRepository(ProviderDocument::class)->willReturn($repositoryProphecy->reveal())->shouldBeCalled();
×
96

97
        $this->managerRegistryProphecy->getManagerForClass(ProviderDocument::class)->willReturn($managerProphecy->reveal())->shouldBeCalled();
×
98

UNCOV
99
        $operation = (new GetCollection())->withExtraProperties(['doctrine_mongodb' => ['execute_options' => ['allowDiskUse' => true]]])->withName('foo')->withClass(ProviderDocument::class);
×
100

UNCOV
101
        $extensionProphecy = $this->prophesize(AggregationCollectionExtensionInterface::class);
×
UNCOV
102
        $extensionProphecy->applyToCollection($aggregationBuilder, ProviderDocument::class, $operation, [])->shouldBeCalled();
×
103

104
        $dataProvider = new CollectionProvider($this->resourceMetadataFactoryProphecy->reveal(), $this->managerRegistryProphecy->reveal(), [$extensionProphecy->reveal()]);
×
UNCOV
105
        $this->assertSame($iterator, $dataProvider->provide($operation, []));
×
106
    }
107

108
    public function testAggregationResultExtension(): void
109
    {
110
        $aggregationBuilderProphecy = $this->prophesize(Builder::class);
×
UNCOV
111
        $aggregationBuilder = $aggregationBuilderProphecy->reveal();
×
112

UNCOV
113
        $repositoryProphecy = $this->prophesize(DocumentRepository::class);
×
114
        $repositoryProphecy->createAggregationBuilder()->willReturn($aggregationBuilder)->shouldBeCalled();
×
115

116
        $managerProphecy = $this->prophesize(DocumentManager::class);
×
117
        $managerProphecy->getRepository(ProviderDocument::class)->willReturn($repositoryProphecy->reveal())->shouldBeCalled();
×
118

119
        $this->managerRegistryProphecy->getManagerForClass(ProviderDocument::class)->willReturn($managerProphecy->reveal())->shouldBeCalled();
×
120

121
        $operation = (new GetCollection())->withName('foo')->withClass(ProviderDocument::class);
×
122

UNCOV
123
        $extensionProphecy = $this->prophesize(AggregationResultCollectionExtensionInterface::class);
×
UNCOV
124
        $extensionProphecy->applyToCollection($aggregationBuilder, ProviderDocument::class, $operation, [])->shouldBeCalled();
×
UNCOV
125
        $extensionProphecy->supportsResult(ProviderDocument::class, $operation, [])->willReturn(true)->shouldBeCalled();
×
UNCOV
126
        $extensionProphecy->getResult($aggregationBuilder, ProviderDocument::class, $operation, [])->willReturn([])->shouldBeCalled();
×
127

128
        $dataProvider = new CollectionProvider($this->resourceMetadataFactoryProphecy->reveal(), $this->managerRegistryProphecy->reveal(), [$extensionProphecy->reveal()]);
×
UNCOV
129
        $this->assertEquals([], $dataProvider->provide($operation, []));
×
130
    }
131

132
    public function testCannotCreateAggregationBuilder(): void
133
    {
UNCOV
134
        $this->expectException(RuntimeException::class);
×
135
        $this->expectExceptionMessage('The repository for "ApiPlatform\Doctrine\Odm\Tests\Fixtures\Document\ProviderDocument" must be an instance of "Doctrine\ODM\MongoDB\Repository\DocumentRepository".');
×
136

137
        $repositoryProphecy = $this->prophesize(ObjectRepository::class);
×
138

139
        $managerProphecy = $this->prophesize(DocumentManager::class);
×
UNCOV
140
        $managerProphecy->getRepository(ProviderDocument::class)->willReturn($repositoryProphecy->reveal())->shouldBeCalled();
×
141

UNCOV
142
        $this->managerRegistryProphecy->getManagerForClass(ProviderDocument::class)->willReturn($managerProphecy->reveal())->shouldBeCalled();
×
143

144
        $dataProvider = new CollectionProvider($this->resourceMetadataFactoryProphecy->reveal(), $this->managerRegistryProphecy->reveal());
×
UNCOV
145
        $operation = (new GetCollection())->withName('foo')->withClass(ProviderDocument::class);
×
146
        $this->assertEquals([], $dataProvider->provide($operation, []));
×
147
    }
148

149
    public function testOperationNotFound(): void
150
    {
151
        $iterator = $this->prophesize(Iterator::class)->reveal();
×
152

UNCOV
153
        $aggregationProphecy = $this->prophesize(IterableResult::class);
×
154
        $aggregationProphecy->getIterator()->willReturn($iterator);
×
155

UNCOV
156
        $aggregationBuilderProphecy = $this->prophesize(Builder::class);
×
157
        $aggregationBuilderProphecy->hydrate(ProviderDocument::class)->willReturn($aggregationBuilderProphecy)->shouldBeCalled();
×
UNCOV
158
        $aggregationBuilderProphecy->getAggregation([])->willReturn($aggregationProphecy)->shouldBeCalled();
×
159
        $aggregationBuilder = $aggregationBuilderProphecy->reveal();
×
160

161
        $repositoryProphecy = $this->prophesize(DocumentRepository::class);
×
162
        $repositoryProphecy->createAggregationBuilder()->willReturn($aggregationBuilder)->shouldBeCalled();
×
163

164
        $managerProphecy = $this->prophesize(DocumentManager::class);
×
165
        $managerProphecy->getRepository(ProviderDocument::class)->willReturn($repositoryProphecy->reveal())->shouldBeCalled();
×
166

UNCOV
167
        $this->managerRegistryProphecy->getManagerForClass(ProviderDocument::class)->willReturn($managerProphecy->reveal())->shouldBeCalled();
×
168

UNCOV
169
        $operation = new GetCollection(name: 'bar', class: ProviderDocument::class);
×
170

UNCOV
171
        $extensionProphecy = $this->prophesize(AggregationCollectionExtensionInterface::class);
×
UNCOV
172
        $extensionProphecy->applyToCollection($aggregationBuilder, ProviderDocument::class, $operation, [])->shouldBeCalled();
×
173

UNCOV
174
        $dataProvider = new CollectionProvider($this->resourceMetadataFactoryProphecy->reveal(), $this->managerRegistryProphecy->reveal(), [$extensionProphecy->reveal()]);
×
UNCOV
175
        $this->assertSame($iterator, $dataProvider->provide($operation, []));
×
176
    }
177
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc