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

Tests/Form/EnumTypeGuesserTest.php (6 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\DForm;
12
13
use Doctrine\Common\Persistence\ManagerRegistry;
14
use Doctrine\ORM\Mapping\ClassMetadataInfo;
15
use Fresh\DoctrineEnumBundle\Form\EnumTypeGuesser;
16
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType;
17
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\NotAChildType;
18
use Fresh\DoctrineEnumBundle\Util\LegacyFormHelper;
19
use Symfony\Component\Form\Guess\Guess;
20
use Symfony\Component\Form\Guess\TypeGuess;
21
22
/**
23
 * EnumTypeGuesserTest.
24
 *
25
 * @author Artem Genvald <[email protected]>
26
 */
27
class EnumTypeGuesserTest extends \PHPUnit_Framework_TestCase
28
{
29
    public function testNullResultWhenClassMetadataNotFound()
30
    {
31
        /** @var EnumTypeGuesser|\PHPUnit_Framework_MockObject_MockObject $enumTypeGuesser */
32
        $enumTypeGuesser = $this->getMockBuilder(EnumTypeGuesser::class)
33
            ->disableOriginalConstructor()
34
            ->setMethods(['getMetadata'])
35
            ->getMock();
36
37
        $enumTypeGuesser->expects($this->once())
38
            ->method('getMetadata')
39
            ->willReturn(null);
40
41
        $this->assertNull($enumTypeGuesser->guessType(\stdClass::class, 'position'));
0 ignored issues
show
The method guessType does only exist in Fresh\DoctrineEnumBundle\Form\EnumTypeGuesser, but not in PHPUnit_Framework_MockObject_MockObject.

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...
42
    }
43
44
    public function testNullResultWhenEnumTypeNotRegistered()
45
    {
46
        /** @var EnumTypeGuesser|\PHPUnit_Framework_MockObject_MockObject $enumTypeGuesser */
47
        $enumTypeGuesser = $this->getMockBuilder(EnumTypeGuesser::class)
48
                                ->disableOriginalConstructor()
49
                                ->setMethods(['getMetadata'])
50
                                ->getMock();
51
52
        $metadata = $this->getMockBuilder(ClassMetadataInfo::class)
53
            ->disableOriginalConstructor()
54
            ->setMethods(['getTypeOfField'])
55
            ->getMock();
56
57
        $metadata->expects($this->once())
58
                 ->method('getTypeOfField')
59
                 ->willReturn('unregistered_enum_type');
60
61
        $enumTypeGuesser->expects($this->once())
62
                        ->method('getMetadata')
63
                        ->willReturn([$metadata]);
64
65
        $this->assertNull($enumTypeGuesser->guessType(\stdClass::class, 'position'));
0 ignored issues
show
The method guessType does only exist in Fresh\DoctrineEnumBundle\Form\EnumTypeGuesser, but not in PHPUnit_Framework_MockObject_MockObject.

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
    }
67
68
    /**
69
     * @expectedException \Fresh\DoctrineEnumBundle\Exception\EnumTypeIsRegisteredButClassDoesNotExistException
70
     */
71 View Code Duplication
    public function testExceptionWhenClassDoesNotExist()
72
    {
73
        $managerRegistry = $this->createMock(ManagerRegistry::class);
0 ignored issues
show
The method createMock() does not seem to exist on object<Fresh\DoctrineEnu...rm\EnumTypeGuesserTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
        $registeredTypes = [
75
            'stub' => [
76
                'class' => '\Acme\Foo\Bar\Baz',
77
            ]
78
        ];
79
80
        /** @var EnumTypeGuesser|\PHPUnit_Framework_MockObject_MockObject $enumTypeGuesser */
81
        $enumTypeGuesser = $this->getMockBuilder(EnumTypeGuesser::class)
82
                                ->setConstructorArgs([$managerRegistry, $registeredTypes])
83
                                ->setMethods(['getMetadata'])
84
                                ->getMock();
85
86
        $metadata = $this->getMockBuilder(ClassMetadataInfo::class)
87
                         ->disableOriginalConstructor()
88
                         ->setMethods(['getTypeOfField'])
89
                         ->getMock();
90
91
        $metadata->expects($this->once())
92
                 ->method('getTypeOfField')
93
                 ->willReturn('stub');
94
95
        $enumTypeGuesser->expects($this->once())
96
                        ->method('getMetadata')
97
                        ->willReturn([$metadata]);
98
99
        $this->assertNull($enumTypeGuesser->guessType(\stdClass::class, 'position'));
0 ignored issues
show
The method guessType does only exist in Fresh\DoctrineEnumBundle\Form\EnumTypeGuesser, but not in PHPUnit_Framework_MockObject_MockObject.

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...
100
    }
101
102 View Code Duplication
    public function testNullResultWhenIsNotChildOfAbstractEnumType()
103
    {
104
        $managerRegistry = $this->createMock(ManagerRegistry::class);
0 ignored issues
show
The method createMock() does not seem to exist on object<Fresh\DoctrineEnu...rm\EnumTypeGuesserTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
        $registeredTypes = [
106
            'NotAChildType' => [
107
                'class' => NotAChildType::class,
108
            ]
109
        ];
110
111
        /** @var EnumTypeGuesser|\PHPUnit_Framework_MockObject_MockObject $enumTypeGuesser */
112
        $enumTypeGuesser = $this->getMockBuilder(EnumTypeGuesser::class)
113
                                ->setConstructorArgs([$managerRegistry, $registeredTypes])
114
                                ->setMethods(['getMetadata'])
115
                                ->getMock();
116
117
        $metadata = $this->getMockBuilder(ClassMetadataInfo::class)
118
                         ->disableOriginalConstructor()
119
                         ->setMethods(['getTypeOfField'])
120
                         ->getMock();
121
122
        $metadata->expects($this->once())
123
                 ->method('getTypeOfField')
124
                 ->willReturn('NotAChildType');
125
126
        $enumTypeGuesser->expects($this->once())
127
                        ->method('getMetadata')
128
                        ->willReturn([$metadata]);
129
130
        $this->assertNull($enumTypeGuesser->guessType(\stdClass::class, 'position'));
131
    }
132
133
    public function testSuccessfulTypeGuessing()
134
    {
135
        $managerRegistry = $this->createMock(ManagerRegistry::class);
0 ignored issues
show
The method createMock() does not seem to exist on object<Fresh\DoctrineEnu...rm\EnumTypeGuesserTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
        $registeredTypes = [
137
            'BasketballPositionType' => [
138
                'class' => BasketballPositionType::class,
139
            ]
140
        ];
141
142
        /** @var EnumTypeGuesser|\PHPUnit_Framework_MockObject_MockObject $enumTypeGuesser */
143
        $enumTypeGuesser = $this->getMockBuilder(EnumTypeGuesser::class)
144
                                ->setConstructorArgs([$managerRegistry, $registeredTypes])
145
                                ->setMethods(['getMetadata'])
146
                                ->getMock();
147
148
        $metadata = $this->getMockBuilder(ClassMetadataInfo::class)
149
                         ->disableOriginalConstructor()
150
                         ->setMethods(['getTypeOfField', 'isNullable'])
151
                         ->getMock();
152
153
        $metadata->expects($this->once())
154
                 ->method('getTypeOfField')
155
                 ->willReturn('BasketballPositionType');
156
157
        $metadata->expects($this->once())
158
                 ->method('isNullable')
159
                 ->willReturn(true);
160
161
        $enumTypeGuesser->expects($this->once())
162
                        ->method('getMetadata')
163
                        ->willReturn([$metadata]);
164
165
        $typeGuess = new TypeGuess(
166
            LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\ChoiceType'),
167
            [
168
                'choices'  => BasketballPositionType::getChoices(),
169
                'required' => false,
170
            ],
171
            Guess::VERY_HIGH_CONFIDENCE
172
        );
173
174
        $this->assertEquals($typeGuess, $enumTypeGuesser->guessType(\stdClass::class, 'position'));
175
    }
176
}
177