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

api-platform / core / 18776385596

24 Oct 2025 10:01AM UTC coverage: 24.303% (-0.2%) from 24.544%
18776385596

push

github

web-flow
feat(doctrine): remove PUT & PATCH for readonly entity (#7453)

7 of 46 new or added lines in 3 files covered. (15.22%)

144 existing lines in 10 files now uncovered.

13881 of 57116 relevant lines covered (24.3%)

13.24 hits per line

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

0.0
/src/Doctrine/Orm/Tests/Metadata/Resource/DoctrineOrmResourceCollectionMetadataFactoryTest.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\Orm\Tests\Metadata\Resource;
15

16
use ApiPlatform\Doctrine\Orm\Metadata\Resource\DoctrineOrmResourceCollectionMetadataFactory;
17
use ApiPlatform\Doctrine\Orm\State\CollectionProvider;
18
use ApiPlatform\Doctrine\Orm\State\ItemProvider;
19
use ApiPlatform\Doctrine\Orm\Tests\Fixtures\Entity\Dummy;
20
use ApiPlatform\Doctrine\Orm\Tests\Fixtures\Entity\DummyReadOnly;
21
use ApiPlatform\Metadata\ApiResource;
22
use ApiPlatform\Metadata\Delete;
23
use ApiPlatform\Metadata\Get;
24
use ApiPlatform\Metadata\GetCollection;
25
use ApiPlatform\Metadata\HttpOperation;
26
use ApiPlatform\Metadata\Operations;
27
use ApiPlatform\Metadata\Patch;
28
use ApiPlatform\Metadata\Post;
29
use ApiPlatform\Metadata\Put;
30
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
31
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
32
use Doctrine\ORM\EntityManagerInterface;
33
use Doctrine\ORM\Mapping\ClassMetadata;
34
use Doctrine\Persistence\ManagerRegistry;
35
use PHPUnit\Framework\TestCase;
36
use Prophecy\PhpUnit\ProphecyTrait;
37

38
class DoctrineOrmResourceCollectionMetadataFactoryTest extends TestCase
39
{
40
    use ProphecyTrait;
41

42
    private function getResourceMetadataCollectionFactory(HttpOperation $operation)
43
    {
44
        $resourceMetadataCollectionFactory = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
45
        $resourceMetadataCollectionFactory->create($operation->getClass())->willReturn(new ResourceMetadataCollection($operation->getClass(), [
×
46
            (new ApiResource())
×
47
                ->withOperations(
×
48
                    new Operations([$operation->getName() => $operation])
×
49
                )->withGraphQlOperations([
×
50
                    'graphql_'.$operation->getName() => $operation->withName('graphql_'.$operation->getName()),
×
51
                ]),
×
52
        ]));
×
53

54
        return $resourceMetadataCollectionFactory->reveal();
×
55
    }
56

57
    public function testWithoutManager(): void
58
    {
59
        $operation = (new Get())->withClass(Dummy::class)->withName('get');
×
60
        $managerRegistry = $this->prophesize(ManagerRegistry::class);
×
61
        $managerRegistry->getManagerForClass(Dummy::class)->willReturn(null);
×
62

63
        $resourceMetadataCollectionFactory = new DoctrineOrmResourceCollectionMetadataFactory($managerRegistry->reveal(), $this->getResourceMetadataCollectionFactory($operation));
×
64
        $resourceMetadataCollection = $resourceMetadataCollectionFactory->create(Dummy::class);
×
65

66
        $this->assertNull($resourceMetadataCollection->getOperation('get')->getProvider());
×
67
        $this->assertNull($resourceMetadataCollection->getOperation('graphql_get')->getProvider());
×
68
    }
69

70
    #[\PHPUnit\Framework\Attributes\DataProvider('operationProvider')]
71
    public function testWithProvider(HttpOperation $operation, ?string $expectedProvider = null, ?string $expectedProcessor = null): void
72
    {
73
        $objectManager = $this->prophesize(EntityManagerInterface::class);
×
NEW
74
        $objectManager->getClassMetadata($operation->getClass())->willReturn(new ClassMetadata(Dummy::class));
×
75
        $managerRegistry = $this->prophesize(ManagerRegistry::class);
×
76
        $managerRegistry->getManagerForClass($operation->getClass())->willReturn($objectManager->reveal());
×
77
        $resourceMetadataCollectionFactory = new DoctrineOrmResourceCollectionMetadataFactory($managerRegistry->reveal(), $this->getResourceMetadataCollectionFactory($operation));
×
78
        $resourceMetadataCollection = $resourceMetadataCollectionFactory->create($operation->getClass());
×
79
        $this->assertSame($expectedProvider, $resourceMetadataCollection->getOperation($operation->getName())->getProvider());
×
80
        $this->assertSame($expectedProvider, $resourceMetadataCollection->getOperation('graphql_'.$operation->getName())->getProvider());
×
81
        $this->assertSame($expectedProcessor, $resourceMetadataCollection->getOperation($operation->getName())->getProcessor());
×
82
        $this->assertSame($expectedProcessor, $resourceMetadataCollection->getOperation('graphql_'.$operation->getName())->getProcessor());
×
83
    }
84

85
    public static function operationProvider(): iterable
86
    {
87
        $default = (new Get())->withName('get')->withClass(Dummy::class);
×
88

89
        yield [(new Get())->withProvider('has a provider')->withProcessor('and a processor')->withOperation($default), 'has a provider', 'and a processor'];
×
90
        yield [(new Get())->withOperation($default), ItemProvider::class, 'api_platform.doctrine.orm.state.persist_processor'];
×
91
        yield [(new GetCollection())->withOperation($default), CollectionProvider::class, 'api_platform.doctrine.orm.state.persist_processor'];
×
92
        yield [(new Delete())->withOperation($default), ItemProvider::class, 'api_platform.doctrine.orm.state.remove_processor'];
×
93
    }
94

95
    public function testReadOnlyEntitiesShouldNotIncludeUpdateOperations(): void
96
    {
NEW
97
        $objectManager = $this->createMock(EntityManagerInterface::class);
×
NEW
98
        $readOnlyMetadata = new ClassMetadata(DummyReadOnly::class);
×
NEW
99
        $readOnlyMetadata->markReadOnly();
×
NEW
100
        $objectManager->method('getClassMetadata')->willReturn($readOnlyMetadata);
×
NEW
101
        $managerRegistry = $this->createMock(ManagerRegistry::class);
×
NEW
102
        $managerRegistry->method('getManagerForClass')->with(DummyReadOnly::class)->willReturn($objectManager);
×
103

NEW
104
        $resourceMetadataCollectionFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class);
×
NEW
105
        $resourceMetadataCollectionFactory
×
NEW
106
            ->method('create')
×
NEW
107
            ->with(DummyReadOnly::class)
×
NEW
108
            ->willReturn(new ResourceMetadataCollection(DummyReadOnly::class, [
×
NEW
109
                (new ApiResource())
×
NEW
110
                    ->withOperations(
×
NEW
111
                        new Operations([
×
NEW
112
                            'get' => (new Get())->withClass(DummyReadOnly::class),
×
NEW
113
                            'get_collection' => (new GetCollection())->withClass(DummyReadOnly::class),
×
NEW
114
                            'post' => (new Post())->withClass(DummyReadOnly::class),
×
NEW
115
                            'put' => (new Put())->withClass(DummyReadOnly::class),
×
NEW
116
                            'patch' => (new Patch())->withClass(DummyReadOnly::class),
×
NEW
117
                            'delete' => (new Delete())->withClass(DummyReadOnly::class),
×
NEW
118
                        ])
×
NEW
119
                    ),
×
NEW
120
            ]));
×
121

NEW
122
        $resourceMetadataCollectionFactory = new DoctrineOrmResourceCollectionMetadataFactory($managerRegistry, $resourceMetadataCollectionFactory);
×
123

NEW
124
        $resourceMetadataCollection = $resourceMetadataCollectionFactory->create(DummyReadOnly::class);
×
125
        /** @var ApiResource $apiResource */
NEW
126
        $apiResource = $resourceMetadataCollection->getIterator()->current();
×
NEW
127
        $operations = $apiResource->getOperations();
×
NEW
128
        $this->assertNotNull($operations);
×
NEW
129
        $this->assertTrue($operations->has('get'));
×
NEW
130
        $this->assertTrue($operations->has('get_collection'));
×
NEW
131
        $this->assertTrue($operations->has('post'));
×
NEW
132
        $this->assertFalse($operations->has('put'));
×
NEW
133
        $this->assertFalse($operations->has('path'));
×
NEW
134
        $this->assertTrue($operations->has('delete'));
×
135
    }
136
}
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