Completed
Push — master ( 8dda9e...e47526 )
by Jeroen
06:51 queued 11s
created

OAuthUserCreatorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Group;
9
use Kunstmaan\AdminBundle\Entity\User;
10
use Kunstmaan\AdminBundle\Helper\Security\OAuth\OAuthUserCreator;
11
use Kunstmaan\AdminBundle\Helper\Security\OAuth\OAuthUserFinderInterface;
12
use PHPUnit\Framework\TestCase;
13
use ReflectionClass;
14
15
class OAuthUserCreatorTest extends TestCase
16
{
17
    /**
18
     * @var OAuthUserCreator
19
     */
20
    private $object;
21
22
    /**
23
     * @var EntityManager
24
     */
25
    private $em;
26
27
    /**
28
     * @var OAuthUserFinderInterface
29
     */
30
    private $finder;
31
32
    /**
33
     * @return \PHPUnit_Framework_MockObject_MockObject
0 ignored issues
show
Documentation introduced by
Should the return type not be \PHPUnit\Framework\MockO...ockObject|EntityManager?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
34
     */
35
    private function getEm()
36
    {
37
        if (!isset($this->em)) {
38
            $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...
39
        }
40
41
        return $this->em;
42
    }
43
44
    /**
45
     * @return \PHPUnit_Framework_MockObject_MockObject
0 ignored issues
show
Documentation introduced by
Should the return type not be \PHPUnit\Framework\MockO...AuthUserFinderInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
46
     */
47
    private function getFinder()
48
    {
49
        if (!isset($this->finder)) {
50
            $this->finder = $this->createMock(OAuthUserFinderInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Kunst...FinderInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Kunstmaan\AdminBu...uthUserFinderInterface> of property $finder.

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...
51
        }
52
53
        return $this->finder;
54
    }
55
56
    public function setUp(): void
57
    {
58
        $em = $this->getEm();
59
        $finder = $this->getFinder();
60
        $object = new OAuthUserCreator($em, [[
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEm() on line 58 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Kunstmaan\AdminBundle\He...rCreator::__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...
61
            'domain_name' => 'gmail.com',
62
            'access_levels' => ['ROLE_ADMIN'],
63
        ]], User::class, $finder);
0 ignored issues
show
Bug introduced by
It seems like $finder defined by $this->getFinder() on line 59 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Kunstmaan\AdminBundle\He...rCreator::__construct() does only seem to accept object<Kunstmaan\AdminBu...uthUserFinderInterface>, 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...
64
        $this->object = $object;
65
    }
66
67
    public function testGetOrCreateUserReturnsNull()
68
    {
69
        $object = $this->object;
70
        $user = $object->getOrCreateUser('[email protected]', 12345679);
71
        $this->assertNull($user);
72
    }
73
74 View Code Duplication
    public function testGetOrCreateUserReturnsUser()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $object = $this->object;
77
        $this->getFinder()->expects($this->once())->method('findUserByGoogleSignInData')->willReturn(new User());
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Kunstmaan\AdminBundle\He...AuthUserFinderInterface.

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...
78
        $mockGroup = $this->createMock(Group::class);
79
        $mockRepo = $this->createMock(EntityRepository::class);
80
        $mockRepo->expects($this->once())->method('findOneBy')->willReturn($mockGroup);
81
        $this->getEm()->expects($this->once())->method('getRepository')->willReturn($mockRepo);
0 ignored issues
show
Bug introduced by
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...
82
        $this->getEm()->expects($this->once())->method('persist')->willReturn(true);
83
        $this->getEm()->expects($this->once())->method('flush')->willReturn(true);
84
        $user = $object->getOrCreateUser('[email protected]', 12345679);
85
        $this->assertInstanceOf(User::class, $user);
86
    }
87
88 View Code Duplication
    public function testGetOrCreateUserCreatesCorrectUserClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        $object = $this->object;
91
        $this->getFinder()->expects($this->once())->method('findUserByGoogleSignInData')->willReturn(new DateTime());
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Kunstmaan\AdminBundle\He...AuthUserFinderInterface.

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...
92
        $mockGroup = $this->createMock(Group::class);
93
        $mockRepo = $this->createMock(EntityRepository::class);
94
        $mockRepo->expects($this->once())->method('findOneBy')->willReturn($mockGroup);
95
        $this->getEm()->expects($this->once())->method('getRepository')->willReturn($mockRepo);
0 ignored issues
show
Bug introduced by
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...
96
        $this->getEm()->expects($this->once())->method('persist')->willReturn(true);
97
        $this->getEm()->expects($this->once())->method('flush')->willReturn(true);
98
        $user = $object->getOrCreateUser('[email protected]', 12345679);
99
        $this->assertInstanceOf(User::class, $user);
100
    }
101
102
    /**
103
     * @throws \ReflectionException
104
     */
105
    public function testAccessLevelsReturnsNull()
106
    {
107
        $em = $this->getEm();
108
        $finder = $this->getFinder();
109
        $object = new OAuthUserCreator($em, [[
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->getEm() on line 107 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Kunstmaan\AdminBundle\He...rCreator::__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...
110
            'domain_name' => 'gmail.com',
111
            'access_levels' => ['ROLE_ADMIN'],
112
        ]], User::class, $finder);
0 ignored issues
show
Bug introduced by
It seems like $finder defined by $this->getFinder() on line 108 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Kunstmaan\AdminBundle\He...rCreator::__construct() does only seem to accept object<Kunstmaan\AdminBu...uthUserFinderInterface>, 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...
113
114
        $mirror = new ReflectionClass(OAuthUserCreator::class);
115
        $method = $mirror->getMethod('getAccessLevels');
116
        $method->setAccessible(true);
117
        $levels = $method->invoke($object, '[email protected]');
118
        $this->assertNull($levels);
119
    }
120
}
121