Completed
Push — master ( c3efa2...a812ec )
by Guilherme
13s
created

ClientManagerTest::getDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 1
b 0
f 1
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
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 LoginCidadao\OpenIDBundle\Tests\Manager;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\OAuthBundle\Entity\Client;
15
use LoginCidadao\OAuthBundle\Entity\ClientRepository;
16
use LoginCidadao\OpenIDBundle\Manager\ClientManager;
17
use Symfony\Component\EventDispatcher\EventDispatcher;
18
19
class ClientManagerTest extends \PHPUnit_Framework_TestCase
20
{
21
    public function testGetClientById()
22
    {
23
        $id = 123;
24
25
        $repo = $this->getClientRepository();
26
        $repo->expects($this->once())->method('find')->willReturn(new Client());
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OAuthBundle\Entity\ClientRepository.

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...
27
28
        $em = $this->getEntityManager();
29
        $em->expects($this->once())->method('getRepository')->with('LoginCidadaoOAuthBundle:Client')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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...
30
            ->willReturn($repo);
31
32
        $manager = new ClientManager($em, $this->getDispatcher());
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 28 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OpenIDBundl...tManager::__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...
Bug introduced by
It seems like $this->getDispatcher() targeting LoginCidadao\OpenIDBundl...erTest::getDispatcher() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OpenIDBundl...tManager::__construct() does only seem to accept object<Symfony\Component...entDispatcherInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
33
        $manager->getClientById($id);
34
    }
35
36
    public function testGetClientByPublicId()
37
    {
38
        $id = '123_randomIdHere';
39
40
        $repo = $this->getClientRepository();
41
        $repo->expects($this->once())->method('findOneBy')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OAuthBundle\Entity\ClientRepository.

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
            ->with(['id' => 123, 'randomId' => 'randomIdHere'])
43
            ->willReturn(new Client());
44
45
        $em = $this->getEntityManager();
46
        $em->expects($this->once())->method('getRepository')->with('LoginCidadaoOAuthBundle:Client')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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...
47
            ->willReturn($repo);
48
49
        $manager = new ClientManager($em, $this->getDispatcher());
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEntityManager() on line 45 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OpenIDBundl...tManager::__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...
Bug introduced by
It seems like $this->getDispatcher() targeting LoginCidadao\OpenIDBundl...erTest::getDispatcher() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, LoginCidadao\OpenIDBundl...tManager::__construct() does only seem to accept object<Symfony\Component...entDispatcherInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
50
        $manager->getClientById($id);
51
    }
52
53
    /**
54
     * @return EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
55
     */
56
    private function getEntityManager()
57
    {
58
        $em = $this->getMock('Doctrine\ORM\EntityManagerInterface');
59
60
        return $em;
61
    }
62
63
    /**
64
     * @return ClientRepository|\PHPUnit_Framework_MockObject_MockObject
65
     */
66
    private function getClientRepository()
67
    {
68
        $repo = $this->getMockBuilder('LoginCidadao\OAuthBundle\Entity\ClientRepository')
69
            ->disableOriginalConstructor()->getMock();
70
71
        return $repo;
72
    }
73
74
    /**
75
     * @return EventDispatcher|\PHPUnit_Framework_MockObject_MockObject
76
     */
77
    private function getDispatcher()
78
    {
79
        $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
80
81
        return $dispatcher;
82
    }
83
}
84