Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

unit/Helper/Security/OAuth/OAuthUserFinderTest.php (3 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
namespace Kunstmaan\AdminBundle\Tests\Helper\Security\Acl;
4
5
use DateTime;
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\EntityRepository;
8
use Kunstmaan\AdminBundle\Entity\User;
9
use Kunstmaan\AdminBundle\Helper\Security\OAuth\OAuthUserFinder;
10
use PHPUnit\Framework\TestCase;
11
12
class OAuthUserFinderTest extends TestCase
13
{
14
    /**
15
     * @var OAuthUserFinder
16
     */
17
    private $object;
18
19
    /**
20
     * @var EntityManager
21
     */
22
    private $em;
23
24
    /**
25
     * @return \PHPUnit_Framework_MockObject_MockObject
26
     */
27
    private function getEm()
28
    {
29
        if (!isset($this->em)) {
30
            $this->em = $this->createMock(EntityManager::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...M\EntityManager::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\ORM\EntityManager> of property $em.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
        }
32
33
        return $this->em;
34
    }
35
36
    public function setup()
37
    {
38
        $em = $this->getEm();
39
        $object = new OAuthUserFinder($em, User::class);
0 ignored issues
show
It seems like $em defined by $this->getEm() on line 38 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Kunstmaan\AdminBundle\He...erFinder::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
40
        $this->object = $object;
41
    }
42
43
    public function testFindUserByGoogleSignInData()
44
    {
45
        $object = $this->object;
46
        $repo = $this->createMock(EntityRepository::class);
47
        $repo->expects($this->exactly(3))->method('findOneBy')->will($this->onConsecutiveCalls(new DateTime(), new DateTime(), new User()));
48
        $this->getEm()->expects($this->exactly(3))->method('getRepository')->willReturn($repo);
0 ignored issues
show
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Doctrine\ORM\EntityManager.

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...
49
        $user = $object->findUserByGoogleSignInData('[email protected]', 12345679);
50
        $this->assertInstanceOf(User::class, $user);
51
    }
52
}
53