| Conditions | 1 |
| Paths | 1 |
| Total Lines | 106 |
| Code Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 19 | /** |
||
| 20 | * @dataProvider providerSetRole |
||
| 21 | * |
||
| 22 | * @param string $currentRole |
||
| 23 | * @param string $oldRole |
||
| 24 | * @param string $newRole |
||
| 25 | * @param null|string $exception |
||
| 26 | */ |
||
| 27 | public function testSetRole(string $currentRole, string $oldRole, string $newRole, ?string $exception): void |
||
| 28 | { |
||
| 29 | User::setCurrent(null); |
||
| 30 | if ($currentRole !== User::ROLE_ANONYMOUS) { |
||
| 31 | $currentUser = new User($currentRole); |
||
| 32 | User::setCurrent($currentUser); |
||
| 33 | } |
||
| 34 | |||
| 35 | $user2 = new User($oldRole); |
||
| 36 | |||
| 37 | if ($exception) { |
||
| 38 | $this->expectExceptionMessage($exception); |
||
| 39 | } |
||
| 40 | |||
| 41 | $user2->setRole($newRole); |
||
| 42 | self::assertSame($newRole, $user2->getRole()); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function providerSetRole(): array |
||
| 46 | { |
||
| 47 | return [ |
||
| 48 | [User::ROLE_ANONYMOUS, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, 'anonymous is not allowed to change role from administrator to member'], |
||
| 49 | [User::ROLE_ANONYMOUS, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'anonymous is not allowed to change role from member to administrator'], |
||
| 50 | |||
| 51 | [User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_MEMBER, null], |
||
| 52 | [User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'member is not allowed to change role from member to administrator'], |
||
| 53 | |||
| 54 | [User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, null], |
||
| 55 | [User::ROLE_ADMINISTRATOR, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, null], |
||
| 56 | ]; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function testSetPassword(): void |
||
| 60 | { |
||
| 61 | $user = new User(); |
||
| 62 | self::assertSame('', $user->getPassword(), 'should have no password at first'); |
||
| 63 | |||
| 64 | $user->setPassword('12345'); |
||
| 65 | $actual1 = $user->getPassword(); |
||
| 66 | self::assertNotSame('', $actual1, 'should be able to change password '); |
||
| 67 | self::assertTrue(password_verify('12345', $actual1), 'password must have been hashed'); |
||
| 68 | |||
| 69 | $user->setPassword(''); |
||
| 70 | $actual2 = $user->getPassword(); |
||
| 71 | self::assertSame($actual1, $actual2, 'should ignore empty password'); |
||
| 72 | |||
| 73 | $user->setPassword('money'); |
||
| 74 | $actual3 = $user->getPassword(); |
||
| 75 | self::assertNotSame($actual1, $actual3, 'should be able to change to something else'); |
||
| 76 | self::assertTrue(password_verify('money', $actual3), 'password must have been hashed again'); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @dataProvider providerSetOwner |
||
| 81 | */ |
||
| 82 | public function testSetOwner(?User $currentUser, ?User $originalOwner, ?User $newOwner, ?string $exception = null): void |
||
| 83 | { |
||
| 84 | User::setCurrent($currentUser); |
||
| 85 | |||
| 86 | $subject = new Booking(); |
||
| 87 | self::assertNull($subject->getOwner()); |
||
| 88 | |||
| 89 | $subject->setOwner($originalOwner); |
||
| 90 | self::assertSame($originalOwner, $subject->getOwner()); |
||
| 91 | |||
| 92 | if ($exception) { |
||
| 93 | $this->expectExceptionMessage($exception); |
||
| 94 | } |
||
| 95 | |||
| 96 | $subject->setOwner($newOwner); |
||
| 97 | self::assertSame($newOwner, $subject->getOwner()); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function providerSetOwner(): array |
||
| 101 | { |
||
| 102 | $u1 = new User(); |
||
| 103 | $u1->setLogin('u1'); |
||
| 104 | $u2 = new User(); |
||
| 105 | $u2->setLogin('u2'); |
||
| 106 | $u3 = new User(); |
||
| 107 | $u3->setLogin('u3'); |
||
| 108 | $admin = new User(User::ROLE_ADMINISTRATOR); |
||
| 109 | $admin->setLogin('admin'); |
||
| 110 | |||
| 111 | return [ |
||
| 112 | 'can change nothing' => [null, null, null], |
||
| 113 | 'can set owner for first time' => [null, null, $u3], |
||
| 114 | 'can set owner for first time to myself' => [$u1, null, $u1], |
||
| 115 | 'can set owner for first time even if it is not myself' => [$u1, null, $u3], |
||
| 116 | 'can donate my stuff' => [$u1, $u1, $u3], |
||
| 117 | 'cannot donate stuff that are not mine' => [$u1, $u2, $u3, 'u1 is not allowed to change owner to u3 because it belongs to u2'], |
||
| 118 | 'admin cannot donate stuff that are not mine' => [$admin, $u2, $u3], |
||
| 119 | ]; |
||
| 120 | } |
||
| 121 | |||
| 122 | public function testIndividualCannotOwnUsers(): void |
||
| 123 | { |
||
| 124 | $u1 = new User(); |
||
| 125 | $u2 = new User(); |
||
| 223 |