Completed
Push — 3.x ( e95e95...638cd1 )
by Oskar
05:54
created

tests/Guesser/TypeGuesserChainTest.php (2 issues)

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Guesser;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Guesser\TypeGuesserChain;
18
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
19
use Sonata\AdminBundle\Model\ModelManagerInterface;
20
use Symfony\Component\Form\Exception\UnexpectedTypeException;
21
use Symfony\Component\Form\Guess\Guess;
22
use Symfony\Component\Form\Guess\TypeGuess;
23
24
/**
25
 * TypeGuesserChain Test.
26
 *
27
 * @author Andrej Hudec <[email protected]>
28
 */
29
class TypeGuesserChainTest extends TestCase
30
{
31
    public function testConstructorWithException(): void
32
    {
33
        $this->expectException(UnexpectedTypeException::class);
34
35
        $typeGuesserChain = new TypeGuesserChain([new \stdClass()]);
36
    }
37
38
    public function testGuessType(): void
39
    {
40
        $typeGuess1 = new TypeGuess('foo1', [], Guess::MEDIUM_CONFIDENCE);
41
        $guesser1 = $this->getMockForAbstractClass(TypeGuesserInterface::class);
42
        $guesser1->expects($this->any())
43
                ->method('guessType')
44
                ->willReturn($typeGuess1);
45
46
        $typeGuess2 = new TypeGuess('foo2', [], Guess::HIGH_CONFIDENCE);
47
        $guesser2 = $this->getMockForAbstractClass(TypeGuesserInterface::class);
48
        $guesser2->expects($this->any())
49
                ->method('guessType')
50
                ->willReturn($typeGuess2);
51
52
        $typeGuess3 = new TypeGuess('foo3', [], Guess::LOW_CONFIDENCE);
53
        $guesser3 = $this->getMockForAbstractClass(TypeGuesserInterface::class);
54
        $guesser3->expects($this->any())
55
                ->method('guessType')
56
                ->willReturn($typeGuess3);
57
58
        $modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class);
59
60
        $class = \stdClass::class;
61
        $property = 'firstName';
62
63
        $typeGuesserChain = new TypeGuesserChain([$guesser1, $guesser2, $guesser3]);
64
        $this->assertSame($typeGuess2, $typeGuesserChain->guessType($class, $property, $modelManager));
0 ignored issues
show
$modelManager is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...\ModelManagerInterface>.

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...
65
66
        $typeGuess4 = new TypeGuess('foo4', [], Guess::LOW_CONFIDENCE);
67
        $guesser4 = $this->getMockForAbstractClass(TypeGuesserInterface::class);
68
        $guesser4->expects($this->any())
69
                ->method('guessType')
70
                ->willReturn($typeGuess4);
71
72
        $typeGuesserChain = new TypeGuesserChain([$guesser4, $typeGuesserChain]);
73
        $this->assertSame($typeGuess2, $typeGuesserChain->guessType($class, $property, $modelManager));
0 ignored issues
show
$modelManager is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...\ModelManagerInterface>.

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...
74
    }
75
}
76