Completed
Pull Request — master (#10)
by
unknown
03:16
created

UpdateUserHandlerTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 2
c 5
b 1
f 2
lcom 1
cbo 6
dl 0
loc 53
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
B testUpdateUserGetsHandled() 0 31 1
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