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 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 |
||
34 | */ |
||
35 | private function getEm() |
||
36 | { |
||
37 | if (!isset($this->em)) { |
||
38 | $this->em = $this->createMock(EntityManager::class); |
||
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); |
||
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() |
|
0 ignored issues
–
show
|
|||
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() |
|
0 ignored issues
–
show
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()); |
||
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 |
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.