Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Helper/Security/OAuth/OAuthUserCreatorTest.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
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 PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * @var OAuthUserCreator $object
19
     */
20
    private $object;
21
22
    /**
23
     * @var EntityManager $em
24
     */
25
    private $em;
26
27
    /**
28
     * @var OAuthUserFinderInterface $finder
29
     */
30
    private $finder;
31
32
    /**
33
     * @return \PHPUnit_Framework_MockObject_MockObject
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
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()
57
    {
58
        $em = $this->getEm();
59
        $finder = $this->getFinder();
60
        $object = new OAuthUserCreator($em, [[
61
            'domain_name' => 'gmail.com',
62
            'access_levels' => ['ROLE_ADMIN'],
63
        ]], User::class, $finder);
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()
75
    {
76
        $object = $this->object;
77
        $this->getFinder()->expects($this->once())->method('findUserByGoogleSignInData')->willReturn(new User());
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);
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()
89
    {
90
        $object = $this->object;
91
        $this->getFinder()->expects($this->once())->method('findUserByGoogleSignInData')->willReturn(new DateTime());
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);
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, [[
110
            'domain_name' => 'gmail.com',
111
            'access_levels' => ['ROLE_ADMIN'],
112
        ]], User::class, $finder);
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
}
122