• 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
/tests/Symfony/Validator/ValidatorTest.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\Tests\Symfony\Validator;
15

16
use ApiPlatform\Symfony\Validator\ValidationGroupsGeneratorInterface;
17
use ApiPlatform\Symfony\Validator\Validator;
18
use ApiPlatform\Tests\Fixtures\DummyEntity;
19
use ApiPlatform\Validator\Exception\ValidationException;
20
use PHPUnit\Framework\TestCase;
21
use Prophecy\PhpUnit\ProphecyTrait;
22
use Psr\Container\ContainerInterface;
23
use Symfony\Component\Validator\ConstraintViolation;
24
use Symfony\Component\Validator\ConstraintViolationList;
25
use Symfony\Component\Validator\ConstraintViolationListInterface;
26
use Symfony\Component\Validator\Validator\ValidatorInterface as SymfonyValidatorInterface;
27

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

35
    public function testValid(): void
36
    {
37
        $data = new DummyEntity();
×
38

39
        $constraintViolationListProphecy = $this->prophesize(ConstraintViolationListInterface::class);
×
40
        $constraintViolationListProphecy->count()->willReturn(0);
×
41

42
        $symfonyValidatorProphecy = $this->prophesize(SymfonyValidatorInterface::class);
×
43
        $symfonyValidatorProphecy->validate($data, null, null)->willReturn($constraintViolationListProphecy->reveal())->shouldBeCalled();
×
44
        $symfonyValidator = $symfonyValidatorProphecy->reveal();
×
45

NEW
46
        $validator = new Validator($symfonyValidator);
×
47
        $validator->validate(new DummyEntity());
×
48
    }
49

50
    public function testInvalid(): void
51
    {
52
        $this->expectException(ValidationException::class);
×
53

54
        $data = new DummyEntity();
×
55
        $constraintViolationList = new ConstraintViolationList([new ConstraintViolation('test', null, [], null, 'test', null), new ConstraintViolation('test', null, [], null, 'test', null)]);
×
56

57
        $symfonyValidatorProphecy = $this->prophesize(SymfonyValidatorInterface::class);
×
58
        $symfonyValidatorProphecy->validate($data, null, null)->willReturn($constraintViolationList)->shouldBeCalled();
×
59
        $symfonyValidator = $symfonyValidatorProphecy->reveal();
×
60

NEW
61
        $validator = new Validator($symfonyValidator);
×
62
        $validator->validate(new DummyEntity());
×
63
    }
64

65
    public function testGetGroupsFromCallable(): void
66
    {
67
        $data = new DummyEntity();
×
68
        $expectedValidationGroups = ['a', 'b', 'c'];
×
69

70
        $constraintViolationListProphecy = $this->prophesize(ConstraintViolationListInterface::class);
×
71
        $constraintViolationListProphecy->count()->willReturn(0);
×
72

73
        $symfonyValidatorProphecy = $this->prophesize(SymfonyValidatorInterface::class);
×
74
        $symfonyValidatorProphecy->validate($data, null, $expectedValidationGroups)->willReturn($constraintViolationListProphecy->reveal())->shouldBeCalled();
×
75
        $symfonyValidator = $symfonyValidatorProphecy->reveal();
×
76

NEW
77
        $validator = new Validator($symfonyValidator);
×
78
        $validator->validate(new DummyEntity(), ['groups' => fn ($data): array => $data instanceof DummyEntity ? $expectedValidationGroups : []]);
×
79
    }
80

81
    public function testValidateGetGroupsFromService(): void
82
    {
83
        $data = new DummyEntity();
×
84

85
        $constraintViolationListProphecy = $this->prophesize(ConstraintViolationListInterface::class);
×
86
        $constraintViolationListProphecy->count()->willReturn(0);
×
87

88
        $symfonyValidatorProphecy = $this->prophesize(SymfonyValidatorInterface::class);
×
89
        $symfonyValidatorProphecy->validate($data, null, ['a', 'b', 'c'])->willReturn($constraintViolationListProphecy)->shouldBeCalled();
×
90

91
        $containerProphecy = $this->prophesize(ContainerInterface::class);
×
92
        $containerProphecy->has('groups_builder')->willReturn(true);
×
93
        $containerProphecy->get('groups_builder')->willReturn(new class() implements ValidationGroupsGeneratorInterface {
×
94
            public function __invoke(object $object): array
95
            {
96
                return $object instanceof DummyEntity ? ['a', 'b', 'c'] : [];
×
97
            }
98
        });
×
99

NEW
100
        $validator = new Validator($symfonyValidatorProphecy->reveal(), $containerProphecy->reveal());
×
101
        $validator->validate(new DummyEntity(), ['groups' => 'groups_builder']);
×
102
    }
103

104
    public function testValidatorWithScalarGroup(): void
105
    {
106
        $data = new DummyEntity();
×
107
        $expectedValidationGroups = ['foo'];
×
108

109
        $constraintViolationListProphecy = $this->prophesize(ConstraintViolationListInterface::class);
×
110
        $constraintViolationListProphecy->count()->willReturn(0);
×
111

112
        $symfonyValidatorProphecy = $this->prophesize(SymfonyValidatorInterface::class);
×
113
        $symfonyValidatorProphecy->validate($data, null, $expectedValidationGroups)->willreturn($constraintViolationListProphecy->reveal())->shouldBeCalled();
×
114
        $symfonyValidator = $symfonyValidatorProphecy->reveal();
×
115

116
        $containerProphecy = $this->prophesize(ContainerInterface::class);
×
117
        $containerProphecy->has('foo')->willReturn(false)->shouldBeCalled();
×
118

NEW
119
        $validator = new Validator($symfonyValidator, $containerProphecy->reveal());
×
120
        $validator->validate(new DummyEntity(), ['groups' => 'foo']);
×
121
    }
122
}
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