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 |
||
| 7 | class ClaimTest extends PHPUnit_Framework_TestCase |
||
| 8 | { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * testConstructor method |
||
| 12 | */ |
||
| 13 | public function testConstructor() |
||
| 14 | { |
||
| 15 | $ttl = 100; |
||
| 16 | $now = Carbon::now()->timestamp; |
||
| 17 | |||
| 18 | Config::shouldReceive('get') |
||
| 19 | ->once() |
||
| 20 | ->with('app.url') |
||
| 21 | ->andReturn('http://www.test.com'); |
||
| 22 | |||
| 23 | Config::shouldReceive('get') |
||
| 24 | ->once() |
||
| 25 | ->with('jwt.ttl') |
||
| 26 | ->andReturn($ttl); |
||
| 27 | |||
| 28 | Config::shouldReceive('get') |
||
| 29 | ->once() |
||
| 30 | ->with('jwt.ttl') |
||
| 31 | ->andReturn($ttl); |
||
| 32 | |||
| 33 | Config::shouldReceive('get') |
||
| 34 | ->once() |
||
| 35 | ->with('jwt.leeway') |
||
| 36 | ->andReturn(0); |
||
| 37 | |||
| 38 | $claim = new Claim([ |
||
| 39 | 'sub' => 1, |
||
| 40 | 'aud' => 'User' |
||
| 41 | ]); |
||
| 42 | |||
| 43 | $this->assertEquals('http://www.test.com', $claim->iss); |
||
| 44 | $this->assertEquals($now, $claim->iat, '', 2); |
||
| 45 | $this->assertEquals($now + 6000, $claim->exp, '', 2); |
||
| 46 | $this->assertEquals($now + 6000, $claim->nat, '', 2); |
||
| 47 | $this->assertEquals(0, $claim->leeway); |
||
| 48 | $this->assertEquals(false, $claim->refresh); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * testCreateClaim method |
||
| 53 | */ |
||
| 54 | public function testCreateClaim() |
||
| 55 | { |
||
| 56 | $now = Carbon::now()->timestamp; |
||
| 57 | |||
| 58 | Config::shouldReceive('get') |
||
| 59 | ->once() |
||
| 60 | ->with('jwt.leeway') |
||
| 61 | ->andReturn(0); |
||
| 62 | |||
| 63 | $claim = new Claim([ |
||
| 64 | 'sub' => 1, |
||
| 65 | 'aud' => 'User', |
||
| 66 | 'iss' => 'http://www.test.com', |
||
| 67 | 'iat' => $now, |
||
| 68 | 'exp' => $now + 6000, |
||
| 69 | 'nat' => $now + 6000, |
||
| 70 | 'jti' => 'asdjhasiudhasud' |
||
| 71 | ]); |
||
| 72 | |||
| 73 | $this->assertEquals('http://www.test.com', $claim->iss); |
||
| 74 | $this->assertEquals($now, $claim->iat, '', 2); |
||
| 75 | $this->assertEquals($now + 6000, $claim->exp, '', 2); |
||
| 76 | $this->assertEquals($now + 6000, $claim->nat, '', 2); |
||
| 77 | $this->assertEquals('asdjhasiudhasud', $claim->jti); |
||
| 78 | $this->assertEquals(0, $claim->leeway); |
||
| 79 | $this->assertEquals(false, $claim->refresh); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * testCreateClaimWithRefreshable method |
||
| 84 | */ |
||
| 85 | public function testCreateClaimWithRefreshable() |
||
| 86 | { |
||
| 87 | $now = Carbon::now()->timestamp; |
||
| 88 | |||
| 89 | Config::shouldReceive('get') |
||
| 90 | ->once() |
||
| 91 | ->with('jwt.refresh_ttl') |
||
| 92 | ->andReturn(1000); |
||
| 93 | |||
| 94 | Config::shouldReceive('get') |
||
| 95 | ->once() |
||
| 96 | ->with('jwt.leeway') |
||
| 97 | ->andReturn(0); |
||
| 98 | |||
| 99 | $claim = new Claim([ |
||
| 100 | 'sub' => 1, |
||
| 101 | 'aud' => 'User', |
||
| 102 | 'iss' => 'http://www.test.com', |
||
| 103 | 'iat' => $now, |
||
| 104 | 'nat' => $now + 6000, |
||
| 105 | 'jti' => 'asdjhasiudhasud', |
||
| 106 | 'refresh' => true |
||
| 107 | ]); |
||
| 108 | |||
| 109 | $this->assertEquals('http://www.test.com', $claim->iss); |
||
| 110 | $this->assertEquals($now, $claim->iat, '', 2); |
||
| 111 | $this->assertEquals($now + 60000, $claim->exp, '', 2); |
||
| 112 | $this->assertEquals($now + 6000, $claim->nat, '', 2); |
||
| 113 | $this->assertEquals('asdjhasiudhasud', $claim->jti); |
||
| 114 | $this->assertEquals(0, $claim->leeway); |
||
| 115 | $this->assertEquals(true, $claim->refresh); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * testCreateClaimWithMalformedException method |
||
| 120 | */ |
||
| 121 | public function testCreateClaimWithMalformedException() |
||
| 122 | { |
||
| 123 | $now = Carbon::now()->timestamp; |
||
| 124 | |||
| 125 | Config::shouldReceive('get') |
||
| 126 | ->once() |
||
| 127 | ->with('jwt.leeway') |
||
| 128 | ->andReturn(0); |
||
| 129 | |||
| 130 | $this->setExpectedException(\WWON\JwtGuard\Exceptions\MalformedException::class); |
||
| 131 | |||
| 132 | $claim = new Claim([ |
||
| 133 | 'sub' => 1, |
||
| 134 | 'aud' => 'User', |
||
| 135 | 'iss' => 'http://www.test.com', |
||
| 136 | 'iat' => $now, |
||
| 137 | 'exp' => $now - 6000, |
||
| 138 | 'nat' => $now - 6000, |
||
| 139 | 'jti' => 'asdjhasiudhasud' |
||
| 140 | ]); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * testCreateClaimWithInaccessibleException method |
||
| 145 | */ |
||
| 146 | public function testCreateClaimWithInaccessibleException() |
||
| 147 | { |
||
| 148 | $now = Carbon::now()->timestamp; |
||
| 149 | |||
| 150 | Config::shouldReceive('get') |
||
| 151 | ->once() |
||
| 152 | ->with('jwt.leeway') |
||
| 153 | ->andReturn(0); |
||
| 154 | |||
| 155 | $this->setExpectedException(\WWON\JwtGuard\Exceptions\InaccessibleException::class); |
||
| 156 | |||
| 157 | $claim = new Claim([ |
||
| 158 | 'sub' => 1, |
||
| 159 | 'aud' => 'User', |
||
| 160 | 'iss' => 'http://www.test.com', |
||
| 161 | 'iat' => $now - 6000, |
||
| 162 | 'exp' => $now + 6000, |
||
| 163 | 'nat' => $now - 10, |
||
| 164 | 'jti' => 'asdjhasiudhasud' |
||
| 165 | ]); |
||
| 166 | |||
| 167 | $claim->validateAccessible(); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * testCreateClaimWithTokenExpiredException method |
||
| 172 | */ |
||
| 173 | public function testCreateClaimWithTokenExpiredException() |
||
| 174 | { |
||
| 175 | $now = Carbon::now()->timestamp; |
||
| 176 | |||
| 177 | Config::shouldReceive('get') |
||
| 178 | ->once() |
||
| 179 | ->with('jwt.leeway') |
||
| 180 | ->andReturn(0); |
||
| 181 | |||
| 182 | $this->setExpectedException(\WWON\JwtGuard\Exceptions\TokenExpiredException::class); |
||
| 183 | |||
| 184 | $claim = new Claim([ |
||
| 185 | 'sub' => 1, |
||
| 186 | 'aud' => 'User', |
||
| 187 | 'iss' => 'http://www.test.com', |
||
| 188 | 'iat' => $now - 6000, |
||
| 189 | 'exp' => $now - 1000, |
||
| 190 | 'nat' => $now - 1000, |
||
| 191 | 'jti' => 'asdjhasiudhasud' |
||
| 192 | ]); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * testCreateClaimWithNoAudienceAndSubject method |
||
| 197 | */ |
||
| 198 | public function testCreateClaimWithNoAudienceAndSubject() |
||
| 199 | { |
||
| 200 | $now = Carbon::now()->timestamp; |
||
| 201 | |||
| 202 | Config::shouldReceive('get') |
||
| 203 | ->once() |
||
| 204 | ->with('jwt.leeway') |
||
| 205 | ->andReturn(0); |
||
| 206 | |||
| 207 | $this->setExpectedException(\WWON\JwtGuard\Exceptions\MalformedException::class); |
||
| 208 | |||
| 209 | $claim = new Claim([ |
||
| 210 | 'iss' => 'http://www.test.com', |
||
| 211 | 'iat' => $now, |
||
| 212 | 'exp' => $now + 6000, |
||
| 213 | 'nat' => $now + 6000, |
||
| 214 | 'jti' => 'asdjhasiudhasud' |
||
| 215 | ]); |
||
| 216 | } |
||
| 217 | |||
| 218 | } |