Failed Conditions
Push — issue#702 ( d312bf...92afd8 )
by Guilherme
06:53
created

AgeValidatorTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 6
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\ValidationBundle\Tests\Validator\Constraints;
12
13
use LoginCidadao\ValidationBundle\Validator\Constraints\Age;
14
use LoginCidadao\ValidationBundle\Validator\Constraints\AgeValidator;
15
use PHPUnit\Framework\TestCase;
16
17
class AgeValidatorTest extends TestCase
18
{
19
    const MIN_AGE = 18;
20
    const MAX_AGE = 150;
21
22
    public function testFutureDate()
23
    {
24
        $context = $this->getContext($this->getConstraint()->futureError);
25
26
        $this->validate($context, new \DateTime('+1 day'));
27
    }
28
29
    public function testMinAge()
30
    {
31
        $context = $this->getContext($this->getConstraint()->minError);
32
33
        $age = (self::MIN_AGE * 12) - 1;
34
        $this->validate($context, new \DateTime("-{$age} months"));
35
    }
36
37
    public function testMaxAge()
38
    {
39
        $context = $this->getContext($this->getConstraint()->maxError);
40
41
        $age = (self::MAX_AGE * 12) + 1;
42
        $this->validate($context, new \DateTime("-{$age} months"));
43
    }
44
45
    private function getContext($expectedMessage)
46
    {
47
        $builder = $this->getMockBuilder('Symfony\Component\Validator\Violation\ConstraintViolationBuilder')
48
            ->disableOriginalConstructor()
49
            ->setMethods(['addViolation'])
50
            ->getMock();
51
52
        $context = $this->getMockBuilder('Symfony\Component\Validator\Context\ExecutionContext')
53
            ->disableOriginalConstructor()
54
            ->setMethods(['buildViolation'])
55
            ->getMock();
56
        $context->expects($this->once())
57
            ->method('buildViolation')
58
            ->with($this->equalTo($expectedMessage))
59
            ->willReturn($builder);
60
61
        return $context;
62
    }
63
64
    private function getConstraint()
65
    {
66
        $constraint = new Age();
67
        $constraint->min = self::MIN_AGE;
68
        $constraint->max = self::MAX_AGE;
69
70
        return $constraint;
71
    }
72
73
    private function validate($context, $date)
74
    {
75
        $validator = new AgeValidator();
76
        $validator->initialize($context);
77
        $validator->validate($date, $this->getConstraint());
78
    }
79
}
80