Completed
Push — issue#736 ( 78f04a )
by Guilherme
19:20
created

AgeValidatorTest::validate()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 9.4285
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 Symfony\Component\Validator\Constraints\Url;
16
17
class AgeValidatorTest extends \PHPUnit_Framework_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
    /**
46
     * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
47
     */
48
    public function testExpectsDateTime()
49
    {
50
        $this->validate($this->getContext(), new \stdClass());
51
    }
52
53
    /**
54
     * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
55
     */
56
    public function testExpectsAgeConstraint()
57
    {
58
        $this->validate($this->getContext(), new \DateTime(), new Url());
0 ignored issues
show
Documentation introduced by
new \Symfony\Component\Validator\Constraints\Url() is of type object<Symfony\Component...idator\Constraints\Url>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
    }
60
61
    private function getContext($expectedMessage = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
62
    {
63
        $builder = $this->getMockBuilder('Symfony\Component\Validator\Violation\ConstraintViolationBuilder')
64
            ->disableOriginalConstructor()
65
            ->setMethods(['addViolation'])
66
            ->getMock();
67
68
        $context = $this->getMockBuilder('Symfony\Component\Validator\Context\ExecutionContext')
69
            ->disableOriginalConstructor()
70
            ->setMethods(['buildViolation'])
71
            ->getMock();
72
        if ($expectedMessage !== null) {
73
            $context->expects($this->once())
74
                ->method('buildViolation')
75
                ->with($this->equalTo($expectedMessage))
76
                ->willReturn($builder);
77
        }
78
79
        return $context;
80
    }
81
82
    private function getConstraint()
83
    {
84
        $constraint = new Age();
85
        $constraint->min = self::MIN_AGE;
86
        $constraint->max = self::MAX_AGE;
87
88
        return $constraint;
89
    }
90
91
    private function validate($context, $date, $constraint = false)
92
    {
93
        $validator = new AgeValidator();
94
        $validator->initialize($context);
95
        $validator->validate($date, $constraint ?: $this->getConstraint());
0 ignored issues
show
Bug introduced by
It seems like $constraint ?: $this->getConstraint() can also be of type boolean; however, LoginCidadao\ValidationB...geValidator::validate() does only seem to accept object<Symfony\Component\Validator\Constraint>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
96
    }
97
}
98