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

api-platform / core / 10884379752

16 Sep 2024 01:01PM UTC coverage: 7.281% (-0.4%) from 7.672%
10884379752

push

github

soyuka
Merge 3.4

0 of 100 new or added lines in 7 files covered. (0.0%)

5332 existing lines in 181 files now uncovered.

11994 of 164725 relevant lines covered (7.28%)

9.52 hits per line

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

0.0
/src/Serializer/Tests/SerializerContextBuilderTest.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\Serializer\Tests;
15

16
use ApiPlatform\Metadata\ApiResource;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\Get;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\Patch;
21
use ApiPlatform\Metadata\Put;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
24
use ApiPlatform\Serializer\SerializerContextBuilder;
25
use PHPUnit\Framework\TestCase;
26
use Prophecy\PhpUnit\ProphecyTrait;
27
use Symfony\Component\HttpFoundation\Request;
28

29
/**
30
 * @author Kévin Dunglas <dunglas@gmail.com>
31
 */
32
class SerializerContextBuilderTest extends TestCase
33
{
34
    use ProphecyTrait;
35

36
    private SerializerContextBuilder $builder;
37
    private HttpOperation $operation;
38
    private HttpOperation $patchOperation;
39

40
    protected function setUp(): void
41
    {
42
        $this->operation = new Get(normalizationContext: ['foo' => 'bar'], denormalizationContext: ['bar' => 'baz'], name: 'get');
×
43
        $resourceMetadata = new ResourceMetadataCollection('Foo', [
×
44
            new ApiResource(operations: [
×
45
                'get' => $this->operation,
×
46
                'post' => $this->operation->withName('post'),
×
47
                'put' => (new Put(name: 'put'))->withOperation($this->operation),
×
48
                'get_collection' => $this->operation->withName('get_collection'),
×
49
            ]),
×
50
        ]);
×
51

52
        $this->patchOperation = new Patch(inputFormats: ['json' => ['application/merge-patch+json']], name: 'patch');
×
53
        $resourceMetadataWithPatch = new ResourceMetadataCollection('Foo', [
×
54
            new ApiResource(operations: [
×
55
                'patch' => $this->patchOperation,
×
56
            ]),
×
57
        ]);
×
58

59
        $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
×
60
        $resourceMetadataFactoryProphecy->create('Foo')->willReturn($resourceMetadata);
×
61
        $resourceMetadataFactoryProphecy->create('FooWithPatch')->willReturn($resourceMetadataWithPatch);
×
62

63
        $this->builder = new SerializerContextBuilder($resourceMetadataFactoryProphecy->reveal());
×
64
    }
65

66
    public function testCreateFromRequest(): void
67
    {
68
        $request = Request::create('/foos/1');
×
69
        $request->attributes->replace(['_api_resource_class' => 'Foo', '_api_operation_name' => 'get', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
×
NEW
70
        $expected = ['foo' => 'bar', 'operation_name' => 'get',  'resource_class' => 'Foo', 'request_uri' => '/foos/1', 'uri' => 'http://localhost/foos/1', 'output' => null, 'input' => null, 'iri_only' => false, 'skip_null_values' => true, 'operation' => $this->operation, 'exclude_from_cache_key' => ['root_operation', 'operation', 'object', 'data', 'property_metadata', 'circular_reference_limit_counters', 'debug_trace_id']];
×
71
        $this->assertEquals($expected, $this->builder->createFromRequest($request, true));
×
72

73
        $request = Request::create('/foos');
×
74
        $request->attributes->replace(['_api_resource_class' => 'Foo', '_api_operation_name' => 'get_collection', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
×
NEW
75
        $expected = ['foo' => 'bar', 'operation_name' => 'get_collection',  'resource_class' => 'Foo', 'request_uri' => '/foos', 'uri' => 'http://localhost/foos', 'output' => null, 'input' => null, 'iri_only' => false, 'skip_null_values' => true, 'operation' => $this->operation->withName('get_collection'), 'exclude_from_cache_key' => ['root_operation', 'operation', 'object', 'data', 'property_metadata',  'circular_reference_limit_counters', 'debug_trace_id']];
×
76
        $this->assertEquals($expected, $this->builder->createFromRequest($request, true));
×
77

78
        $request = Request::create('/foos/1');
×
79
        $request->attributes->replace(['_api_resource_class' => 'Foo', '_api_operation_name' => 'get', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
×
NEW
80
        $expected = ['bar' => 'baz', 'operation_name' => 'get',  'resource_class' => 'Foo', 'request_uri' => '/foos/1', 'api_allow_update' => false, 'uri' => 'http://localhost/foos/1', 'output' => null, 'input' => null, 'iri_only' => false, 'skip_null_values' => true, 'operation' => $this->operation, 'exclude_from_cache_key' => ['root_operation', 'operation', 'object', 'data', 'property_metadata', 'circular_reference_limit_counters', 'debug_trace_id']];
×
81
        $this->assertEquals($expected, $this->builder->createFromRequest($request, false));
×
82

83
        $request = Request::create('/foos', 'POST');
×
84
        $request->attributes->replace(['_api_resource_class' => 'Foo', '_api_operation_name' => 'post', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
×
NEW
85
        $expected = ['bar' => 'baz', 'operation_name' => 'post',  'resource_class' => 'Foo', 'request_uri' => '/foos', 'api_allow_update' => false, 'uri' => 'http://localhost/foos', 'output' => null, 'input' => null, 'iri_only' => false, 'skip_null_values' => true, 'operation' => $this->operation->withName('post'), 'exclude_from_cache_key' => ['root_operation', 'operation', 'object', 'data', 'property_metadata', 'circular_reference_limit_counters', 'debug_trace_id']];
×
86
        $this->assertEquals($expected, $this->builder->createFromRequest($request, false));
×
87

88
        $request = Request::create('/foos', 'PUT');
×
89
        $request->attributes->replace(['_api_resource_class' => 'Foo', '_api_operation_name' => 'put', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
×
NEW
90
        $expected = ['bar' => 'baz', 'operation_name' => 'put', 'resource_class' => 'Foo', 'request_uri' => '/foos', 'api_allow_update' => true, 'uri' => 'http://localhost/foos', 'output' => null, 'input' => null, 'iri_only' => false, 'skip_null_values' => true, 'operation' => (new Put(name: 'put'))->withOperation($this->operation), 'exclude_from_cache_key' => ['root_operation', 'operation', 'object', 'data', 'property_metadata', 'circular_reference_limit_counters', 'debug_trace_id']];
×
91
        $this->assertEquals($expected, $this->builder->createFromRequest($request, false));
×
92

93
        $request = Request::create('/bars/1/foos');
×
94
        $request->attributes->replace(['_api_resource_class' => 'Foo', '_api_operation_name' => 'get', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
×
NEW
95
        $expected = ['bar' => 'baz', 'operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '/bars/1/foos', 'api_allow_update' => false, 'uri' => 'http://localhost/bars/1/foos', 'output' => null, 'input' => null, 'iri_only' => false, 'skip_null_values' => true, 'operation' => $this->operation, 'exclude_from_cache_key' => ['root_operation', 'operation', 'object', 'data', 'property_metadata', 'circular_reference_limit_counters', 'debug_trace_id']];
×
96
        $this->assertEquals($expected, $this->builder->createFromRequest($request, false));
×
97

98
        $request = Request::create('/foowithpatch/1', 'PATCH');
×
99
        $request->attributes->replace(['_api_resource_class' => 'FooWithPatch', '_api_operation_name' => 'patch', '_api_format' => 'json', '_api_mime_type' => 'application/json']);
×
NEW
100
        $expected = ['operation_name' => 'patch', 'resource_class' => 'FooWithPatch', 'request_uri' => '/foowithpatch/1', 'api_allow_update' => true, 'uri' => 'http://localhost/foowithpatch/1', 'output' => null, 'input' => null, 'deep_object_to_populate' => true, 'skip_null_values' => true, 'iri_only' => false, 'operation' => $this->patchOperation, 'exclude_from_cache_key' => ['root_operation', 'operation', 'object', 'data', 'property_metadata', 'circular_reference_limit_counters', 'debug_trace_id']];
×
101
        $this->assertEquals($expected, $this->builder->createFromRequest($request, false));
×
102

103
        $request = Request::create('/bars/1/foos');
×
104
        $request->attributes->replace(['_api_resource_class' => 'Foo', '_api_operation_name' => 'get', '_api_format' => 'xml', '_api_mime_type' => 'text/xml', 'id' => '1']);
×
NEW
105
        $expected = ['bar' => 'baz', 'operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '/bars/1/foos', 'api_allow_update' => false, 'uri' => 'http://localhost/bars/1/foos', 'output' => null, 'input' => null, 'iri_only' => false, 'operation' => $this->operation, 'skip_null_values' => true, 'exclude_from_cache_key' => ['root_operation', 'operation', 'object', 'data', 'property_metadata', 'circular_reference_limit_counters', 'debug_trace_id']];
×
106
        $this->assertEquals($expected, $this->builder->createFromRequest($request, false));
×
107
    }
108

109
    public function testThrowExceptionOnInvalidRequest(): void
110
    {
111
        $this->expectException(RuntimeException::class);
×
112

113
        $this->builder->createFromRequest(new Request(), false);
×
114
    }
115

116
    public function testReuseExistingAttributes(): void
117
    {
NEW
118
        $expected = ['bar' => 'baz', 'operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '/foos/1', 'api_allow_update' => false, 'uri' => 'http://localhost/foos/1', 'output' => null, 'input' => null, 'iri_only' => false, 'skip_null_values' => true, 'operation' => $this->operation, 'exclude_from_cache_key' => ['root_operation', 'operation', 'object', 'data', 'property_metadata', 'circular_reference_limit_counters', 'debug_trace_id']];
×
119
        $this->assertEquals($expected, $this->builder->createFromRequest(Request::create('/foos/1'), false, ['resource_class' => 'Foo', 'operation_name' => 'get']));
×
120
    }
121

122
    public function testCreateFromRequestKeyCollectDenormalizationErrorsIsInContext(): void
123
    {
124
        $operationWithCollectDenormalizationErrors = $this->operation->withCollectDenormalizationErrors(true);
×
125
        $request = Request::create('/foos', 'POST');
×
126
        $request->attributes->replace(['_api_resource_class' => 'Foo', '_api_operation_name' => 'post', '_api_format' => 'xml', '_api_mime_type' => 'text/xml', '_api_operation' => $operationWithCollectDenormalizationErrors]);
×
127
        $serializerContext = $this->builder->createFromRequest($request, false);
×
128
        $this->assertArrayHasKey('collect_denormalization_errors', $serializerContext);
×
129
        $this->assertTrue($serializerContext['collect_denormalization_errors']);
×
130
    }
131
}
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

© 2025 Coveralls, Inc