Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 12 | class AuthTest extends TestCase |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var Auth|MockObject |
||
| 16 | */ |
||
| 17 | protected $auth; |
||
| 18 | |||
| 19 | public function setUp() |
||
| 20 | { |
||
| 21 | $this->auth = $this->getMockForAbstractClass(Auth::class); |
||
| 22 | } |
||
| 23 | |||
| 24 | |||
| 25 | public function testHashPassword() |
||
| 26 | { |
||
| 27 | $hash = $this->auth->hashPassword('abc'); |
||
| 28 | $this->assertTrue(password_verify('abc', $hash)); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function invalidPasswordProvider() |
||
| 32 | { |
||
| 33 | return [ |
||
| 34 | [''], |
||
| 35 | [array()], |
||
| 36 | [123] |
||
| 37 | ]; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @dataProvider invalidPasswordProvider |
||
| 42 | * @expectedException \InvalidArgumentException |
||
| 43 | */ |
||
| 44 | public function testHashPasswordWithInvalidArgument($password) |
||
| 45 | { |
||
| 46 | $this->auth->hashPassword($password); |
||
| 47 | } |
||
| 48 | |||
| 49 | |||
| 50 | public function testVerifyCredentials() |
||
| 51 | { |
||
| 52 | $hash = password_hash('abc', PASSWORD_BCRYPT); |
||
| 53 | |||
| 54 | $user = $this->createMock(Auth\User::class); |
||
| 55 | $user->method('getHashedPassword')->willReturn($hash); |
||
| 56 | |||
| 57 | $this->assertTrue($this->auth->verifyCredentials($user, 'abc')); |
||
| 58 | |||
| 59 | $this->assertFalse($this->auth->verifyCredentials($user, 'god')); |
||
| 60 | $this->assertFalse($this->auth->verifyCredentials($user, '')); |
||
| 61 | } |
||
| 62 | |||
| 63 | |||
| 64 | public function testUserWithoutSessionUser() |
||
| 65 | { |
||
| 66 | $user = $this->createMock(Auth\User::class); |
||
| 67 | $user->expects($this->never())->method('onLogin'); |
||
| 68 | |||
| 69 | $this->auth->expects($this->once())->method('getCurrentUserId')->willReturn(123); |
||
| 70 | $this->auth->expects($this->once())->method('fetchUserById')->with(123)->willReturn($user); |
||
| 71 | |||
| 72 | $this->assertSame($user, $this->auth->user()); |
||
| 73 | } |
||
| 74 | |||
| 75 | public function testUserWithSessionUser() |
||
| 76 | { |
||
| 77 | $this->assertNull($this->auth->user()); |
||
| 78 | } |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * @return Auth|MockObject |
||
| 83 | */ |
||
| 84 | public function testSetUser() |
||
| 85 | { |
||
| 86 | $user = $this->createMock(Auth\User::class); |
||
| 87 | |||
| 88 | $this->auth->expects($this->once())->method('persistCurrentUser'); |
||
| 89 | |||
| 90 | $result = $this->auth->setUser($user); |
||
| 91 | |||
| 92 | $this->assertSame($user, $result); |
||
| 93 | $this->assertSame($user, $this->auth->user()); |
||
| 94 | |||
| 95 | return $this->auth; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function testSetUserWithExistingUser() |
||
| 99 | { |
||
| 100 | $this->auth->expects($this->exactly(2))->method('persistCurrentUser'); |
||
| 101 | |||
| 102 | $this->auth->setUser($this->createMock(Auth\User::class)); |
||
| 103 | |||
| 104 | $user = $this->createMock(Auth\User::class); |
||
| 105 | $user->expects($this->once())->method('onLogin'); |
||
| 106 | |||
| 107 | $result = $this->auth->setUser($user); |
||
| 108 | |||
| 109 | $this->assertSame($user, $result); |
||
| 110 | $this->assertSame($user, $this->auth->user()); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function testSetUserWithOnLoginFail() |
||
| 114 | { |
||
| 115 | $this->auth->expects($this->once())->method('persistCurrentUser'); |
||
| 116 | |||
| 117 | $oldUser = $this->createMock(Auth\User::class); |
||
| 118 | $this->auth->setUser($oldUser); |
||
| 119 | |||
| 120 | $user = $this->createMock(Auth\User::class); |
||
| 121 | $user->expects($this->once())->method('onLogin')->willReturn(false); |
||
| 122 | |||
| 123 | $this->auth->expects($this->never())->method('persistCurrentUser'); |
||
| 124 | |||
| 125 | $result = $this->auth->setUser($user); |
||
| 126 | |||
| 127 | $this->assertNull($result); |
||
| 128 | $this->assertSame($oldUser, $this->auth->user()); |
||
| 129 | } |
||
| 130 | |||
| 131 | |||
| 132 | public function testLogin() |
||
| 133 | { |
||
| 134 | $hash = password_hash('abc', PASSWORD_BCRYPT); |
||
| 135 | |||
| 136 | $user = $this->createMock(Auth\User::class); |
||
| 137 | $user->method('getHashedPassword')->willReturn($hash); |
||
| 138 | $user->expects($this->once())->method('onLogin'); |
||
| 139 | |||
| 140 | $this->auth->expects($this->once())->method('fetchUserByUsername')->with('john')->willReturn($user); |
||
| 141 | $this->auth->expects($this->once())->method('persistCurrentUser'); |
||
| 142 | |||
| 143 | $result = $this->auth->login('john', 'abc'); |
||
| 144 | |||
| 145 | $this->assertSame($user, $result); |
||
| 146 | $this->assertSame($user, $this->auth->user()); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function testLoginWithIncorrectPassword() |
||
| 150 | { |
||
| 151 | $hash = password_hash('abc', PASSWORD_BCRYPT); |
||
| 152 | |||
| 153 | $user = $this->createMock(Auth\User::class); |
||
| 154 | $user->method('getHashedPassword')->willReturn($hash); |
||
| 155 | $user->expects($this->never())->method('onLogin'); |
||
| 156 | |||
| 157 | $this->auth->expects($this->once())->method('fetchUserByUsername')->with('john')->willReturn($user); |
||
| 158 | $this->auth->expects($this->never())->method('persistCurrentUser'); |
||
| 159 | |||
| 160 | $result = $this->auth->login('john', 'god'); |
||
| 161 | |||
| 162 | $this->assertNull($result); |
||
| 163 | $this->assertNull($this->auth->user()); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function testLogout() |
||
| 167 | { |
||
| 168 | $user = $this->createMock(Auth\User::class); |
||
| 169 | $user->expects($this->once())->method('onLogout'); |
||
| 170 | |||
| 171 | $this->auth->setUser($user); |
||
| 172 | |||
| 173 | $this->auth->logout(); |
||
| 174 | |||
| 175 | $this->assertNull($this->auth->user()); |
||
| 176 | |||
| 177 | // Logout again shouldn't really do anything |
||
| 178 | $this->auth->logout(); |
||
| 179 | } |
||
| 180 | } |
||
| 181 |