testFindOneByReturnsNullIfNotExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace JhUserTest\Repository;
4
5
use JhUserTest\Util\ServiceManagerFactory;
6
use JhUserTest\Fixture\SingleUser;
7
use JhUserTest\Fixture\MultipleUser;
8
9
/**
10
 * Class UserRepositoryTest
11
 * @package JhUserTest\Repository
12
 * @author Aydin Hassan <[email protected]>
13
 */
14
class UserRepositoryTest extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * @var \Doctrine\Common\DataFixtures\Executor\AbstractExecutor
18
     */
19
    protected $fixtureExectutor;
20
21
    /**
22
     * @var \JhUser\Repository\UserRepository
23
     */
24
    protected $repository;
25
26
    public function setUp()
27
    {
28
        $sm = ServiceManagerFactory::getServiceManager();
29
        $this->repository = $sm->get('JhUser\Repository\UserRepository');
0 ignored issues
show
Documentation Bug introduced by
It seems like $sm->get('JhUser\\Repository\\UserRepository') can also be of type array. However, the property $repository is declared as type object<JhUser\Repository\UserRepository>. 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->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...
31
        $this->assertInstanceOf('JhUser\Repository\UserRepository', $this->repository);
32
    }
33
34
    public function testGetAllUsers()
35
    {
36
        $user = new SingleUser();
37
        $this->fixtureExectutor->execute([$user]);
38
39
        $this->assertCount(1, $this->repository->findAll());
40
    }
41
42
    public function testGetAllUsersWithPagination()
43
    {
44
        $user = new SingleUser();
45
        $this->fixtureExectutor->execute([$user]);
46
47
        $this->assertCount(1, $this->repository->findAll(true));
48
    }
49
50
    public function testFindOneByEmailReturnsNullIfNotExists()
51
    {
52
        $this->assertNull($this->repository->findOneByEmail("[email protected]"));
53
    }
54
55
    public function testFindOneByEmailReturnsUserIfExists()
56
    {
57
        $user = new SingleUser();
58
        $this->fixtureExectutor->execute([$user]);
59
        $result = $this->repository->findOneByEmail($user->getUser()->getEmail());
60
        $this->assertInstanceOf('JhUser\Entity\User', $result);
61
        $this->assertSame($user->getUser()->getEmail(), $result->getEmail());
62
        $this->assertSame($user->getUser()->getPassword(), $result->getPassword());
63
        $this->assertSame($user->getUser()->getId(), $result->getId());
64
    }
65
66
    public function testFindOneByReturnsNullIfNotExists()
67
    {
68
        $this->assertNull($this->repository->findOneBy(["email" => "[email protected]"]));
69
    }
70
71
    public function testFindOneByReturnsUserIfExists()
72
    {
73
        $user = new SingleUser();
74
        $this->fixtureExectutor->execute([$user]);
75
        $result = $this->repository->findOneBy(["email" => "[email protected]"]);
76
        $this->assertInstanceOf('JhUser\Entity\User', $result);
77
        $this->assertSame($user->getUser()->getEmail(), $result->getEmail());
78
        $this->assertSame($user->getUser()->getPassword(), $result->getPassword());
79
        $this->assertSame($user->getUser()->getId(), $result->getId());
80
    }
81
82
    public function testFindByReturnsEmptyIfNonExist()
83
    {
84
        $this->assertEmpty($this->repository->findBy(['email' => '[email protected]']));
85
    }
86
87
    public function testFindByReturnsCollectionIfExist()
88
    {
89
        $this->assertEmpty($this->repository->findBy(['email' => '[email protected]']));
90
91
        $users = new MultipleUser();
92
        $this->fixtureExectutor->execute([$users]);
93
        $result = $this->repository->findBy(["password" => 'p$ssw0rd']);
94
        $this->assertSame(count($users->getUsers()), count($result));
95
    }
96
97
    public function testFindById()
98
    {
99
        $user = new SingleUser();
100
        $this->fixtureExectutor->execute([$user]);
101
        $result = $this->repository->find($user->getUser()->getId());
102
        $this->assertInstanceOf('JhUser\Entity\User', $result);
103
        $this->assertSame($user->getUser()->getEmail(), $result->getEmail());
104
        $this->assertSame($user->getUser()->getPassword(), $result->getPassword());
105
        $this->assertSame($user->getUser()->getId(), $result->getId());
106
    }
107
108
    public function testGetClass()
109
    {
110
        $this->assertSame('JhUser\Entity\User', $this->repository->getClassName());
111
    }
112
}
113