PermissionRepositoryTest::testFindById()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace JhUserTest\Repository;
4
5
use JhUserTest\Util\ServiceManagerFactory;
6
use JhUserTest\Fixture\SinglePermission;
7
8
/**
9
 * Class PermissionRepositoryTest
10
 * @package JhUserTest\Repository
11
 * @author Aydin Hassan <[email protected]>
12
 */
13
class PermissionRepositoryTest extends \PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @var \Doctrine\Common\DataFixtures\Executor\AbstractExecutor
17
     */
18
    protected $fixtureExectutor;
19
20
    /**
21
     * @var \JhUser\Repository\RoleRepository
22
     */
23
    protected $repository;
24
25
    public function setUp()
26
    {
27
        $sm = ServiceManagerFactory::getServiceManager();
28
        $this->repository = $sm->get('JhUser\Repository\PermissionRepository');
0 ignored issues
show
Documentation Bug introduced by
It seems like $sm->get('JhUser\\Reposi...\PermissionRepository') can also be of type array. However, the property $repository is declared as type object<JhUser\Repository\RoleRepository>. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
29
        $this->fixtureExectutor = $sm->get('Doctrine\Common\DataFixtures\Executor\AbstractExecutor');
0 ignored issues
show
Documentation Bug introduced by
It seems like $sm->get('Doctrine\\Comm...tor\\AbstractExecutor') can also be of type array. However, the property $fixtureExectutor is declared as type object<Doctrine\Common\D...cutor\AbstractExecutor>. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
30
        $this->assertInstanceOf('JhUser\Repository\PermissionRepository', $this->repository);
31
    }
32
33
    public function testGetAllPermissions()
34
    {
35
        $permission = new SinglePermission();
36
        $this->fixtureExectutor->execute([$permission]);
37
38
        $this->assertCount(1, $this->repository->findAll());
39
    }
40
41
    public function testFindByPermissionNameReturnsNullIfNotExists()
42
    {
43
        $this->assertNull($this->repository->findByName("delete"));
44
    }
45
46
    public function testFindByPermissionNameReturnsPermissionIfExists()
47
    {
48
        $permission = new SinglePermission();
49
        $this->fixtureExectutor->execute([$permission]);
50
        $result = $this->repository->findByName((string) $permission->getPermission());
51
        $this->assertInstanceOf('JhUser\Entity\Permission', $result);
52
        $this->assertSame($permission->getPermission()->getId(), $result->getId());
53
        $this->assertSame((string) $permission->getPermission(), (string) $result);
54
    }
55
56
    public function testFindOneByReturnsNullIfNotExists()
57
    {
58
        $this->assertNull($this->repository->findOneBy(["name" => "super-admin"]));
59
    }
60
61
    public function testFindOneByReturnsPermissionIfExists()
62
    {
63
        $permission = new SinglePermission();
64
        $this->fixtureExectutor->execute([$permission]);
65
        $result = $this->repository->findOneBy(["name" => "delete"]);
66
        $this->assertInstanceOf('JhUser\Entity\Permission', $result);
67
        $this->assertSame($permission->getPermission()->getId(), $result->getId());
68
        $this->assertSame((string) $permission->getPermission(), (string) $result);
69
    }
70
71
    public function testFindByReturnsEmptyIfNonExist()
72
    {
73
        $this->assertEmpty($this->repository->findBy(['name' => 'admin']));
74
    }
75
76
    public function testFindByReturnsCollectionIfExist()
77
    {
78
        $this->assertEmpty($this->repository->findBy(['name' => 'user']));
79
80
        $permission = new SinglePermission();
81
        $this->fixtureExectutor->execute([$permission]);
82
        $result = $this->repository->findBy(["name" => (string) $permission->getPermission()]);
83
        $this->assertSame(1, count($result));
84
    }
85
86
    public function testFindById()
87
    {
88
        $permission = new SinglePermission();
89
        $this->fixtureExectutor->execute([$permission]);
90
        $result = $this->repository->find($permission->getPermission()->getId());
91
        $this->assertInstanceOf('JhUser\Entity\Permission', $result);
92
        $this->assertSame($permission->getPermission()->getId(), $result->getId());
93
        $this->assertSame((string) $permission->getPermission(), (string) $result);
94
    }
95
96
    public function testGetClass()
97
    {
98
        $this->assertSame('JhUser\Entity\Permission', $this->repository->getClassName());
99
    }
100
}
101