Completed
Push — master ( 6f6131...7b9880 )
by Artem
02:11
created

Tests/Validator/EnumValidatorTest.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * This file is part of the FreshDoctrineEnumBundle
4
 *
5
 * (c) Artem Genvald <[email protected]>
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 Fresh\DoctrineEnumBundle\Tests\Validator;
12
13
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType;
14
use Fresh\DoctrineEnumBundle\Validator\Constraints\Enum;
15
use Fresh\DoctrineEnumBundle\Validator\Constraints\EnumValidator;
16
use Symfony\Component\Validator\Context\ExecutionContext;
17
18
/**
19
 * EnumValidatorTest.
20
 *
21
 * @author Artem Genvald <[email protected]>
22
 */
23
class EnumValidatorTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * @var EnumValidator $enumValidator ENUM validator
27
     */
28
    private $enumValidator;
29
30
    /**
31
     * @var ExecutionContext|\PHPUnit_Framework_MockObject_MockObject $context Context
32
     */
33
    private $context;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function setUp()
39
    {
40
        $this->enumValidator = new EnumValidator();
41
42
        $this->context = $this->getMockBuilder('Symfony\Component\Validator\Context\ExecutionContext')
43
                              ->disableOriginalConstructor()
44
                              ->getMock();
45
    }
46
47
    /**
48
     * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
49
     */
50
    public function testExceptionEntityNotSpecified()
51
    {
52
        $constraint = new Enum([
53
            'entity' => null,
54
        ]);
55
56
        $this->enumValidator->validate(BasketballPositionType::POINT_GUARD, $constraint);
57
    }
58
59
    public function testValidBasketballPositionType()
60
    {
61
        $constraint = new Enum([
62
            'entity' => 'Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType',
63
        ]);
64
65
        $this->context->expects($this->never())
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Valida...ontext\ExecutionContext.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
66
                      ->method('buildViolation');
67
68
        $this->enumValidator->initialize($this->context);
69
        $this->enumValidator->validate(BasketballPositionType::SMALL_FORWARD, $constraint);
70
    }
71
72
    public function testInvalidBasketballPositionType()
73
    {
74
        $constraint = new Enum([
75
            'entity' => 'Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType',
76
        ]);
77
78
        $constraintValidationBuilder = $this->getMockBuilder('Symfony\Component\Validator\Violation\ConstraintViolationBuilder')
79
                                            ->disableOriginalConstructor()
80
                                            ->getMock();
81
82
        $constraintValidationBuilder->expects($this->once())
83
                                    ->method('setParameter')
84
                                    ->with($this->equalTo('{{ value }}'), $this->equalTo('"Pitcher"'))
85
                                    ->will($this->returnSelf());
86
87
        $constraintValidationBuilder->expects($this->once())
88
                                    ->method('setCode')
89
                                    ->will($this->returnSelf());
90
91
        $this->context->expects($this->once())
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Valida...ontext\ExecutionContext.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
92
                      ->method('buildViolation')
93
                      ->with($this->equalTo('The value you selected is not a valid choice.'))
94
                      ->will($this->returnValue($constraintValidationBuilder));
95
96
        $this->enumValidator->initialize($this->context);
97
        $this->enumValidator->validate('Pitcher', $constraint); // It's not a baseball =)
98
    }
99
}
100