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 |
||
| 22 | class ReviewCommandTest extends BaseCommandTestCase |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Data provider for show command. |
||
| 26 | * |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | public function showTestDataProvider() |
||
| 30 | { |
||
| 31 | return [ |
||
| 32 | 'review:show 47640' => ['47640'], |
||
| 33 | 'review:show http://review.typo3.org/#/c/47640/' => ['http://review.typo3.org/#/c/47640/'], |
||
| 34 | 'review:show https://review.typo3.org/#/c/47640/' => ['https://review.typo3.org/#/c/47640/'], |
||
| 35 | 'review:show http://review.typo3.org/#/c/47640/1' => ['http://review.typo3.org/#/c/47640/1'], |
||
| 36 | 'review:show https://review.typo3.org/#/c/47640/1' => ['https://review.typo3.org/#/c/47640/1'], |
||
| 37 | ]; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Data provider for prjectPhase test. |
||
| 42 | * |
||
| 43 | * @return array |
||
| 44 | */ |
||
| 45 | public function projectPhaseDataProvider() |
||
| 46 | { |
||
| 47 | return [ |
||
| 48 | AbstractCommand::PROJECT_PHASE_DEVELOPMENT => [AbstractCommand::PROJECT_PHASE_DEVELOPMENT, ''], |
||
| 49 | AbstractCommand::PROJECT_PHASE_STABILISATION => [AbstractCommand::PROJECT_PHASE_STABILISATION, ':warning: *stabilisation phase*'], |
||
| 50 | AbstractCommand::PROJECT_PHASE_SOFT_FREEZE => [AbstractCommand::PROJECT_PHASE_SOFT_FREEZE, ':no_entry: *soft merge freeze*'], |
||
| 51 | AbstractCommand::PROJECT_PHASE_CODE_FREEZE => [AbstractCommand::PROJECT_PHASE_CODE_FREEZE, ':no_entry: *merge freeze*'], |
||
| 52 | AbstractCommand::PROJECT_PHASE_FEATURE_FREEZE => [AbstractCommand::PROJECT_PHASE_FEATURE_FREEZE, ':no_entry: *FEATURE FREEZE*'], |
||
| 53 | ]; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @test |
||
| 58 | */ |
||
| 59 | public function ensureHelpCommandWorks() |
||
| 60 | { |
||
| 61 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 62 | 'user' => 'U54321', |
||
| 63 | 'text' => 'review:foo', |
||
| 64 | ]); |
||
| 65 | $result = $this->command->process(); |
||
| 66 | static::assertStringStartsWith('*HELP*', $result); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @test |
||
| 71 | */ |
||
| 72 | public function processCountReturnsCorrectOutputForDefaultProject() |
||
| 73 | { |
||
| 74 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 75 | 'user' => 'U54321', |
||
| 76 | 'text' => 'review:count', |
||
| 77 | ]); |
||
| 78 | $result = $this->command->process(); |
||
| 79 | $expectedResult = '/There are currently \*([0-9]*)\* open reviews for project _Packages\/TYPO3.CMS_/'; |
||
| 80 | static::assertRegExp($expectedResult, $result); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @test |
||
| 85 | */ |
||
| 86 | public function processRandomReturnsCorrectOutput() |
||
| 87 | { |
||
| 88 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 89 | 'user' => 'U54321', |
||
| 90 | 'text' => 'review:random', |
||
| 91 | ]); |
||
| 92 | /** @var Message $result */ |
||
| 93 | $result = $this->command->process(); |
||
| 94 | static::assertInstanceOf(Message::class, $result); |
||
| 95 | $attachments = $result->getAttachments(); |
||
| 96 | /** @var Message\Attachment $attachment */ |
||
| 97 | foreach ($attachments as $attachment) { |
||
| 98 | static::assertNotEmpty($attachment->getTitle()); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @test |
||
| 104 | */ |
||
| 105 | public function processUserReturnsCorrectOutputForNoUser() |
||
| 106 | { |
||
| 107 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 108 | 'user' => 'U54321', |
||
| 109 | 'text' => 'review:user', |
||
| 110 | ]); |
||
| 111 | /** @var Message $result */ |
||
| 112 | $result = $this->command->process(); |
||
| 113 | static::assertEquals('hey, I need a username!', $result); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @test |
||
| 118 | */ |
||
| 119 | public function processUserReturnsCorrectOutputForValidUser() |
||
| 120 | { |
||
| 121 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 122 | 'user' => 'U54321', |
||
| 123 | 'text' => 'review:user neoblack', |
||
| 124 | ]); |
||
| 125 | /** @var Message $result */ |
||
| 126 | $result = $this->command->process(); |
||
| 127 | static::assertContains('*Here are the results for neoblack*:', $result); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @test |
||
| 132 | */ |
||
| 133 | public function processUserReturnsCorrectOutputForValidUserWithoutOpenReviews() |
||
| 134 | { |
||
| 135 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 136 | 'user' => 'U54321', |
||
| 137 | 'text' => 'review:user kasper', |
||
| 138 | ]); |
||
| 139 | /** @var Message $result */ |
||
| 140 | $result = $this->command->process(); |
||
| 141 | static::assertContains('kasper has no open reviews or username is unknown', $result); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @test |
||
| 146 | * @dataProvider showTestDataProvider |
||
| 147 | * |
||
| 148 | * @param string $refId |
||
| 149 | */ |
||
| 150 | public function processShowReturnsCorrectOutputForValidRefIds($refId) |
||
| 151 | { |
||
| 152 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 153 | 'user' => 'U54321', |
||
| 154 | 'text' => 'review:show ' . $refId, |
||
| 155 | ]); |
||
| 156 | /** @var Message $result */ |
||
| 157 | $result = $this->command->process(); |
||
| 158 | static::assertInstanceOf(Message::class, $result); |
||
| 159 | $attachments = $result->getAttachments(); |
||
| 160 | /** @var Message\Attachment $attachment */ |
||
| 161 | foreach ($attachments as $attachment) { |
||
| 162 | static::assertEquals('[BUGFIX] Prevent fatal error when uploading file with invalid filename', $attachment->getTitle()); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @test |
||
| 168 | */ |
||
| 169 | public function processShowReturnsCorrectOutputForMultipleValidRefIds() |
||
| 170 | { |
||
| 171 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 172 | 'user' => 'U54321', |
||
| 173 | 'text' => 'review:show 47640 23456', |
||
| 174 | ]); |
||
| 175 | /** @var Message $result */ |
||
| 176 | $result = $this->command->process(); |
||
| 177 | $expectedString = '*[BUGFIX] Prevent fatal error when uploading file with invalid filename* ' |
||
| 178 | . '<https://review.typo3.org/47640|Review #47640 now>' . chr(10); |
||
| 179 | $expectedString .= '*[BUGFIX] Cast autoload and classAliasMap to Array* ' |
||
| 180 | . '<https://review.typo3.org/23456|Review #23456 now>'; |
||
| 181 | static::assertEquals($expectedString, $result); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @test |
||
| 186 | */ |
||
| 187 | public function processShowReturnsCorrectOutputForNoRefIds() |
||
| 188 | { |
||
| 189 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 190 | 'user' => 'U54321', |
||
| 191 | 'text' => 'review:show', |
||
| 192 | ]); |
||
| 193 | /** @var Message $result */ |
||
| 194 | $result = $this->command->process(); |
||
| 195 | $expectedString = 'hey, I need at least one change number!'; |
||
| 196 | static::assertEquals($expectedString, $result); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @test |
||
| 201 | */ |
||
| 202 | public function processShowReturnsCorrectOutputForInvalidRefId() |
||
| 203 | { |
||
| 204 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 205 | 'user' => 'U54321', |
||
| 206 | 'text' => 'review:show x11111', |
||
| 207 | ]); |
||
| 208 | /** @var Message $result */ |
||
| 209 | $result = $this->command->process(); |
||
| 210 | $expectedString = 'hey, I need at least one change number!'; |
||
| 211 | static::assertEquals($expectedString, $result); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @test |
||
| 216 | */ |
||
| 217 | public function processShowReturnsCorrectOutputForUnknownRefId() |
||
| 218 | { |
||
| 219 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 220 | 'user' => 'U54321', |
||
| 221 | 'text' => 'review:show 999999', |
||
| 222 | ]); |
||
| 223 | /** @var Message $result */ |
||
| 224 | $result = $this->command->process(); |
||
| 225 | $expectedString = '999999 not found, sorry!'; |
||
| 226 | static::assertEquals($expectedString, $result); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @test |
||
| 231 | */ |
||
| 232 | public function processQueryReturnsCorrectOutputForNoQuery() |
||
| 233 | { |
||
| 234 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 235 | 'user' => 'U54321', |
||
| 236 | 'text' => 'review:query', |
||
| 237 | ]); |
||
| 238 | /** @var Message $result */ |
||
| 239 | $result = $this->command->process(); |
||
| 240 | $expectedString = 'hey, I need a query!'; |
||
| 241 | static::assertEquals($expectedString, $result); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @test |
||
| 246 | */ |
||
| 247 | public function processQueryReturnsCorrectOutputForTestQuery() |
||
| 248 | { |
||
| 249 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 250 | 'user' => 'U54321', |
||
| 251 | 'text' => 'review:query test', |
||
| 252 | ]); |
||
| 253 | /** @var Message $result */ |
||
| 254 | $result = $this->command->process(); |
||
| 255 | $expectedString = '*Here are the results for test*:'; |
||
| 256 | static::assertStringStartsWith($expectedString, $result); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @test |
||
| 261 | */ |
||
| 262 | public function processQueryReturnsCorrectOutputForValidQueryWithNoResults() |
||
| 263 | { |
||
| 264 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 265 | 'user' => 'U54321', |
||
| 266 | 'text' => 'review:query öäauieqd-asucc3ucbauiscaui-sd', |
||
| 267 | ]); |
||
| 268 | /** @var Message $result */ |
||
| 269 | $result = $this->command->process(); |
||
| 270 | $expectedString = 'öäauieqd-asucc3ucbauiscaui-sd not found, sorry!'; |
||
| 271 | static::assertStringStartsWith($expectedString, $result); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @test |
||
| 276 | */ |
||
| 277 | public function processMergedReturnsCorrectOutputForNoDate() |
||
| 278 | { |
||
| 279 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 280 | 'user' => 'U54321', |
||
| 281 | 'text' => 'review:merged', |
||
| 282 | ]); |
||
| 283 | /** @var Message $result */ |
||
| 284 | $result = $this->command->process(); |
||
| 285 | $expectedString = 'hey, I need a date in the format YYYY-MM-DD!'; |
||
| 286 | static::assertEquals($expectedString, $result); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @test |
||
| 291 | */ |
||
| 292 | public function processMergedReturnsCorrectOutputForInvalidDate() |
||
| 293 | { |
||
| 294 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 295 | 'user' => 'U54321', |
||
| 296 | 'text' => 'review:merged 01.01.2015', |
||
| 297 | ]); |
||
| 298 | /** @var Message $result */ |
||
| 299 | $result = $this->command->process(); |
||
| 300 | $expectedString = 'hey, I need a date in the format YYYY-MM-DD!'; |
||
| 301 | static::assertEquals($expectedString, $result); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @test |
||
| 306 | */ |
||
| 307 | public function processMergedReturnsCorrectOutputForValidDate() |
||
| 308 | { |
||
| 309 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 310 | 'user' => 'U54321', |
||
| 311 | 'text' => 'review:merged 2015-01-01', |
||
| 312 | ]); |
||
| 313 | /** @var Message $result */ |
||
| 314 | $result = $this->command->process(); |
||
| 315 | $expectedString = '/Good job folks, since 2015-01-01 you merged \*([0-9]*)\* patches into master/'; |
||
| 316 | static::assertRegExp($expectedString, $result); |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @test |
||
| 321 | * @dataProvider projectPhaseDataProvider |
||
| 322 | * |
||
| 323 | * @param string $projectPhase |
||
| 324 | * @param string $expectedPretext |
||
| 325 | */ |
||
| 326 | public function processShowWithProjectPhasesReturnsCorrectPretext($projectPhase, $expectedPretext) |
||
| 327 | { |
||
| 328 | $GLOBALS['config']['projectPhase'] = $projectPhase; |
||
| 329 | |||
| 330 | $this->initCommandWithPayload(ReviewCommand::class, [ |
||
| 331 | 'user' => 'U54321', |
||
| 332 | 'text' => 'review:show 47640', |
||
| 333 | ]); |
||
| 334 | /** @var Message $result */ |
||
| 335 | $result = $this->command->process(); |
||
| 336 | static::assertEquals($expectedPretext, $result->getAttachments()[0]->getPretext()); |
||
| 337 | } |
||
| 338 | } |
||
| 339 |