1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the EloyekunlePermissionsBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Elijah Oyekunle <https://elijahoyekunle.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Eloyekunle\PermissionsBundle\Tests\Doctrine; |
13
|
|
|
|
14
|
|
|
use Eloyekunle\PermissionsBundle\Doctrine\RoleManager; |
15
|
|
|
use Eloyekunle\PermissionsBundle\Model\RoleManagerInterface; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
|
18
|
|
|
class RoleManagerTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
const ROLE_CLASS = 'Eloyekunle\PermissionsBundle\Tests\TestRole'; |
21
|
|
|
|
22
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
23
|
|
|
protected $repository; |
24
|
|
|
|
25
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
26
|
|
|
protected $om; |
27
|
|
|
|
28
|
|
|
/** @var RoleManagerInterface */ |
29
|
|
|
protected $roleManager; |
30
|
|
|
|
31
|
|
|
public function setUp() |
32
|
|
|
{ |
33
|
|
|
if (!interface_exists('Doctrine\Common\Persistence\ObjectManager')) { |
34
|
|
|
$this->markTestSkipped('Doctrine Common has to be installed for this test to run.'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$class = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')->getMock(); |
38
|
|
|
$this->om = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')->getMock(); |
39
|
|
|
$this->repository = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectRepository')->getMock(); |
40
|
|
|
|
41
|
|
|
$this->om->expects($this->any()) |
42
|
|
|
->method('getRepository') |
43
|
|
|
->with($this->equalTo(static::ROLE_CLASS)) |
44
|
|
|
->will($this->returnValue($this->repository)); |
45
|
|
|
$this->om->expects($this->any()) |
46
|
|
|
->method('getClassMetadata') |
47
|
|
|
->with($this->equalTo(static::ROLE_CLASS)) |
48
|
|
|
->will($this->returnValue($class)); |
49
|
|
|
$class->expects($this->any()) |
50
|
|
|
->method('getName') |
51
|
|
|
->will($this->returnValue(static::ROLE_CLASS)); |
52
|
|
|
|
53
|
|
|
$this->roleManager = new RoleManager($this->om, static::ROLE_CLASS); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testGetClass() |
57
|
|
|
{ |
58
|
|
|
$this->assertSame(static::ROLE_CLASS, $this->roleManager->getClass()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testFindRoles() |
62
|
|
|
{ |
63
|
|
|
$this->repository->expects($this->once())->method('findAll'); |
64
|
|
|
|
65
|
|
|
$this->roleManager->findRoles(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testFindRoleBy() |
69
|
|
|
{ |
70
|
|
|
$crit = ['name' => 'finestRole']; |
71
|
|
|
$this->repository->expects($this->once())->method('findOneBy')->with($crit); |
72
|
|
|
|
73
|
|
|
$this->roleManager->findRoleBy($crit); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testFindRole() |
77
|
|
|
{ |
78
|
|
|
$id = 10; |
79
|
|
|
$this->repository->expects($this->once())->method('find')->with($id); |
80
|
|
|
|
81
|
|
|
$this->roleManager->findRole($id); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testUpdateRole() |
85
|
|
|
{ |
86
|
|
|
$role = $this->getRole(); |
87
|
|
|
$this->om->expects($this->once())->method('persist')->with($role); |
88
|
|
|
$this->om->expects($this->once())->method('flush'); |
89
|
|
|
|
90
|
|
|
$this->roleManager->updateRole($role); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testDeleteRole() |
94
|
|
|
{ |
95
|
|
|
$role = $this->getRole(); |
96
|
|
|
$this->om->expects($this->once())->method('remove')->with($role); |
97
|
|
|
$this->om->expects($this->once())->method('flush'); |
98
|
|
|
|
99
|
|
|
$this->roleManager->deleteRole($role); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function getRole() |
103
|
|
|
{ |
104
|
|
|
return $this->roleManager->createRole(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|