Completed
Pull Request — master (#10)
by
unknown
09:12
created

UpdateUserHandlerTest::testUpdateUserGetsHandled()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 31
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Tests\Command;
4
5
use SumoCoders\FrameworkMultiUserBundle\Command\UpdateUser;
6
use SumoCoders\FrameworkMultiUserBundle\Command\UpdateUserHandler;
7
use SumoCoders\FrameworkMultiUserBundle\User\InMemoryUserRepository;
8
use SumoCoders\FrameworkMultiUserBundle\User\UserRepository;
9
use SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection;
10
11
class UpdateUserHandlerTest extends \PHPUnit_Framework_TestCase
12
{
13
    /**
14
     * @var UserRepository
15
     */
16
    private $userRepository;
17
18
    /**
19
     * @var UserRepositoryCollection
20
     */
21
    private $userRepositoryCollection;
22
23
    public function setUp()
24
    {
25
        $this->userRepository = new InMemoryUserRepository();
26
        $this->userRepositoryCollection = new UserRepositoryCollection([$this->userRepository]);
27
    }
28
29
    /**
30
     * Test if UpdateUserHandler gets handled.
31
     */
32
    public function testUpdateUserGetsHandled()
33
    {
34
        $handler = new UpdateUserHandler($this->userRepositoryCollection);
35
36
        $updatingUser = $this->userRepository->findByUsername('wouter');
37
38
        $command = new UpdateUser($updatingUser, 'wouter', 'randomPassword', 'sumocoders');
0 ignored issues
show
Bug introduced by
It seems like $updatingUser defined by $this->userRepository->findByUsername('wouter') on line 36 can be null; however, SumoCoders\FrameworkMult...dateUser::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
39
40
        $handler->handle($command);
41
42
        $this->assertEquals(
43
            'wouter',
44
            $this->userRepository->findByUsername('wouter')->getUsername()
45
        );
46
        $this->assertEquals(
47
            'sumocoders',
48
            $this->userRepository->findByUsername('wouter')->getDisplayName()
49
        );
50
        $this->assertEquals(
51
            'randomPassword',
52
            $this->userRepository->findByUsername('wouter')->getPassword()
53
        );
54
        $this->assertNotEquals(
55
            $updatingUser->getDisplayName(),
56
            $this->userRepository->findByUsername('wouter')->getDisplayName()
57
        );
58
        $this->assertNotEquals(
59
            $updatingUser->getPassword(),
60
            $this->userRepository->findByUsername('wouter')->getPassword()
61
        );
62
    }
63
}
64