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

api-platform / core / 17054099799

18 Aug 2025 10:28PM UTC coverage: 22.386% (-0.2%) from 22.612%
17054099799

Pull #7150

github

web-flow
Merge 973c71211 into 2d501b315
Pull Request #7150: fix: array shape in ProviderInterface

11062 of 49414 relevant lines covered (22.39%)

11.75 hits per line

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

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

16
use ApiPlatform\JsonApi\Serializer\ConstraintViolationListNormalizer;
17
use ApiPlatform\JsonApi\Tests\Fixtures\Dummy;
18
use ApiPlatform\JsonApi\Tests\Fixtures\RelatedDummy;
19
use ApiPlatform\Metadata\ApiProperty;
20
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21
use PHPUnit\Framework\TestCase;
22
use Prophecy\PhpUnit\ProphecyTrait;
23
use Symfony\Component\PropertyInfo\Type;
24
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
25
use Symfony\Component\Validator\ConstraintViolation;
26
use Symfony\Component\Validator\ConstraintViolationList;
27
use Symfony\Component\Validator\ConstraintViolationListInterface;
28

29
/**
30
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
31
 */
32
class ConstraintViolationNormalizerTest extends TestCase
33
{
34
    use ProphecyTrait;
35

36
    public function testSupportNormalization(): void
37
    {
38
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
39
        $nameConverterInterface = $this->prophesize(NameConverterInterface::class);
×
40

41
        $normalizer = new ConstraintViolationListNormalizer($propertyMetadataFactoryProphecy->reveal(), $nameConverterInterface->reveal());
×
42

43
        $this->assertTrue($normalizer->supportsNormalization(new ConstraintViolationList(), ConstraintViolationListNormalizer::FORMAT));
×
44
        $this->assertFalse($normalizer->supportsNormalization(new ConstraintViolationList(), 'xml'));
×
45
        $this->assertFalse($normalizer->supportsNormalization(new \stdClass(), ConstraintViolationListNormalizer::FORMAT));
×
46
        $this->assertEmpty($normalizer->getSupportedTypes('json'));
×
47
        $this->assertSame([ConstraintViolationListInterface::class => true], $normalizer->getSupportedTypes($normalizer::FORMAT));
×
48
    }
49

50
    public function testNormalize(): void
51
    {
52
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
53
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy')->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class)]))->shouldBeCalledTimes(1);
×
54
        $propertyMetadataFactoryProphecy->create(Dummy::class, 'name')->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)]))->shouldBeCalledTimes(1);
×
55

56
        $nameConverterProphecy = $this->prophesize(NameConverterInterface::class);
×
57
        $nameConverterProphecy->normalize('relatedDummy', Dummy::class, 'jsonapi')->willReturn('relatedDummy')->shouldBeCalledTimes(1);
×
58
        $nameConverterProphecy->normalize('name', Dummy::class, 'jsonapi')->willReturn('name')->shouldBeCalledTimes(1);
×
59

60
        $dummy = new Dummy();
×
61

62
        $constraintViolationList = new ConstraintViolationList([
×
63
            new ConstraintViolation('This value should not be null.', 'This value should not be null.', [], $dummy, 'relatedDummy', null),
×
64
            new ConstraintViolation('This value should not be null.', 'This value should not be null.', [], $dummy, 'name', null),
×
65
            new ConstraintViolation('Unknown violation.', 'Unknown violation.', [], $dummy, '', ''),
×
66
        ]);
×
67

68
        $this->assertEquals(
×
69
            [
×
70
                'errors' => [
×
71
                    [
×
72
                        'detail' => 'This value should not be null.',
×
73
                        'source' => [
×
74
                            'pointer' => 'data/relationships/relatedDummy',
×
75
                        ],
×
76
                    ],
×
77
                    [
×
78
                        'detail' => 'This value should not be null.',
×
79
                        'source' => [
×
80
                            'pointer' => 'data/attributes/name',
×
81
                        ],
×
82
                    ],
×
83
                    [
×
84
                        'detail' => 'Unknown violation.',
×
85
                        'source' => [
×
86
                            'pointer' => 'data',
×
87
                        ],
×
88
                    ],
×
89
                ],
×
90
            ],
×
91
            (new ConstraintViolationListNormalizer($propertyMetadataFactoryProphecy->reveal(), $nameConverterProphecy->reveal()))->normalize($constraintViolationList)
×
92
        );
×
93
    }
94

95
    public function testNormalizeWithStringRoot(): void
96
    {
97
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
98

99
        // Create a violation with a string root (simulating query parameter validation)
100
        $constraintViolationList = new ConstraintViolationList([
×
101
            new ConstraintViolation('Invalid page value.', 'Invalid page value.', [], 'page', 'page', 'invalid'),
×
102
        ]);
×
103

104
        $normalizer = new ConstraintViolationListNormalizer($propertyMetadataFactoryProphecy->reveal());
×
105

106
        $result = $normalizer->normalize($constraintViolationList);
×
107

108
        $this->assertEquals(
×
109
            [
×
110
                'errors' => [
×
111
                    [
×
112
                        'detail' => 'Invalid page value.',
×
113
                        'source' => [
×
114
                            'pointer' => 'data/attributes/page',
×
115
                        ],
×
116
                    ],
×
117
                ],
×
118
            ],
×
119
            $result
×
120
        );
×
121
    }
122

123
    public function testNormalizeWithNullRoot(): void
124
    {
125
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
126

127
        // Create a violation with a null root
128
        $constraintViolationList = new ConstraintViolationList([
×
129
            new ConstraintViolation('Invalid value.', 'Invalid value.', [], null, 'field', 'invalid'),
×
130
        ]);
×
131

132
        $normalizer = new ConstraintViolationListNormalizer($propertyMetadataFactoryProphecy->reveal());
×
133

134
        // This should not throw a TypeError and should handle the null root gracefully
135
        $result = $normalizer->normalize($constraintViolationList);
×
136

137
        $this->assertEquals(
×
138
            [
×
139
                'errors' => [
×
140
                    [
×
141
                        'detail' => 'Invalid value.',
×
142
                        'source' => [
×
143
                            'pointer' => 'data/attributes/field',
×
144
                        ],
×
145
                    ],
×
146
                ],
×
147
            ],
×
148
            $result
×
149
        );
×
150
    }
151
}
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