EnumValidatorTest::testAcceptOnlyEnum()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace Yokai\EnumBundle\Tests\Validator\Constraints;
4
5
use Prophecy\PhpUnit\ProphecyTrait;
6
use Prophecy\Prophecy\ObjectProphecy;
7
use Symfony\Component\Validator\Constraints\Choice;
8
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
9
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
10
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
11
use Yokai\EnumBundle\EnumRegistry;
12
use Yokai\EnumBundle\Tests\Fixtures\GenderEnum;
13
use Yokai\EnumBundle\Tests\Fixtures\TypeEnum;
14
use Yokai\EnumBundle\Validator\Constraints\Enum;
15
use Yokai\EnumBundle\Validator\Constraints\EnumValidator;
16
17
/**
18
 * @author Yann Eugoné <[email protected]>
19
 */
20
class EnumValidatorTest extends ConstraintValidatorTestCase
21
{
22
    use ProphecyTrait;
23
24
    protected function createValidator(): EnumValidator
25
    {
26
        /** @var EnumRegistry|ObjectProphecy $registry */
27
        $registry = $this->prophesize(EnumRegistry::class);
28
        $registry->has('state')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method has does only exist in Yokai\EnumBundle\EnumRegistry, but not in Prophecy\Prophecy\ObjectProphecy.

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...
29
        $registry->has(GenderEnum::class)->willReturn(true);
30
        $registry->has('type')->willReturn(true);
31
        $registry->get('type')->willReturn(new TypeEnum);
0 ignored issues
show
Bug introduced by
The method get does only exist in Yokai\EnumBundle\EnumRegistry, but not in Prophecy\Prophecy\ObjectProphecy.

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...
32
33
        return new EnumValidator($registry->reveal());
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Yokai\EnumBundle\EnumRegistry.

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...
34
    }
35
36
    public function testAcceptOnlyEnum(): void
37
    {
38
        $this->expectException(UnexpectedTypeException::class);
39
        $this->validator->validate(null, new Choice);
40
    }
41
42
    public function testEnumIsRequired(): void
43
    {
44
        $this->expectException(ConstraintDefinitionException::class);
45
        $this->validator->validate('foo', new Enum);
46
    }
47
48
    public function testValidEnumIsRequired(): void
49
    {
50
        $this->expectException(ConstraintDefinitionException::class);
51
        $this->validator->validate('foo', new Enum('state'));
52
    }
53
54
    public function testNullIsValid(): void
55
    {
56
        $this->validator->validate(null, new Enum('type'));
57
58
        $this->assertNoViolation();
59
    }
60
61
    public function testValidSingleEnum(): void
62
    {
63
        $this->validator->validate('customer', new Enum('type'));
64
65
        $this->assertNoViolation();
66
    }
67
68
    public function testInvalidSingleEnum(): void
69
    {
70
        $constraint = new Enum(['enum' => 'type', 'message' => 'myMessage']);
71
72
        $this->validator->validate('foo', $constraint);
73
74
        $this->buildViolation('myMessage')
75
            ->setParameter('{{ value }}', '"foo"')
76
            ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
77
            ->setParameter('{{ choices }}', '"customer", "prospect"')
78
            ->assertRaised();
79
    }
80
81
    public function testValidMultipleEnum(): void
82
    {
83
        $constraint = new Enum(['enum' => 'type', 'multiple' => true]);
84
85
        $this->validator->validate(['customer', 'prospect'], $constraint);
86
87
        $this->assertNoViolation();
88
    }
89
90
    public function testInvalidMultipleEnum(): void
91
    {
92
        $constraint = new Enum(['enum' => 'type', 'multiple' => true, 'multipleMessage' => 'myMessage']);
93
94
        $this->validator->validate(['customer', 'foo'], $constraint);
95
96
        $this->buildViolation('myMessage')
97
            ->setParameter('{{ value }}', '"foo"')
98
            ->setInvalidValue('foo')
99
            ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
100
            ->setParameter('{{ choices }}', '"customer", "prospect"')
101
            ->assertRaised();
102
    }
103
}
104