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

api-platform / core / 10014117656

19 Jul 2024 08:44PM UTC coverage: 7.856% (-56.3%) from 64.185%
10014117656

push

github

soyuka
Merge branch 'sf/remove-flag'

0 of 527 new or added lines in 83 files covered. (0.0%)

10505 existing lines in 362 files now uncovered.

12705 of 161727 relevant lines covered (7.86%)

26.85 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\AdvancedNameConverterInterface;
21
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
22
use Symfony\Component\Validator\Constraints\NotNull;
23
use Symfony\Component\Validator\ConstraintViolation;
24
use Symfony\Component\Validator\ConstraintViolationList;
25
use Symfony\Component\Validator\ConstraintViolationListInterface;
26

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

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

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

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

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

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

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

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

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

92
        $advancedNameConverterFactory = function (self $that) {
×
93
            $advancedNameConverterProphecy = $that->prophesize(AdvancedNameConverterInterface::class);
×
94
            $advancedNameConverterProphecy->normalize(Argument::type('string'), null, Argument::type('string'))->will(fn ($args): string => '_'.$args[0]);
×
95

96
            return $advancedNameConverterProphecy->reveal();
×
97
        };
×
98

99
        $nameConverterFactory = function (self $that) {
×
100
            $nameConverterProphecy = $that->prophesize(NameConverterInterface::class);
×
101
            $nameConverterProphecy->normalize(Argument::type('string'))->will(fn ($args): string => '_'.$args[0]);
×
102

103
            return $nameConverterProphecy->reveal();
×
104
        };
×
105

106
        $nullNameConverterFactory = fn () => null;
×
107

108
        $expected = $nameConverterBasedExpectation;
×
NEW
109
        $expected[0]['payload'] = ['severity' => 'warning'];
×
110
        yield [$advancedNameConverterFactory, ['severity', 'anotherField1'], $expected];
×
111
        yield [$nameConverterFactory, ['severity', 'anotherField1'], $expected];
×
112
        $expected = $basicExpectation;
×
NEW
113
        $expected[0]['payload'] = ['severity' => 'warning'];
×
114
        yield [$nullNameConverterFactory, ['severity', 'anotherField1'], $expected];
×
115

116
        $expected = $nameConverterBasedExpectation;
×
NEW
117
        $expected[0]['payload'] = ['severity' => 'warning', 'anotherField2' => 'aValue'];
×
118
        yield [$advancedNameConverterFactory, null, $expected];
×
119
        yield [$nameConverterFactory, null, $expected];
×
120
        $expected = $basicExpectation;
×
NEW
121
        $expected[0]['payload'] = ['severity' => 'warning', 'anotherField2' => 'aValue'];
×
122
        yield [$nullNameConverterFactory, null, $expected];
×
123

124
        yield [$advancedNameConverterFactory, [], $nameConverterBasedExpectation];
×
125
        yield [$nameConverterFactory, [], $nameConverterBasedExpectation];
×
126
        yield [$nullNameConverterFactory, [], $basicExpectation];
×
127
    }
128
}
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