Complex classes like RepositoryTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RepositoryTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class RepositoryTest extends TestCase |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * setUp |
||
| 33 | */ |
||
| 34 | public function setUp() |
||
| 35 | { |
||
| 36 | $this->initRepository(); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @covers \GitElephant\Repository::__construct |
||
| 41 | * @covers \GitElephant\Repository::getPath |
||
| 42 | */ |
||
| 43 | public function testConstruct() |
||
| 44 | { |
||
| 45 | $this->assertEquals($this->getRepository()->getPath(), $this->path); |
||
| 46 | |||
| 47 | $this->expectException('GitElephant\Exception\InvalidRepositoryPathException'); |
||
| 48 | $repo = new Repository('non-existent-path'); |
||
|
|
|||
| 49 | |||
| 50 | $repo = Repository::open($this->path); |
||
| 51 | $this->assertInstanceOf('GitElephant\Repository', $repo); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @covers \GitElephant\Repository::init |
||
| 56 | */ |
||
| 57 | public function testInit() |
||
| 58 | { |
||
| 59 | $this->getRepository()->init(); |
||
| 60 | $match = false; |
||
| 61 | |||
| 62 | // Force US/EN locale |
||
| 63 | putenv('LANG=en_US.UTF-8'); |
||
| 64 | |||
| 65 | foreach ($this->getRepository()->getStatusOutput() as $line) { |
||
| 66 | if (preg_match('/nothing to commit?(.*)/', $line)) { |
||
| 67 | $match = true; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | $this->assertTrue($match, 'init problem, git status on an empty repo should give nothing to commit'); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * testName |
||
| 75 | */ |
||
| 76 | public function testName() |
||
| 77 | { |
||
| 78 | $this->getRepository()->setName('test-repo'); |
||
| 79 | $this->assertEquals('test-repo', $this->getRepository()->getName()); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @covers \GitElephant\Repository::stage |
||
| 84 | */ |
||
| 85 | public function testStage() |
||
| 86 | { |
||
| 87 | $this->getRepository()->init(); |
||
| 88 | $this->addFile('test'); |
||
| 89 | $this->getRepository()->stage(); |
||
| 90 | $match = false; |
||
| 91 | foreach ($this->getRepository()->getStatusOutput() as $line) { |
||
| 92 | if (preg_match('/(.*)Changes to be committed(.*)/', $line)) { |
||
| 93 | $match = true; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | $this->assertTrue($match, 'stageAll error, git status should give Changes to be committed'); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @covers \GitElephant\Repository::unstage |
||
| 101 | */ |
||
| 102 | public function testUnstage() |
||
| 103 | { |
||
| 104 | $this->getRepository()->init(); |
||
| 105 | $this->addFile('test'); |
||
| 106 | $this->getRepository()->commit('first commit', true); |
||
| 107 | $this->addFile('test2'); |
||
| 108 | $this->assertCount(1, $this->getRepository()->getStatus()->untracked()); |
||
| 109 | $this->assertCount(0, $this->getRepository()->getStatus()->added()); |
||
| 110 | $this->getRepository()->stage('test2'); |
||
| 111 | $this->assertCount(0, $this->getRepository()->getStatus()->untracked()); |
||
| 112 | $this->assertCount(1, $this->getRepository()->getStatus()->added()); |
||
| 113 | $this->getRepository()->unstage('test2'); |
||
| 114 | $this->assertCount(1, $this->getRepository()->getStatus()->untracked()); |
||
| 115 | $this->assertCount(0, $this->getRepository()->getStatus()->added()); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @covers \GitElephant\Repository::commit |
||
| 120 | * @covers \GitElephant\Repository::getStatusOutput |
||
| 121 | */ |
||
| 122 | public function testCommit() |
||
| 123 | { |
||
| 124 | $this->getRepository()->init(); |
||
| 125 | $this->addFile('test'); |
||
| 126 | $this->getRepository()->stage(); |
||
| 127 | $this->getRepository()->commit('initial import'); |
||
| 128 | $match = false; |
||
| 129 | foreach ($this->getRepository()->getStatusOutput() as $line) { |
||
| 130 | if (preg_match('/nothing to commit?(.*)/', $line)) { |
||
| 131 | $match = true; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | $this->assertTrue($match, 'commit error, git status should give nothing to commit'); |
||
| 135 | |||
| 136 | $this->getRepository()->createBranch('develop', $this->getRepository()->getCommit()); |
||
| 137 | $this->addFile('test2'); |
||
| 138 | $this->getRepository()->commit('commit 2', true, 'develop'); |
||
| 139 | $match = false; |
||
| 140 | foreach ($this->getRepository()->getStatusOutput() as $line) { |
||
| 141 | if (preg_match('/nothing to commit?(.*)/', $line)) { |
||
| 142 | $match = true; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | $this->assertTrue($match, 'commit error, git status should give nothing to commit'); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @covers \GitElephant\Repository::getStatusOutput |
||
| 150 | */ |
||
| 151 | public function testGetStatus() |
||
| 152 | { |
||
| 153 | $this->getRepository()->init(); |
||
| 154 | $this->addFile('test'); |
||
| 155 | $this->getRepository()->commit('test commit', true); |
||
| 156 | $output = $this->getRepository()->getStatusOutput(); |
||
| 157 | $this->assertStringEndsWith('master', $output[0]); |
||
| 158 | $this->addFile('file2'); |
||
| 159 | $output = $this->getRepository()->getStatusOutput(); |
||
| 160 | $this->assertStringEndsWith('file2', $output[4]); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @covers \GitElephant\Repository::createBranch |
||
| 165 | */ |
||
| 166 | public function testCreateBranch() |
||
| 167 | { |
||
| 168 | $this->getRepository()->init(); |
||
| 169 | $this->addFile('test'); |
||
| 170 | $this->getRepository()->commit('foo', true); |
||
| 171 | $this->getRepository()->createBranch('test-branch'); |
||
| 172 | $this->assertEquals(2, count($this->getRepository()->getBranches())); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @covers \GitElephant\Repository::deleteBranch |
||
| 177 | */ |
||
| 178 | public function testDeleteBranch() |
||
| 179 | { |
||
| 180 | $this->getRepository()->init(); |
||
| 181 | $this->addFile('test-file'); |
||
| 182 | $this->getRepository()->commit('test', true); |
||
| 183 | $this->getRepository()->createBranch('branch2'); |
||
| 184 | $this->assertEquals(2, count($this->getRepository()->getBranches(true))); |
||
| 185 | $this->getRepository()->deleteBranch('branch2'); |
||
| 186 | $this->assertEquals(1, count($this->getRepository()->getBranches(true))); |
||
| 187 | $this->addFile('test-file2'); |
||
| 188 | $this->getRepository()->commit('test2', true); |
||
| 189 | $this->getRepository()->createBranch('branch3'); |
||
| 190 | $this->assertEquals(2, count($this->getRepository()->getBranches(true))); |
||
| 191 | $this->getRepository()->deleteBranch('branch3', true); |
||
| 192 | $this->assertEquals(1, count($this->getRepository()->getBranches(true))); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @covers \GitElephant\Repository::getBranches |
||
| 197 | */ |
||
| 198 | public function testGetBranches() |
||
| 199 | { |
||
| 200 | $this->getRepository()->init(); |
||
| 201 | $this->addFile('test'); |
||
| 202 | $this->getRepository()->stage(); |
||
| 203 | $this->getRepository()->commit('initial import', true); |
||
| 204 | $this->assertCount( |
||
| 205 | 1, |
||
| 206 | $this->getRepository()->getBranches(), |
||
| 207 | 'an initialized repository should have only one branch' |
||
| 208 | ); |
||
| 209 | $this->getRepository()->createBranch('test-branch'); |
||
| 210 | $this->assertCount(2, $this->getRepository()->getBranches(), 'two branches expected'); |
||
| 211 | $branches = $this->getRepository()->getBranches(); |
||
| 212 | /** @var Branch $branch */ |
||
| 213 | $branch = $branches[0]; |
||
| 214 | $this->assertEquals('master', $branch->getName()); |
||
| 215 | $this->getRepository()->deleteBranch('test-branch'); |
||
| 216 | $this->assertCount(1, $this->getRepository()->getBranches(), 'one branch expected'); |
||
| 217 | $this->assertInstanceOf( |
||
| 218 | 'GitElephant\Objects\Branch', |
||
| 219 | $this->getRepository()->getMainBranch(), |
||
| 220 | 'main branch should be an instance of Branch' |
||
| 221 | ); |
||
| 222 | $this->assertTrue( |
||
| 223 | $this->getRepository()->getMainBranch()->getCurrent(), |
||
| 224 | 'getCurrent on main branch should be true' |
||
| 225 | ); |
||
| 226 | $this->assertEquals( |
||
| 227 | 'master', |
||
| 228 | $this->getRepository()->getMainBranch()->getName(), |
||
| 229 | 'main branch should be named "master"' |
||
| 230 | ); |
||
| 231 | $this->assertEquals(array('master'), $this->getRepository()->getBranches(true)); |
||
| 232 | $this->getRepository()->createBranch('develop'); |
||
| 233 | $this->assertContains('master', $this->getRepository()->getBranches(true)); |
||
| 234 | $this->assertContains('develop', $this->getRepository()->getBranches(true)); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @covers \GitElephant\Repository::getMainBranch |
||
| 239 | */ |
||
| 240 | public function testGetMainBranch() |
||
| 241 | { |
||
| 242 | $this->getRepository()->init(); |
||
| 243 | $this->addFile('test-file'); |
||
| 244 | $this->getRepository()->commit('test', true); |
||
| 245 | $this->assertEquals('master', $this->getRepository()->getMainBranch()->getName()); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @covers \GitElephant\Repository::getBranch |
||
| 250 | */ |
||
| 251 | public function testGetBranch() |
||
| 252 | { |
||
| 253 | $this->getRepository()->init(); |
||
| 254 | $this->addFile('test-file'); |
||
| 255 | $this->getRepository()->commit('test', true); |
||
| 256 | $this->assertInstanceOf('GitElephant\Objects\Branch', $this->getRepository()->getBranch('master')); |
||
| 257 | $this->assertNull($this->getRepository()->getBranch('a-branch-that-do-not-exists')); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @covers \GitElephant\Repository::merge |
||
| 262 | */ |
||
| 263 | public function testMerge() |
||
| 264 | { |
||
| 265 | $this->getRepository()->init(); |
||
| 266 | $this->addFile('test-file'); |
||
| 267 | $this->getRepository()->commit('test', true); |
||
| 268 | $this->assertEquals(1, count($this->getRepository()->getTree())); |
||
| 269 | $this->getRepository()->createBranch('branch2'); |
||
| 270 | $this->getRepository()->checkout('branch2'); |
||
| 271 | $this->addFile('file2'); |
||
| 272 | $this->getRepository()->commit('test2', true); |
||
| 273 | $this->assertEquals(2, count($this->getRepository()->getTree())); |
||
| 274 | $this->getRepository()->checkout('master'); |
||
| 275 | $this->assertEquals(1, count($this->getRepository()->getTree())); |
||
| 276 | $this->getRepository()->merge($this->getRepository()->getBranch('branch2')); |
||
| 277 | $this->assertEquals(2, count($this->getRepository()->getTree())); |
||
| 278 | |||
| 279 | // attempt to merge a different branch by forcing a 3-way merge and verify the merge commit message |
||
| 280 | $this->getRepository()->createBranch('branch3'); |
||
| 281 | $this->getRepository()->checkout('branch3'); |
||
| 282 | $this->addFile('file3'); |
||
| 283 | $this->getRepository()->commit('test3', true); |
||
| 284 | $this->assertEquals(3, count($this->getRepository()->getTree())); |
||
| 285 | $this->getRepository()->checkout('master'); |
||
| 286 | $this->assertEquals(2, count($this->getRepository()->getTree())); |
||
| 287 | $this->getRepository()->merge($this->getRepository()->getBranch('branch3'), 'test msg', 'no-ff'); |
||
| 288 | $this->assertEquals(3, count($this->getRepository()->getTree())); |
||
| 289 | $this->assertEquals('test msg', $this->getRepository()->getCommit()->getMessage()->getFullMessage()); |
||
| 290 | |||
| 291 | // attempt a fast forward merge where a 3-way is necessary and trap the resulting exception |
||
| 292 | $this->getRepository()->checkout('branch2'); |
||
| 293 | $this->addFile('file4'); |
||
| 294 | $this->getRepository()->commit('test4', true); |
||
| 295 | $this->assertEquals(3, count($this->getRepository()->getTree())); |
||
| 296 | $this->getRepository()->checkout('master'); |
||
| 297 | $this->assertEquals(3, count($this->getRepository()->getTree())); |
||
| 298 | try { |
||
| 299 | $this->getRepository()->merge($this->getRepository()->getBranch('branch2'), '', 'ff-only'); |
||
| 300 | } catch (\RuntimeException $e) { |
||
| 301 | return; |
||
| 302 | } |
||
| 303 | $this->fail("Merge should have produced a runtime exception."); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @covers \GitElephant\Repository::getTags |
||
| 308 | * @covers \GitElephant\Repository::getTag |
||
| 309 | * @covers \GitElephant\Repository::createTag |
||
| 310 | * @covers \GitElephant\Repository::deleteTag |
||
| 311 | */ |
||
| 312 | public function testTags() |
||
| 313 | { |
||
| 314 | $this->getRepository()->init(); |
||
| 315 | $this->addFile('test-file'); |
||
| 316 | $this->getRepository()->commit('test', true); |
||
| 317 | $this->assertEquals(0, count($this->getRepository()->getTags())); |
||
| 318 | $this->getRepository()->createTag('test-tag'); |
||
| 319 | $this->assertEquals(1, count($this->getRepository()->getTags())); |
||
| 320 | $this->assertInstanceOf('GitElephant\Objects\Tag', $this->getRepository()->getTag('test-tag')); |
||
| 321 | $this->getRepository()->deleteTag('test-tag'); |
||
| 322 | $this->assertEquals(0, count($this->getRepository()->getTags())); |
||
| 323 | $this->assertNull($this->getRepository()->getTag('a-tag-that-do-not-exists')); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * test getLastTag |
||
| 328 | */ |
||
| 329 | public function testGetLastTag() |
||
| 330 | { |
||
| 331 | $this->getRepository()->init(); |
||
| 332 | $this->addFile('test-file'); |
||
| 333 | $this->getRepository()->commit('test', true); |
||
| 334 | $this->getRepository()->createTag('0.0.2'); |
||
| 335 | sleep(1); |
||
| 336 | $this->getRepository()->createTag('0.0.4'); |
||
| 337 | sleep(1); |
||
| 338 | $this->getRepository()->createTag('0.0.3'); |
||
| 339 | sleep(1); |
||
| 340 | $this->getRepository()->createTag('0.0.1'); |
||
| 341 | sleep(1); |
||
| 342 | $this->assertEquals(Tag::pick($this->getRepository(), '0.0.1'), $this->getRepository()->getLastTag()); |
||
| 343 | $this->getRepository()->createTag('0.0.05'); |
||
| 344 | $this->assertEquals(Tag::pick($this->getRepository(), '0.0.05'), $this->getRepository()->getLastTag()); |
||
| 345 | $this->getRepository()->deleteTag(Tag::pick($this->getRepository(), '0.0.05')); |
||
| 346 | $this->assertEquals(Tag::pick($this->getRepository(), '0.0.1'), $this->getRepository()->getLastTag()); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @covers \GitElephant\Repository::getCommit |
||
| 351 | */ |
||
| 352 | public function testGetCommit() |
||
| 353 | { |
||
| 354 | $this->getRepository()->init(); |
||
| 355 | $this->addFile('test-file'); |
||
| 356 | $this->getRepository()->commit('test', true); |
||
| 357 | $this->assertInstanceOf('GitElephant\Objects\Commit', $this->getRepository()->getCommit()); |
||
| 358 | } |
||
| 359 | |||
| 360 | public function testGetBranchOrTag() |
||
| 361 | { |
||
| 362 | $this->getRepository()->init(); |
||
| 363 | $this->addFile('test-file'); |
||
| 364 | $this->getRepository()->commit('test', true); |
||
| 365 | $this->getRepository()->createBranch('branch2'); |
||
| 366 | $this->getRepository()->createTag('tag1'); |
||
| 367 | $this->assertInstanceOf('\GitElephant\Objects\Branch', $this->getRepository()->getBranchOrTag('branch2')); |
||
| 368 | $this->assertInstanceOf('\GitElephant\Objects\Tag', $this->getRepository()->getBranchOrTag('tag1')); |
||
| 369 | $this->assertNull($this->getRepository()->getBranchOrTag('not-exists')); |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @covers \GitElephant\Repository::getObjectLog |
||
| 374 | */ |
||
| 375 | public function testGetObjectLog() |
||
| 376 | { |
||
| 377 | $repo = $this->getRepository(); |
||
| 378 | $repo->init(); |
||
| 379 | |||
| 380 | $this->addFolder('test'); |
||
| 381 | |||
| 382 | $this->addFile('A.txt', 'test'); |
||
| 383 | $repo->commit('added A.txt', true); |
||
| 384 | |||
| 385 | $this->addFile('B.txt', 'test'); |
||
| 386 | $repo->commit('added B.txt', true); |
||
| 387 | |||
| 388 | $this->addFile('C.txt', 'test'); |
||
| 389 | $repo->commit('added C.txt', true); |
||
| 390 | |||
| 391 | $this->addFile('D.txt', 'test'); |
||
| 392 | $repo->commit('added D.txt', true); |
||
| 393 | |||
| 394 | $this->addFile('E.txt', 'test'); |
||
| 395 | $repo->commit('added E.txt', true); |
||
| 396 | |||
| 397 | $tree = $repo->getTree(); |
||
| 398 | $obj = $tree[0]; |
||
| 399 | |||
| 400 | $log = $this->getRepository()->getObjectLog($obj); |
||
| 401 | $this->assertInstanceOf('GitElephant\Objects\Log', $log); |
||
| 402 | $this->assertEquals(1, $log->count()); |
||
| 403 | |||
| 404 | $log = $this->getRepository()->getObjectLog($obj, null, null, null); |
||
| 405 | $this->assertEquals(5, $log->count()); |
||
| 406 | |||
| 407 | $this->assertEquals('added E.txt', $log->first()->getMessage()->toString()); |
||
| 408 | $this->assertEquals('added A.txt', $log->last()->getMessage()->toString()); |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Test logs on different tree objects |
||
| 413 | * |
||
| 414 | * @covers \GitElephant\Repository::getObjectLog |
||
| 415 | */ |
||
| 416 | public function testGetObjectLogFolders() |
||
| 417 | { |
||
| 418 | $repo = $this->getRepository(); |
||
| 419 | $repo->init(); |
||
| 420 | |||
| 421 | $this->addFolder('A'); |
||
| 422 | $this->addFile('A1.txt', 'A'); |
||
| 423 | $repo->commit('A/A1', true); |
||
| 424 | |||
| 425 | $this->addFile('A2.txt', 'A'); |
||
| 426 | $repo->commit('A/A2', true); |
||
| 427 | |||
| 428 | $this->addFolder('B'); |
||
| 429 | $this->addFile('B1.txt', 'B'); |
||
| 430 | $repo->commit('B/B1', true); |
||
| 431 | |||
| 432 | $this->addFile('B2.txt', 'B'); |
||
| 433 | $repo->commit('B/B2', true); |
||
| 434 | |||
| 435 | $tree = $repo->getTree(); |
||
| 436 | |||
| 437 | /* @var $treeObj NodeObject */ |
||
| 438 | foreach ($tree as $treeObj) { |
||
| 439 | $name = $treeObj->getName(); |
||
| 440 | $log = $repo->getObjectLog($treeObj, null, null, null); |
||
| 441 | |||
| 442 | $this->assertEquals(2, $log->count()); |
||
| 443 | |||
| 444 | $i = 2; |
||
| 445 | foreach ($log as $commit) { |
||
| 446 | $this->assertEquals($name . '/' . $name . $i, $commit->getMessage()->toString()); |
||
| 447 | --$i; |
||
| 448 | } |
||
| 449 | } |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Test logs on different branches |
||
| 454 | * |
||
| 455 | * @covers \GitElephant\Repository::getObjectLog |
||
| 456 | */ |
||
| 457 | public function testGetObjectLogBranches() |
||
| 458 | { |
||
| 459 | $repo = $this->getRepository(); |
||
| 460 | $repo->init(); |
||
| 461 | |||
| 462 | $this->addFolder('A'); |
||
| 463 | $this->addFile('A1.txt', 'A'); |
||
| 464 | $repo->commit('A/A1', true); |
||
| 465 | |||
| 466 | $this->addFile('A2.txt', 'A'); |
||
| 467 | $repo->commit('A/A2', true); |
||
| 468 | |||
| 469 | $repo->createBranch('test-branch'); |
||
| 470 | $repo->checkout('test-branch'); |
||
| 471 | |||
| 472 | $this->addFile('A3.txt', 'A'); |
||
| 473 | $repo->commit('A/A3', true); |
||
| 474 | |||
| 475 | // master branch |
||
| 476 | $repo->checkout('master'); |
||
| 477 | $tree = $repo->getTree(); |
||
| 478 | $dir = $tree[0]; |
||
| 479 | $log = $repo->getObjectLog($dir, null, null, null); |
||
| 480 | |||
| 481 | $this->assertEquals(2, $log->count()); |
||
| 482 | $this->assertEquals('A/A2', $log->first()->getMessage()->toString()); |
||
| 483 | |||
| 484 | // test branch |
||
| 485 | $repo->checkout('test-branch'); |
||
| 486 | $tree = $repo->getTree(); |
||
| 487 | $dir = $tree[0]; |
||
| 488 | $log = $repo->getObjectLog($dir, null, null, null); |
||
| 489 | |||
| 490 | $this->assertEquals(3, $log->count()); |
||
| 491 | $this->assertEquals('A/A3', $log->first()->getMessage()->toString()); |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @covers \GitElephant\Repository::getLog |
||
| 496 | */ |
||
| 497 | public function testGetLog() |
||
| 498 | { |
||
| 499 | $this->getRepository()->init(); |
||
| 500 | |||
| 501 | for ($i = 0; $i < 50; $i++) { |
||
| 502 | $this->addFile('test file ' . $i); |
||
| 503 | $this->getRepository()->commit('test commit ' . $i, true); |
||
| 504 | } |
||
| 505 | |||
| 506 | $log = $this->getRepository()->getLog(); |
||
| 507 | $this->assertInstanceOf('GitElephant\Objects\Log', $this->getRepository()->getLog()); |
||
| 508 | $this->assertGreaterThan(0, $log->count()); |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @covers \GitElephant\Repository::getLog |
||
| 513 | */ |
||
| 514 | public function testGetLog_for_a_branch() |
||
| 515 | { |
||
| 516 | $this->getRepository()->init(); |
||
| 517 | $this->addFile('test file 0'); |
||
| 518 | $this->getRepository()->commit('first commit', true); |
||
| 519 | $this->getRepository()->checkout('test-branch', true); |
||
| 520 | |||
| 521 | for ($i = 1; $i <= 2; $i++) { |
||
| 522 | $this->addFile('test file ' . $i); |
||
| 523 | $this->getRepository()->commit('test commit ' . $i, true); |
||
| 524 | } |
||
| 525 | |||
| 526 | $log = $this->getRepository()->getLog(array('test-branch', '^master')); |
||
| 527 | $this->assertInstanceOf('GitElephant\Objects\Log', $this->getRepository()->getLog()); |
||
| 528 | $this->assertEquals(2, $log->count()); |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @covers \GitElephant\Repository::checkout |
||
| 533 | */ |
||
| 534 | public function testCheckout() |
||
| 535 | { |
||
| 536 | $this->getRepository()->init(); |
||
| 537 | $this->addFile('test-file'); |
||
| 538 | $this->getRepository()->commit('test', true); |
||
| 539 | $this->assertEquals('master', $this->getRepository()->getMainBranch()->getName()); |
||
| 540 | $this->getRepository()->createBranch('branch2'); |
||
| 541 | $this->getRepository()->checkout('branch2'); |
||
| 542 | $this->assertEquals('branch2', $this->getRepository()->getMainBranch()->getName()); |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @covers \GitElephant\Repository::checkout |
||
| 547 | */ |
||
| 548 | public function testCheckoutTag() |
||
| 549 | { |
||
| 550 | $this->getRepository()->init(); |
||
| 551 | $this->addFile('test-file'); |
||
| 552 | $this->getRepository()->commit('test', true); |
||
| 553 | $this->getRepository()->createTag('v0.0.1'); |
||
| 554 | $this->addFile('test-file2'); |
||
| 555 | $this->getRepository()->commit('test2', true); |
||
| 556 | $tag = $this->getRepository()->getTag('v0.0.1'); |
||
| 557 | $this->assertInstanceOf('GitElephant\Objects\Tag', $tag); |
||
| 558 | $lastCommit = $this->getRepository()->getCommit(); |
||
| 559 | $this->assertNotContains('detached', implode(' ', $this->getRepository()->getStatusOutput())); |
||
| 560 | $this->getRepository()->checkout($tag); |
||
| 561 | $newCommit = $this->getRepository()->getCommit(); |
||
| 562 | $this->assertNotEquals($newCommit->getSha(), $lastCommit->getSha()); |
||
| 563 | $this->assertContains('detached', implode(' ', $this->getRepository()->getStatusOutput())); |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @covers \GitElephant\Repository::getTree |
||
| 568 | * @covers \GitElephant\Objects\Tree |
||
| 569 | */ |
||
| 570 | public function testGetTree() |
||
| 571 | { |
||
| 572 | $this->getRepository()->init(); |
||
| 573 | $this->addFile('test'); |
||
| 574 | $this->addFolder('test-folder'); |
||
| 575 | $this->addFile('test2', 'test-folder'); |
||
| 576 | |||
| 577 | $this->getRepository()->stage(); |
||
| 578 | $this->getRepository()->commit('initial import'); |
||
| 579 | |||
| 580 | $tree = $this->getRepository()->getTree(); |
||
| 581 | $this->assertFalse($tree->isBlob()); |
||
| 582 | $this->assertTrue($this->getRepository()->getTree($this->getRepository()->getCommit(), 'test')->isBlob()); |
||
| 583 | $this->assertCount(2, $tree, 'One file in the repository'); |
||
| 584 | $firstNode = $tree[0]; |
||
| 585 | $this->assertInstanceOf( |
||
| 586 | 'GitElephant\Objects\NodeObject', |
||
| 587 | $firstNode, |
||
| 588 | 'array access on tree should give always a node type' |
||
| 589 | ); |
||
| 590 | $this->assertEquals( |
||
| 591 | 'test-folder', |
||
| 592 | $firstNode->getName(), |
||
| 593 | 'First repository file should be named "test"' |
||
| 594 | ); |
||
| 595 | $secondNode = $tree[1]; |
||
| 596 | $this->assertInstanceOf( |
||
| 597 | 'GitElephant\Objects\NodeObject', |
||
| 598 | $secondNode, |
||
| 599 | 'array access on tree should give always a node type' |
||
| 600 | ); |
||
| 601 | $this->assertEquals( |
||
| 602 | NodeObject::TYPE_BLOB, |
||
| 603 | $secondNode->getType(), |
||
| 604 | 'second node should be of type tree' |
||
| 605 | ); |
||
| 606 | $subtree = $this->getRepository()->getTree('master', 'test-folder'); |
||
| 607 | $subnode = $subtree[0]; |
||
| 608 | $this->assertInstanceOf( |
||
| 609 | 'GitElephant\Objects\NodeObject', |
||
| 610 | $subnode, |
||
| 611 | 'array access on tree should give always a node type' |
||
| 612 | ); |
||
| 613 | $this->assertEquals( |
||
| 614 | NodeObject::TYPE_BLOB, |
||
| 615 | $subnode->getType(), |
||
| 616 | 'subnode should be of type blob' |
||
| 617 | ); |
||
| 618 | $this->assertEquals( |
||
| 619 | 'test2', |
||
| 620 | $subnode->getName(), |
||
| 621 | 'subnode should be named "test2"' |
||
| 622 | ); |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @covers \GitElephant\Repository::getDiff |
||
| 627 | */ |
||
| 628 | public function testGetDiff() |
||
| 629 | { |
||
| 630 | $this->getRepository()->init(); |
||
| 631 | $this->addFile('test-file'); |
||
| 632 | $this->getRepository()->commit('commit 1', true); |
||
| 633 | $commit1 = $this->getRepository()->getCommit(); |
||
| 634 | $this->assertInstanceOf('GitElephant\Objects\Diff\Diff', $this->getRepository()->getDiff($commit1)); |
||
| 635 | $this->addFile('test-file2'); |
||
| 636 | $this->getRepository()->commit('commit 2', true); |
||
| 637 | $commit2 = $this->getRepository()->getCommit(); |
||
| 638 | $this->assertInstanceOf('GitElephant\Objects\Diff\Diff', $this->getRepository()->getDiff($commit2)); |
||
| 639 | $this->assertInstanceOf('GitElephant\Objects\Diff\Diff', $this->getRepository()->getDiff($commit2, $commit1)); |
||
| 640 | $shaHead = $this->getRepository()->getCommit(); |
||
| 641 | $this->assertInstanceOf('GitElephant\Objects\Diff\Diff', $diff = $this->getRepository()->getDiff($shaHead)); |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * testCloneFrom |
||
| 646 | */ |
||
| 647 | public function testCloneFrom() |
||
| 648 | { |
||
| 649 | $this->initRepository(null, 0); |
||
| 650 | $this->initRepository(null, 1); |
||
| 651 | $remote = $this->getRepository(0); |
||
| 652 | $remote->init(); |
||
| 653 | $this->addFile('test', null, null, $remote); |
||
| 654 | $remote->commit('test', true); |
||
| 655 | $local = $this->getRepository(1); |
||
| 656 | $local->cloneFrom($remote->getPath(), '.'); |
||
| 657 | $commit = $local->getCommit(); |
||
| 658 | $this->assertEquals($remote->getCommit()->getSha(), $commit->getSha()); |
||
| 659 | $this->assertEquals($remote->getCommit()->getMessage(), $commit->getMessage()); |
||
| 660 | } |
||
| 661 | |||
| 662 | /** |
||
| 663 | * testOutputContent |
||
| 664 | */ |
||
| 665 | public function testOutputContent() |
||
| 666 | { |
||
| 667 | $this->initRepository(); |
||
| 668 | $this->getRepository()->init(); |
||
| 669 | $this->addFile('file1', null, 'file content'); |
||
| 670 | $this->getRepository()->commit('first commit', true); |
||
| 671 | $branch = $this->getRepository()->getBranch('master'); |
||
| 672 | $tree = $this->getRepository()->getTree($branch, 'file1'); |
||
| 673 | $treeObject = $tree->getBlob(); |
||
| 674 | $this->assertEquals(array('file content'), $this->getRepository()->outputContent($treeObject, $branch)); |
||
| 675 | } |
||
| 676 | |||
| 677 | /** |
||
| 678 | * testMove |
||
| 679 | */ |
||
| 680 | public function testMove() |
||
| 681 | { |
||
| 682 | $this->getRepository()->init(); |
||
| 683 | $this->addFile('foo'); |
||
| 684 | $this->getRepository()->commit('commit 1', true); |
||
| 685 | $this->getRepository()->move('foo', 'bar'); |
||
| 686 | $status = $this->getRepository()->getStatusOutput(); |
||
| 687 | |||
| 688 | $this->assertRegExp('/(.*): foo -> bar/', $status[4]); |
||
| 689 | } |
||
| 690 | |||
| 691 | /** |
||
| 692 | * testRemove |
||
| 693 | */ |
||
| 694 | public function testRemove() |
||
| 695 | { |
||
| 696 | $this->getRepository()->init(); |
||
| 697 | $this->addFile('foo'); |
||
| 698 | $this->getRepository()->commit('commit 1', true); |
||
| 699 | $this->getRepository()->remove('foo'); |
||
| 700 | $status = $this->getRepository()->getStatusOutput(); |
||
| 701 | |||
| 702 | $this->assertRegExp('/(.*): foo/', $status[4]); |
||
| 703 | } |
||
| 704 | |||
| 705 | /** |
||
| 706 | * testCountCommits |
||
| 707 | */ |
||
| 708 | public function testCountCommits() |
||
| 709 | { |
||
| 710 | $this->getRepository()->init(); |
||
| 711 | $this->addFile('foo'); |
||
| 712 | $this->getRepository()->commit('commit 1', true); |
||
| 713 | $this->assertEquals(1, $this->getRepository()->countCommits()); |
||
| 714 | $this->addFile('foo2'); |
||
| 715 | $this->getRepository()->commit('commit 2', true); |
||
| 716 | $this->assertEquals(2, $this->getRepository()->countCommits()); |
||
| 717 | $this->getRepository()->createBranch('new-branch'); |
||
| 718 | $this->getRepository()->checkout('new-branch'); |
||
| 719 | $this->assertEquals(2, $this->getRepository()->countCommits()); |
||
| 720 | $this->addFile('bar'); |
||
| 721 | $this->getRepository()->commit('commit 3', true); |
||
| 722 | $this->assertEquals(3, $this->getRepository()->countCommits()); |
||
| 723 | $this->getRepository()->checkout('master'); |
||
| 724 | $this->assertEquals(2, $this->getRepository()->countCommits()); |
||
| 725 | } |
||
| 726 | |||
| 727 | /** |
||
| 728 | * testHumanishName |
||
| 729 | */ |
||
| 730 | public function testHumanishName() |
||
| 731 | { |
||
| 732 | $this->initRepository('test-dir'); |
||
| 733 | $this->assertEquals('test-dir', $this->getRepository()->getHumanishName()); |
||
| 734 | } |
||
| 735 | |||
| 736 | /** |
||
| 737 | * testCreateFromRemote |
||
| 738 | * |
||
| 739 | */ |
||
| 740 | public function testCreateFromRemote() |
||
| 741 | { |
||
| 742 | $this->initRepository(null, 0); |
||
| 743 | $remote = $this->getRepository(0); |
||
| 744 | $remote->init(); |
||
| 745 | $this->addFile('test', null, null, $remote); |
||
| 746 | $remote->commit('test', true); |
||
| 747 | $remote->createBranch('develop'); |
||
| 748 | |||
| 749 | $repo = Repository::createFromRemote($remote->getPath()); |
||
| 750 | $this->assertInstanceOf('GitElephant\Repository', $repo); |
||
| 751 | $this->assertGreaterThanOrEqual(2, $repo->getBranches()); |
||
| 752 | $branches = $repo->getBranches(); |
||
| 753 | $branchesName = array_map( |
||
| 754 | function (Branch $b) { |
||
| 755 | return $b->getName(); |
||
| 756 | }, |
||
| 757 | $branches |
||
| 758 | ); |
||
| 759 | $this->assertContains('master', $branchesName); |
||
| 760 | $this->assertContains('develop', $branchesName); |
||
| 761 | } |
||
| 762 | |||
| 763 | /** |
||
| 764 | * testAddRemote |
||
| 765 | */ |
||
| 766 | public function testRemote() |
||
| 767 | { |
||
| 768 | $this->initRepository(null, 0); |
||
| 769 | $remote = $this->getRepository(0); |
||
| 770 | $remote->init(true); |
||
| 771 | $this->initRepository(); |
||
| 772 | $this->repository->init(); |
||
| 773 | $this->repository->addRemote('github', $remote->getPath()); |
||
| 774 | $this->assertInstanceOf('GitElephant\Objects\Remote', $this->repository->getRemote('github')); |
||
| 775 | $this->repository->addRemote('github2', $remote->getPath()); |
||
| 776 | $this->assertCount(2, $this->repository->getRemotes()); |
||
| 777 | } |
||
| 778 | |||
| 779 | /** |
||
| 780 | * testFetch, git branch -a should find the branch |
||
| 781 | */ |
||
| 782 | public function testFetch() |
||
| 803 | |||
| 804 | /** |
||
| 805 | * test pull |
||
| 806 | */ |
||
| 807 | public function testPull() |
||
| 808 | { |
||
| 809 | $this->initRepository(null, 0); |
||
| 810 | $this->initRepository(null, 1); |
||
| 811 | $r1 = $this->getRepository(0); |
||
| 812 | $r1->init(); |
||
| 813 | $this->addFile('test1', null, null, $r1); |
||
| 814 | $r1->commit('test commit', true); |
||
| 815 | $r2 = $this->getRepository(1); |
||
| 816 | $r2->init(); |
||
| 817 | $r2->addRemote('origin', $r1->getPath()); |
||
| 818 | $r2->pull('origin', 'master'); |
||
| 819 | $this->assertEquals('test commit', $r2->getLog()->last()->getMessage()); |
||
| 820 | $this->assertEquals($r1->getMainBranch()->getSha(), $r2->getLog()->last()->getSha()); |
||
| 821 | } |
||
| 822 | |||
| 823 | /** |
||
| 824 | * test pull |
||
| 825 | */ |
||
| 826 | public function testPush() |
||
| 827 | { |
||
| 828 | $this->initRepository(null, 0); |
||
| 829 | $this->initRepository(null, 1); |
||
| 830 | $this->initRepository(null, 2); |
||
| 831 | // commit on r1 |
||
| 832 | $r1 = $this->getRepository(0); |
||
| 833 | $r1->init(); |
||
| 834 | $this->addFile('test1', null, null, $r1); |
||
| 835 | $r1->commit('test commit', true); |
||
| 836 | // push from r1 to r2 |
||
| 837 | $r2 = $this->getRepository(1); |
||
| 838 | $r2->init(true); |
||
| 839 | $r1->addRemote('origin', $r2->getPath()); |
||
| 840 | $r1->push('origin', 'master'); |
||
| 841 | // pull from r2 to r3 should get the same result |
||
| 842 | $r3 = $this->getRepository(2); |
||
| 843 | $r3->init(); |
||
| 844 | $r3->addRemote('origin', $r2->getPath()); |
||
| 845 | $r3->pull('origin', 'master'); |
||
| 846 | |||
| 847 | $this->assertEquals('test commit', $r3->getLog()->last()->getMessage()); |
||
| 848 | $this->assertEquals($r1->getMainBranch()->getSha(), $r3->getLog()->last()->getSha()); |
||
| 849 | } |
||
| 850 | |||
| 851 | public function testRevParse() |
||
| 862 | |||
| 863 | public function testIsBare() |
||
| 878 | |||
| 879 | /** |
||
| 880 | * test add, remove and get global configs |
||
| 881 | * |
||
| 882 | * @covers \GitElephant\Repository::addGlobalConfig |
||
| 883 | * @covers \GitElephant\Repository::getGlobalConfigs |
||
| 884 | * @covers \GitElephant\Repository::removeGlobalConfig |
||
| 885 | */ |
||
| 886 | public function testGlobalConfigs() |
||
| 907 | |||
| 908 | /** |
||
| 909 | * test reset |
||
| 910 | */ |
||
| 911 | public function testResetHard() |
||
| 912 | { |
||
| 913 | $this->initRepository(); |
||
| 914 | $repo=$this->getRepository(); |
||
| 915 | $repo->init(); |
||
| 916 | $this->addFile('file1'); |
||
| 917 | $repo->stage(); |
||
| 918 | $repo->commit('message1'); |
||
| 919 | $headCommit=$repo->getCommit(); |
||
| 920 | $this->addFile('file2'); |
||
| 921 | $repo->stage(); |
||
| 922 | $repo->commit('message2'); |
||
| 923 | |||
| 924 | $this->assertEquals(2,$repo->countCommits()); |
||
| 925 | $repo->reset($headCommit,array(ResetCommand::OPTION_HARD)); |
||
| 926 | $this->assertEquals(1,$repo->countCommits()); |
||
| 927 | $this->assertEmpty($repo->getIndexStatus()->added()); |
||
| 928 | } |
||
| 929 | |||
| 930 | /** |
||
| 931 | * test reset |
||
| 932 | */ |
||
| 933 | public function testResetSoft() |
||
| 934 | { |
||
| 935 | $this->initRepository(); |
||
| 936 | $repo=$this->getRepository(); |
||
| 937 | $repo->init(); |
||
| 938 | $this->addFile('file1'); |
||
| 939 | $repo->stage(); |
||
| 940 | $repo->commit('message1'); |
||
| 941 | $headCommit=$repo->getCommit(); |
||
| 942 | $this->addFile('file2'); |
||
| 943 | $repo->stage(); |
||
| 944 | $repo->commit('message2'); |
||
| 945 | |||
| 946 | $this->assertEquals(2,$repo->countCommits()); |
||
| 947 | $repo->reset($headCommit,array(ResetCommand::OPTION_SOFT)); |
||
| 948 | $this->assertEquals(1,$repo->countCommits()); |
||
| 949 | $this->assertNotEmpty($repo->getIndexStatus()->added()); |
||
| 950 | } |
||
| 951 | |||
| 952 | /** |
||
| 953 | * test add, remove and get global options |
||
| 954 | * |
||
| 955 | * @covers \GitElephant\Repository::addGlobalOption |
||
| 956 | * @covers \GitElephant\Repository::getGlobalOptions |
||
| 957 | * @covers \GitElephant\Repository::removeGlobalOption |
||
| 958 | */ |
||
| 959 | public function testGlobalOptions() |
||
| 980 | |||
| 981 | /** |
||
| 982 | * test add, remove and get global command arguments |
||
| 983 | * |
||
| 984 | * @covers \GitElephant\Repository::addGlobalCommandArgument |
||
| 985 | * @covers \GitElephant\Repository::getGlobalCommandArguments |
||
| 986 | * @covers \GitElephant\Repository::removeGlobalCommandArgument |
||
| 987 | */ |
||
| 988 | public function testGlobalCommandArguments() |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * @covers \GitElephant\Repository::stash |
||
| 1012 | */ |
||
| 1013 | public function testStashThrowsExceptionIfNoCommits() |
||
| 1014 | { |
||
| 1015 | $this->getRepository()->init(); |
||
| 1016 | $this->addFile('test'); |
||
| 1017 | |||
| 1018 | $this->setExpectedException('RuntimeException'); |
||
| 1019 | $this->getRepository()->stash('My stash', true); |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | /** |
||
| 1023 | * @covers \GitElephant\Repository::stash |
||
| 1024 | */ |
||
| 1025 | public function testStash() |
||
| 1026 | { |
||
| 1027 | $this->getRepository()->init(); |
||
| 1028 | $this->addFile('test'); |
||
| 1029 | $this->getRepository()->commit('Test commit', true); |
||
| 1030 | $this->addFile('test2'); |
||
| 1031 | $this->getRepository()->stash('My stash', true); |
||
| 1032 | $this->assertTrue($this->getRepository()->isClean()); |
||
| 1033 | $stashList = $this->getRepository()->stashList(); |
||
| 1034 | $this->assertEquals(1, preg_match('%My stash%', $stashList[0])); |
||
| 1035 | } |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * @covers \GitElephant\Repository::stashList |
||
| 1039 | */ |
||
| 1040 | public function testStashList() |
||
| 1041 | { |
||
| 1042 | $this->getRepository()->init(); |
||
| 1043 | $this->addFile('test'); |
||
| 1044 | $this->getRepository()->commit('Test commit', true); |
||
| 1045 | $this->addFile('test2'); |
||
| 1046 | $this->getRepository()->stash('My stash', true); |
||
| 1047 | $this->assertCount(1, $this->getRepository()->stashList()); |
||
| 1048 | } |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * @covers \GitElephant\Repository::stashShow |
||
| 1052 | */ |
||
| 1053 | public function testStashShow() |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * @covers \GitElephant\Repository::stashDrop |
||
| 1065 | */ |
||
| 1066 | public function testStashDrop() |
||
| 1076 | |||
| 1077 | /** |
||
| 1078 | * @covers \GitElephant\Repository::stashPop |
||
| 1079 | */ |
||
| 1080 | public function testStashPop() |
||
| 1091 | |||
| 1092 | /** |
||
| 1093 | * @covers \GitElephant\Repository::stashApply |
||
| 1094 | */ |
||
| 1095 | public function testStashApply() |
||
| 1106 | |||
| 1107 | /** |
||
| 1108 | * @covers \GitElephant\Repository::stashBranch |
||
| 1109 | */ |
||
| 1110 | public function testStashBranch() |
||
| 1120 | |||
| 1121 | /** |
||
| 1122 | * @covers \GitElephant\Repository::stashCreate |
||
| 1123 | */ |
||
| 1124 | public function testStashCreate() |
||
| 1132 | |||
| 1133 | /** |
||
| 1134 | * @covers \GitElephant\Repository::stashCreate |
||
| 1135 | */ |
||
| 1136 | public function testStashClear() |
||
| 1148 | |||
| 1149 | } |
||
| 1150 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.