EnumTypeGuesserTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Yokai\EnumBundle\Tests\Form\Extension;
4
5
use Prophecy\PhpUnit\ProphecyTrait;
6
use Prophecy\Prophecy\ObjectProphecy;
7
use Symfony\Component\Form\Extension\Core\Type\FormType;
8
use Symfony\Component\Form\Guess\Guess;
9
use Symfony\Component\Form\Guess\TypeGuess;
10
use Symfony\Component\Form\Test\TypeTestCase;
11
use Symfony\Component\Validator\Mapping\ClassMetadata;
12
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
13
use Yokai\EnumBundle\EnumRegistry;
14
use Yokai\EnumBundle\Form\Extension\EnumTypeGuesser;
15
use Yokai\EnumBundle\Form\Type\EnumType;
16
use Yokai\EnumBundle\Tests\Fixtures\GenderEnum;
17
use Yokai\EnumBundle\Tests\Form\TestExtension;
18
use Yokai\EnumBundle\Validator\Constraints\Enum;
19
20
/**
21
 * @author Yann Eugoné <[email protected]>
22
 */
23
class EnumTypeGuesserTest extends TypeTestCase
24
{
25
    use ProphecyTrait;
26
27
    const TEST_CLASS = EnumTypeGuesserTest_TestClass::class;
28
29
    const TEST_PROPERTY = 'property';
30
31
    /**
32
     * @var EnumTypeGuesser
33
     */
34
    private $guesser;
35
36
    /**
37
     * @var ObjectProphecy|EnumRegistry
38
     */
39
    private $enumRegistry;
40
41
    /**
42
     * @var ClassMetadata
43
     */
44
    private $metadata;
45
46
    /**
47
     * @var ObjectProphecy|MetadataFactoryInterface
48
     */
49
    private $metadataFactory;
50
51
    protected function setUp(): void
52
    {
53
        $this->enumRegistry = $this->prophesize(EnumRegistry::class);
54
        $this->enumRegistry->has('state')->willReturn(false);
55
        $this->enumRegistry->has(GenderEnum::class)->willReturn(true);
56
        $this->enumRegistry->get(GenderEnum::class)->willReturn(new GenderEnum);
57
58
        $this->metadata = new ClassMetadata(self::TEST_CLASS);
59
        $this->metadata->addPropertyConstraint(self::TEST_PROPERTY, new Enum(['enum' => GenderEnum::class]));
60
        $this->metadataFactory = $this->prophesize(MetadataFactoryInterface::class);
61
        $this->metadataFactory->getMetadataFor(self::TEST_CLASS)
62
            ->willReturn($this->metadata);
63
64
        $this->guesser = new EnumTypeGuesser($this->metadataFactory->reveal(), $this->enumRegistry->reveal());
65
66
        parent::setUp();
67
    }
68
69
    public function testGuessType(): void
70
    {
71
        $guess = new TypeGuess(
72
            EnumType::class,
73
            [
74
                'enum' => GenderEnum::class,
75
                'multiple' => false,
76
            ],
77
            Guess::HIGH_CONFIDENCE
78
        );
79
80
        $this->assertEquals($guess, $this->guesser->guessType(self::TEST_CLASS, self::TEST_PROPERTY));
81
    }
82
83
    public function testGuessRequired(): void
84
    {
85
        $this->assertNull($this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY));
86
    }
87
88
    public function testGuessMaxLength(): void
89
    {
90
        $this->assertNull($this->guesser->guessMaxLength(self::TEST_CLASS, self::TEST_PROPERTY));
91
    }
92
93
    public function testGuessPattern(): void
94
    {
95
        $this->assertNull($this->guesser->guessPattern(self::TEST_CLASS, self::TEST_PROPERTY));
96
    }
97
98
    public function testCreateForm(): void
99
    {
100
        $class = self::TEST_CLASS;
101
        $form = $this->factory->create(FormType::class, new $class, ['data_class' => $class])
102
            ->add(self::TEST_PROPERTY);
103
104
        $this->assertEquals(
105
            ['Male' => 'male', 'Female' => 'female'],
106
            $form->get(self::TEST_PROPERTY)->getConfig()->getOption('choices')
107
        );
108
    }
109
110
    protected function getExtensions(): array
111
    {
112
        return [
113
            new TestExtension($this->enumRegistry->reveal(), $this->metadataFactory->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...
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Symfony\Component\Valida...etadataFactoryInterface.

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...
114
        ];
115
    }
116
}
117
118
class EnumTypeGuesserTest_TestClass
119
{
120
    public $property;
121
}
122