1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JhUserTest\Repository; |
4
|
|
|
|
5
|
|
|
use JhUserTest\Util\ServiceManagerFactory; |
6
|
|
|
use JhUserTest\Fixture\SingleRole; |
7
|
|
|
use JhUserTest\Fixture\MultipleRole; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class RoleRepositoryTest |
11
|
|
|
* @package JhUserTest\Repository |
12
|
|
|
* @author Aydin Hassan <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class RoleRepositoryTest extends \PHPUnit_Framework_TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Doctrine\Common\DataFixtures\Executor\AbstractExecutor |
18
|
|
|
*/ |
19
|
|
|
protected $fixtureExectutor; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \JhUser\Repository\RoleRepository |
23
|
|
|
*/ |
24
|
|
|
protected $repository; |
25
|
|
|
|
26
|
|
|
public function setUp() |
27
|
|
|
{ |
28
|
|
|
$sm = ServiceManagerFactory::getServiceManager(); |
29
|
|
|
$this->repository = $sm->get('JhUser\Repository\RoleRepository'); |
|
|
|
|
30
|
|
|
$this->fixtureExectutor = $sm->get('Doctrine\Common\DataFixtures\Executor\AbstractExecutor'); |
|
|
|
|
31
|
|
|
$this->assertInstanceOf('JhUser\Repository\RoleRepository', $this->repository); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGetAllRoles() |
35
|
|
|
{ |
36
|
|
|
$role = new SingleRole(); |
37
|
|
|
$this->fixtureExectutor->execute([$role]); |
38
|
|
|
|
39
|
|
|
$this->assertCount(1, $this->repository->findAll()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testGetAllRolesWithPagination() |
43
|
|
|
{ |
44
|
|
|
$role = new SingleRole(); |
45
|
|
|
$this->fixtureExectutor->execute([$role]); |
46
|
|
|
|
47
|
|
|
$this->assertCount(1, $this->repository->findAll(true)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testFindByRoleNameReturnsNullIfNotExists() |
51
|
|
|
{ |
52
|
|
|
$this->assertNull($this->repository->findByName("super-admin")); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testFindByRoleNameReturnsRoleIfExists() |
56
|
|
|
{ |
57
|
|
|
$role = new SingleRole(); |
58
|
|
|
$this->fixtureExectutor->execute([$role]); |
59
|
|
|
$result = $this->repository->findByName($role->getRole()->getName()); |
60
|
|
|
$this->assertInstanceOf('JhUser\Entity\HierarchicalRole', $result); |
61
|
|
|
$this->assertEquals($role->getRole()->getChildren()->toArray(), $result->getChildren()->toArray()); |
62
|
|
|
$this->assertSame($role->getRole()->getId(), $result->getId()); |
63
|
|
|
$this->assertSame($role->getRole()->getName(), $result->getName()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testFindOneByReturnsNullIfNotExists() |
67
|
|
|
{ |
68
|
|
|
$this->assertNull($this->repository->findOneBy(["name" => "super-admin"])); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testFindOneByReturnsRoleIfExists() |
72
|
|
|
{ |
73
|
|
|
$role = new SingleRole(); |
74
|
|
|
$this->fixtureExectutor->execute([$role]); |
75
|
|
|
$result = $this->repository->findOneBy(["name" => "admin"]); |
76
|
|
|
$this->assertInstanceOf('JhUser\Entity\HierarchicalRole', $result); |
77
|
|
|
$this->assertEquals($role->getRole()->getChildren()->toArray(), $result->getChildren()->toArray()); |
78
|
|
|
$this->assertSame($role->getRole()->getId(), $result->getId()); |
79
|
|
|
$this->assertSame($role->getRole()->getName(), $result->getName()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testFindByReturnsEmptyIfNonExist() |
83
|
|
|
{ |
84
|
|
|
$this->assertEmpty($this->repository->findBy(['name' => 'admin'])); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testFindByReturnsCollectionIfExist() |
88
|
|
|
{ |
89
|
|
|
$this->assertEmpty($this->repository->findBy(['name' => 'user'])); |
90
|
|
|
|
91
|
|
|
$roles = new MultipleRole(); |
92
|
|
|
$this->fixtureExectutor->execute([$roles]); |
93
|
|
|
$result = $this->repository->findBy(["name" => "manager"]); |
94
|
|
|
$this->assertSame(1, count($result)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testFindById() |
98
|
|
|
{ |
99
|
|
|
$role = new SingleRole(); |
100
|
|
|
$this->fixtureExectutor->execute([$role]); |
101
|
|
|
$result = $this->repository->find($role->getRole()->getId()); |
102
|
|
|
$this->assertInstanceOf('JhUser\Entity\HierarchicalRole', $result); |
103
|
|
|
$this->assertEquals($role->getRole()->getChildren()->toArray(), $result->getChildren()->toArray()); |
104
|
|
|
$this->assertSame($role->getRole()->getId(), $result->getId()); |
105
|
|
|
$this->assertSame($role->getRole()->getName(), $result->getName()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testGetClass() |
109
|
|
|
{ |
110
|
|
|
$this->assertSame('JhUser\Entity\HierarchicalRole', $this->repository->getClassName()); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.