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); |
|
|
|
|
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); |
|
|
|
|
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, [[ |
|
|
|
|
62
|
|
|
'domain_name' => 'gmail.com', |
63
|
|
|
'access_levels' => ['ROLE_ADMIN'], |
64
|
|
|
]], User::class, $finder); |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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, [[ |
|
|
|
|
111
|
|
|
'domain_name' => 'gmail.com', |
112
|
|
|
'access_levels' => ['ROLE_ADMIN'], |
113
|
|
|
]], User::class, $finder); |
|
|
|
|
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
|
|
|
|
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..