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

api-platform / core / 19799380020

30 Nov 2025 01:09PM UTC coverage: 0.0%. Remained the same
19799380020

push

github

soyuka
Merge 4.2

0 of 306 new or added lines in 35 files covered. (0.0%)

22 existing lines in 15 files now uncovered.

0 of 57173 relevant lines covered (0.0%)

0.0 hits per line

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

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

16
use ApiPlatform\Hydra\Serializer\ConstraintViolationListNormalizer;
17
use PHPUnit\Framework\TestCase;
18
use Prophecy\Argument;
19
use Prophecy\PhpUnit\ProphecyTrait;
20
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
21
use Symfony\Component\Validator\Constraints\NotNull;
22
use Symfony\Component\Validator\ConstraintViolation;
23
use Symfony\Component\Validator\ConstraintViolationList;
24
use Symfony\Component\Validator\ConstraintViolationListInterface;
25

26
/**
27
 * @author Kévin Dunglas <dunglas@gmail.com>
28
 */
29
class ConstraintViolationNormalizerTest extends TestCase
30
{
31
    use ProphecyTrait;
32

33
    public function testSupportNormalization(): void
34
    {
35
        $nameConverterProphecy = $this->prophesize(NameConverterInterface::class);
×
36

37
        $normalizer = new ConstraintViolationListNormalizer([], $nameConverterProphecy->reveal());
×
38

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

47
    #[\PHPUnit\Framework\Attributes\DataProvider('nameConverterAndPayloadFieldsProvider')]
48
    public function testNormalize(callable $nameConverterFactory, ?array $fields, array $expected): void
49
    {
50
        $normalizer = new ConstraintViolationListNormalizer($fields, $nameConverterFactory($this));
×
51

52
        // Note : we use NotNull constraint and not Constraint class because Constraint is abstract
53
        $constraint = new NotNull();
×
54
        $constraint->payload = ['severity' => 'warning', 'anotherField2' => 'aValue'];
×
55
        $list = new ConstraintViolationList([
×
56
            new ConstraintViolation('a', 'b', [], 'c', 'd', 'e', null, 'f24bdbad0becef97a6887238aa58221c', $constraint),
×
57
            new ConstraintViolation('1', '2', [], '3', '4', '5'),
×
58
        ]);
×
59

60
        $this->assertSame($expected, $normalizer->normalize($list));
×
61
    }
62

63
    public static function nameConverterAndPayloadFieldsProvider(): iterable
64
    {
65
        $basicExpectation = [
×
66
            [
×
67
                'propertyPath' => 'd',
×
68
                'message' => 'a',
×
69
                'code' => 'f24bdbad0becef97a6887238aa58221c',
×
70
            ],
×
71
            [
×
72
                'propertyPath' => '4',
×
73
                'message' => '1',
×
74
                'code' => null,
×
75
            ],
×
76
        ];
×
77

78
        $nameConverterBasedExpectation = [
×
79
            [
×
80
                'propertyPath' => '_d',
×
81
                'message' => 'a',
×
82
                'code' => 'f24bdbad0becef97a6887238aa58221c',
×
83
            ],
×
84
            [
×
85
                'propertyPath' => '_4',
×
86
                'message' => '1',
×
87
                'code' => null,
×
88
            ],
×
89
        ];
×
90

91
        $nameConverterFactory = function (self $that) {
×
92
            $nameConverterProphecy = $that->prophesize(NameConverterInterface::class);
×
NEW
93
            $nameConverterProphecy->normalize(Argument::cetera())->will(fn ($args): string => '_'.$args[0]);
×
94

95
            return $nameConverterProphecy->reveal();
×
96
        };
×
97

98
        $nullNameConverterFactory = fn () => null;
×
99

100
        $expected = $nameConverterBasedExpectation;
×
101
        $expected[0]['payload'] = ['severity' => 'warning'];
×
102
        yield [$nameConverterFactory, ['severity', 'anotherField1'], $expected];
×
103
        $expected = $basicExpectation;
×
104
        $expected[0]['payload'] = ['severity' => 'warning'];
×
105
        yield [$nullNameConverterFactory, ['severity', 'anotherField1'], $expected];
×
106

107
        $expected = $nameConverterBasedExpectation;
×
108
        $expected[0]['payload'] = ['severity' => 'warning', 'anotherField2' => 'aValue'];
×
109
        yield [$nameConverterFactory, null, $expected];
×
110
        $expected = $basicExpectation;
×
111
        $expected[0]['payload'] = ['severity' => 'warning', 'anotherField2' => 'aValue'];
×
112
        yield [$nullNameConverterFactory, null, $expected];
×
113

114
        yield [$nameConverterFactory, [], $nameConverterBasedExpectation];
×
115
        yield [$nullNameConverterFactory, [], $basicExpectation];
×
116
    }
117
}
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