Completed
Push — 5.6 ( c5fe0b...561087 )
by Jeroen
19:25 queued 04:42
created

testGetOrCreateUserReturnsUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Tests\Helper\Security\OAuth;
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\MockObject\MockObject;
13
use PHPUnit\Framework\TestCase;
14
use ReflectionClass;
15
16
class OAuthUserCreatorTest extends TestCase
17
{
18
    /**
19
     * @var OAuthUserCreator
20
     */
21
    private $object;
22
23
    /**
24
     * @var EntityManager
25
     */
26
    private $em;
27
28
    /**
29
     * @var OAuthUserFinderInterface
30
     */
31
    private $finder;
32
33
    /**
34
     * @return MockObject
35
     */
36
    private function getEm()
37
    {
38
        if (!isset($this->em)) {
39
            $this->em = $this->createMock(EntityManager::class);
0 ignored issues
show
Documentation Bug introduced by Ruud Denivel
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...
40
        }
41
42
        return $this->em;
43
    }
44
45
    /**
46
     * @return MockObject
47
     */
48
    private function getFinder()
49
    {
50
        if (!isset($this->finder)) {
51
            $this->finder = $this->createMock(OAuthUserFinderInterface::class);
0 ignored issues
show
Documentation Bug introduced by Ruud Denivel
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...
52
        }
53
54
        return $this->finder;
55
    }
56
57
    public function setup()
58
    {
59
        $em = $this->getEm();
60
        $finder = $this->getFinder();
61
        $object = new OAuthUserCreator($em, [[
0 ignored issues
show
Documentation introduced by Ruud Denivel
$em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

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...
62
            'domain_name' => 'gmail.com',
63
            'access_levels' => ['ROLE_ADMIN'],
64
        ]], User::class, $finder);
0 ignored issues
show
Documentation introduced by Ruud Denivel
$finder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\AdminBu...uthUserFinderInterface>.

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
        $this->object = $object;
66
    }
67
68
    public function testGetOrCreateUserReturnsNull()
69
    {
70
        $object = $this->object;
71
        $user = $object->getOrCreateUser('[email protected]', 12345679);
72
        $this->assertNull($user);
73
    }
74
75 View Code Duplication
    public function testGetOrCreateUserReturnsUser()
0 ignored issues
show
Duplication introduced by Ruud Denivel
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...
76
    {
77
        $object = $this->object;
78
        $this->getFinder()->expects($this->once())->method('findUserByGoogleSignInData')->willReturn(new User());
79
        $mockGroup = $this->createMock(Group::class);
80
        $mockRepo = $this->createMock(EntityRepository::class);
81
        $mockRepo->expects($this->once())->method('findOneBy')->willReturn($mockGroup);
82
        $this->getEm()->expects($this->once())->method('getRepository')->willReturn($mockRepo);
83
        $this->getEm()->expects($this->once())->method('persist')->willReturn(true);
84
        $this->getEm()->expects($this->once())->method('flush')->willReturn(true);
85
        $user = $object->getOrCreateUser('[email protected]', 12345679);
86
        $this->assertInstanceOf(User::class, $user);
87
    }
88
89 View Code Duplication
    public function testGetOrCreateUserCreatesCorrectUserClass()
0 ignored issues
show
Duplication introduced by Ruud Denivel
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...
90
    {
91
        $object = $this->object;
92
        $this->getFinder()->expects($this->once())->method('findUserByGoogleSignInData')->willReturn(new DateTime());
93
        $mockGroup = $this->createMock(Group::class);
94
        $mockRepo = $this->createMock(EntityRepository::class);
95
        $mockRepo->expects($this->once())->method('findOneBy')->willReturn($mockGroup);
96
        $this->getEm()->expects($this->once())->method('getRepository')->willReturn($mockRepo);
97
        $this->getEm()->expects($this->once())->method('persist')->willReturn(true);
98
        $this->getEm()->expects($this->once())->method('flush')->willReturn(true);
99
        $user = $object->getOrCreateUser('[email protected]', 12345679);
100
        $this->assertInstanceOf(User::class, $user);
101
    }
102
103
    /**
104
     * @throws \ReflectionException
105
     */
106
    public function testAccessLevelsReturnsNull()
107
    {
108
        $em = $this->getEm();
109
        $finder = $this->getFinder();
110
        $object = new OAuthUserCreator($em, [[
0 ignored issues
show
Documentation introduced by Ruud Denivel
$em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

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...
111
            'domain_name' => 'gmail.com',
112
            'access_levels' => ['ROLE_ADMIN'],
113
        ]], User::class, $finder);
0 ignored issues
show
Documentation introduced by Ruud Denivel
$finder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\AdminBu...uthUserFinderInterface>.

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...
114
115
        $mirror = new ReflectionClass(OAuthUserCreator::class);
116
        $method = $mirror->getMethod('getAccessLevels');
117
        $method->setAccessible(true);
118
        $levels = $method->invoke($object, '[email protected]');
119
        $this->assertNull($levels);
120
    }
121
}
122