Passed
Push — release/v2 ( 119cbe...817cd6 )
by Benoit
02:50
created

LengthFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 5
1
<?php
2
3
namespace JBen87\ParsleyBundle\Tests\Constraint\Factory;
4
5
use JBen87\ParsleyBundle\Constraint\Constraints as ParsleyAssert;
6
use JBen87\ParsleyBundle\Constraint\Factory\FactoryInterface;
7
use JBen87\ParsleyBundle\Constraint\Factory\LengthFactory;
8
use Symfony\Component\Validator\Constraint as SymfonyConstraint;
9
use Symfony\Component\Validator\Constraints as Assert;
10
11
class LengthFactoryTest extends FactoryTestCase
12
{
13
    private const MIN = 5;
14
    private const MAX = 10;
15
    private const LIMIT = 10;
16
    private const ORIGINAL_MESSAGE = 'This value should have {{ min }} to {{ max }} characters.';
17
    private const TRANSLATED_MESSAGE = 'This value should have '.self::MIN.' to '.self::MIN.' characters.';
18
    private const ORIGINAL_EXACT_MESSAGE = 'This value should have exactly {{ limit }} character.|This value should have exactly {{ limit }} characters.';
19
    private const TRANSLATED_EXACT_MESSAGE = 'This value should have exactly '.self::LIMIT.' characters.';
20
21
    /**
22
     * @inheritdoc
23
     */
24
    public function provideCreate(): array
25
    {
26
        return [
27
            [
28
                new ParsleyAssert\Length([
29
                    'min' => static::MIN,
30
                    'max' => static::MAX,
31
                    'message' => static::TRANSLATED_MESSAGE,
32
                ]),
33
                new Assert\Length([
34
                    'min' => static::MIN,
35
                    'max' => static::MAX,
36
                ]),
37
                function (LengthFactoryTest $self): void {
38
                    $self->translator
39
                        ->expects($this->once())
40
                        ->method('trans')
41
                        ->with(
42
                            static::ORIGINAL_MESSAGE,
43
                            ['{{ min }}' => static::MIN, '{{ max }}' => static::MAX]
44
                        )
45
                        ->willReturn(static::TRANSLATED_MESSAGE)
46
                    ;
47
                },
48
            ],
49
            [
50
                new ParsleyAssert\Length([
51
                    'min' => static::LIMIT,
52
                    'max' => static::LIMIT,
53
                    'message' => static::TRANSLATED_EXACT_MESSAGE,
54
                ]),
55
                new Assert\Length([
56
                    'min' => static::LIMIT,
57
                    'max' => static::LIMIT,
58
                ]),
59
                function (LengthFactoryTest $self): void {
60
                    $self->translator
61
                        ->expects($this->once())
62
                        ->method('trans')
63
                        ->with(
64
                            static::ORIGINAL_MESSAGE,
65
                            ['{{ min }}' => static::LIMIT, '{{ max }}' => static::LIMIT]
66
                        )
67
                        ->willReturn(sprintf('This value should have %d to %d characters.', static::MAX, static::MIN))
68
                    ;
69
70
                    $self->translator
71
                        ->expects($this->once())
72
                        ->method('transChoice')
73
                        ->with(
74
                            static::ORIGINAL_EXACT_MESSAGE,
75
                            static::LIMIT,
76
                            ['{{ limit }}' => static::LIMIT]
77
                        )
78
                        ->willReturn(static::TRANSLATED_EXACT_MESSAGE)
79
                    ;
80
                },
81
            ],
82
        ];
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    protected function setUpCreate(): void
89
    {
90
        $this->translator
91
            ->expects($this->once())
92
            ->method('trans')
93
            ->with(static::ORIGINAL_MESSAGE, ['{{ min }}' => static::MIN, '{{ max }}' => static::MAX])
94
            ->willReturn(static::TRANSLATED_MESSAGE)
95
        ;
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    protected function getOriginalConstraint(): SymfonyConstraint
102
    {
103
        return new Assert\Length([
104
            'min' => static::MIN,
105
            'max' => static::MAX,
106
        ]);
107
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112
    protected function getUnsupportedConstraint(): SymfonyConstraint
113
    {
114
        return new Assert\Valid();
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120
    protected function createFactory(): FactoryInterface
121
    {
122
        return new LengthFactory();
123
    }
124
}
125