RepositoryTest   F
last analyzed

Complexity

Total Complexity 74

Size/Duplication

Total Lines 1126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 74
lcom 1
cbo 9
dl 0
loc 1126
rs 1.576
c 0
b 0
f 0

55 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testConstruct() 0 10 1
A testInit() 0 15 3
A testName() 0 5 1
A testStage() 0 13 3
A testUnstage() 0 15 1
A testCommit() 0 31 5
A testGetStatus() 0 11 1
A testCreateBranch() 0 8 1
A testDeleteBranch() 0 16 1
A testGetBranches() 0 38 1
A testGetMainBranch() 0 7 1
A testGetBranch() 0 8 1
A testMerge() 0 42 2
A testTags() 0 13 1
A testGetLastTag() 0 21 1
A testGetCommit() 0 7 1
A testGetBranchOrTag() 0 11 1
A testGetObjectLog() 0 36 1
A testGetObjectLogFolders() 0 35 3
A testGetObjectLogBranches() 0 36 1
A testGetLog() 0 13 2
A testGetLogForBranch() 0 16 2
A testCheckout() 0 10 1
A testCheckoutTag() 0 17 1
A testGetTree() 0 54 1
A testGetDiff() 0 15 1
A testCloneFrom() 0 14 1
A testOutputContent() 0 11 1
A testMove() 0 9 1
A testRemove() 0 10 1
A testCountCommits() 0 18 1
A testHumanishName() 0 5 1
A testCreateFromRemote() 0 22 1
A testRemote() 0 12 1
A testFetch() 0 21 1
A testPull() 0 15 1
A testPush() 0 24 1
A testRevParse() 0 11 1
A testIsBare() 0 14 1
A testGlobalConfigs() 0 21 3
A testResetHard() 0 18 1
A testResetSoft() 0 18 1
A testGlobalOptions() 0 21 3
A testGlobalCommandArguments() 0 21 3
A testStashThrowsExceptionIfNoCommits() 0 8 1
A testStash() 0 11 1
A testStashList() 0 9 1
A testStashShow() 0 9 1
A testStashDrop() 0 10 1
A testStashPop() 0 11 1
A testStashApply() 0 11 1
A testStashBranch() 0 10 1
A testStashCreate() 0 8 1
A testStashClear() 0 12 1

How to fix   Complexity   

Complex Class

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
2
3
/**
4
 * This file is part of the GitElephant package.
5
 *
6
 * (c) Matteo Giachino <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Just for fun...
12
 */
13
14
namespace GitElephant;
15
16
use GitElephant\Command\ResetCommand;
17
use GitElephant\Objects\Branch;
18
use GitElephant\Objects\Log;
19
use GitElephant\Objects\NodeObject;
20
use GitElephant\Objects\Tag;
21
22
/**
23
 * RepositoryTest
24
 *
25
 * Repository Test Class
26
 *
27
 * @author Matteo Giachino <[email protected]>
28
 */
29
class RepositoryTest extends TestCase
30
{
31
    /**
32
     * setUp
33
     */
34
    public function setUp(): void
35
    {
36
        $this->initRepository();
37
    }
38
39
    /**
40
     * @covers \GitElephant\Repository::__construct
41
     * @covers \GitElephant\Repository::getPath
42
     */
43
    public function testConstruct(): void
44
    {
45
        $this->assertEquals($this->getRepository()->getPath(), $this->path);
46
47
        $this->expectException('GitElephant\Exception\InvalidRepositoryPathException');
48
        $repo = new Repository('non-existent-path');
0 ignored issues
show
Unused Code introduced by
$repo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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(): void
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(): void
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(): void
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(): void
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(): void
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
        // Commit something with a custom date.
148
        $this->addFile('test3');
149
        $this->getRepository()->commit('commit 3', true, 'develop', null, false, new \DateTimeImmutable('1981-09-24'));
150
        $log = $this->getRepository()->getLog('develop', null, 1)->current();
151
        $this->assertEquals('1981-09-24', $log->getDatetimeAuthor()->format('Y-m-d'));
152
    }
153
154
    /**
155
     * @covers \GitElephant\Repository::getStatusOutput
156
     */
157
    public function testGetStatus(): void
158
    {
159
        $this->getRepository()->init();
160
        $this->addFile('test');
161
        $this->getRepository()->commit('test commit', true);
162
        $output = $this->getRepository()->getStatusOutput();
163
        $this->assertStringEndsWith('master', $output[0]);
164
        $this->addFile('file2');
165
        $output = $this->getRepository()->getStatusOutput();
166
        $this->assertContains('file2', $output);
167
    }
168
169
    /**
170
     * @covers \GitElephant\Repository::createBranch
171
     */
172
    public function testCreateBranch(): void
173
    {
174
        $this->getRepository()->init();
175
        $this->addFile('test');
176
        $this->getRepository()->commit('foo', true);
177
        $this->getRepository()->createBranch('test-branch');
178
        $this->assertEquals(2, count($this->getRepository()->getBranches()));
179
    }
180
181
    /**
182
     * @covers \GitElephant\Repository::deleteBranch
183
     */
184
    public function testDeleteBranch(): void
185
    {
186
        $this->getRepository()->init();
187
        $this->addFile('test-file');
188
        $this->getRepository()->commit('test', true);
189
        $this->getRepository()->createBranch('branch2');
190
        $this->assertEquals(2, count($this->getRepository()->getBranches(true)));
191
        $this->getRepository()->deleteBranch('branch2');
192
        $this->assertEquals(1, count($this->getRepository()->getBranches(true)));
193
        $this->addFile('test-file2');
194
        $this->getRepository()->commit('test2', true);
195
        $this->getRepository()->createBranch('branch3');
196
        $this->assertEquals(2, count($this->getRepository()->getBranches(true)));
197
        $this->getRepository()->deleteBranch('branch3', true);
198
        $this->assertEquals(1, count($this->getRepository()->getBranches(true)));
199
    }
200
201
    /**
202
     * @covers \GitElephant\Repository::getBranches
203
     */
204
    public function testGetBranches(): void
205
    {
206
        $this->getRepository()->init();
207
        $this->addFile('test');
208
        $this->getRepository()->stage();
209
        $this->getRepository()->commit('initial import', true);
210
        $this->assertCount(
211
            1,
212
            $this->getRepository()->getBranches(),
213
            'an initialized repository should have only one branch'
214
        );
215
        $this->getRepository()->createBranch('test-branch');
216
        $this->assertCount(2, $this->getRepository()->getBranches(), 'two branches expected');
217
        $branches = $this->getRepository()->getBranches();
218
        /** @var Branch $branch */
219
        $branch = $branches[0];
220
        $this->assertEquals('master', $branch->getName());
221
        $this->getRepository()->deleteBranch('test-branch');
222
        $this->assertCount(1, $this->getRepository()->getBranches(), 'one branch expected');
223
        $this->assertInstanceOf(
224
            'GitElephant\Objects\Branch',
225
            $this->getRepository()->getMainBranch(),
226
            'main branch should be an instance of Branch'
227
        );
228
        $this->assertTrue(
229
            $this->getRepository()->getMainBranch()->getCurrent(),
230
            'getCurrent on main branch should be true'
231
        );
232
        $this->assertEquals(
233
            'master',
234
            $this->getRepository()->getMainBranch()->getName(),
235
            'main branch should be named "master"'
236
        );
237
        $this->assertEquals(['master'], $this->getRepository()->getBranches(true));
238
        $this->getRepository()->createBranch('develop');
239
        $this->assertContains('master', $this->getRepository()->getBranches(true));
240
        $this->assertContains('develop', $this->getRepository()->getBranches(true));
241
    }
242
243
    /**
244
     * @covers \GitElephant\Repository::getMainBranch
245
     */
246
    public function testGetMainBranch(): void
247
    {
248
        $this->getRepository()->init();
249
        $this->addFile('test-file');
250
        $this->getRepository()->commit('test', true);
251
        $this->assertEquals('master', $this->getRepository()->getMainBranch()->getName());
252
    }
253
254
    /**
255
     * @covers \GitElephant\Repository::getBranch
256
     */
257
    public function testGetBranch(): void
258
    {
259
        $this->getRepository()->init();
260
        $this->addFile('test-file');
261
        $this->getRepository()->commit('test', true);
262
        $this->assertInstanceOf('GitElephant\Objects\Branch', $this->getRepository()->getBranch('master'));
263
        $this->assertNull($this->getRepository()->getBranch('a-branch-that-do-not-exists'));
264
    }
265
266
    /**
267
     * @covers \GitElephant\Repository::merge
268
     */
269
    public function testMerge(): void
270
    {
271
        $this->getRepository()->init();
272
        $this->addFile('test-file');
273
        $this->getRepository()->commit('test', true);
274
        $this->assertEquals(1, count($this->getRepository()->getTree()));
275
        $this->getRepository()->createBranch('branch2');
276
        $this->getRepository()->checkout('branch2');
277
        $this->addFile('file2');
278
        $this->getRepository()->commit('test2', true);
279
        $this->assertEquals(2, count($this->getRepository()->getTree()));
280
        $this->getRepository()->checkout('master');
281
        $this->assertEquals(1, count($this->getRepository()->getTree()));
282
        $this->getRepository()->merge($this->getRepository()->getBranch('branch2'));
0 ignored issues
show
Bug introduced by
It seems like $this->getRepository()->getBranch('branch2') can be null; however, merge() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
283
        $this->assertEquals(2, count($this->getRepository()->getTree()));
284
285
        // attempt to merge a different branch by forcing a 3-way merge and verify the merge commit message
286
        $this->getRepository()->createBranch('branch3');
287
        $this->getRepository()->checkout('branch3');
288
        $this->addFile('file3');
289
        $this->getRepository()->commit('test3', true);
290
        $this->assertEquals(3, count($this->getRepository()->getTree()));
291
        $this->getRepository()->checkout('master');
292
        $this->assertEquals(2, count($this->getRepository()->getTree()));
293
        $this->getRepository()->merge($this->getRepository()->getBranch('branch3'), 'test msg', 'no-ff');
0 ignored issues
show
Bug introduced by
It seems like $this->getRepository()->getBranch('branch3') can be null; however, merge() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
294
        $this->assertEquals(3, count($this->getRepository()->getTree()));
295
        $this->assertEquals('test msg', $this->getRepository()->getCommit()->getMessage()->getFullMessage());
296
297
        // attempt a fast forward merge where a 3-way is necessary and trap the resulting exception
298
        $this->getRepository()->checkout('branch2');
299
        $this->addFile('file4');
300
        $this->getRepository()->commit('test4', true);
301
        $this->assertEquals(3, count($this->getRepository()->getTree()));
302
        $this->getRepository()->checkout('master');
303
        $this->assertEquals(3, count($this->getRepository()->getTree()));
304
        try {
305
            $this->getRepository()->merge($this->getRepository()->getBranch('branch2'), '', 'ff-only');
0 ignored issues
show
Bug introduced by
It seems like $this->getRepository()->getBranch('branch2') can be null; however, merge() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
306
        } catch (\RuntimeException $e) {
307
            return;
308
        }
309
        $this->fail("Merge should have produced a runtime exception.");
310
    }
311
312
    /**
313
     * @covers \GitElephant\Repository::getTags
314
     * @covers \GitElephant\Repository::getTag
315
     * @covers \GitElephant\Repository::createTag
316
     * @covers \GitElephant\Repository::deleteTag
317
     */
318
    public function testTags(): void
319
    {
320
        $this->getRepository()->init();
321
        $this->addFile('test-file');
322
        $this->getRepository()->commit('test', true);
323
        $this->assertEquals(0, count($this->getRepository()->getTags()));
324
        $this->getRepository()->createTag('test-tag');
325
        $this->assertEquals(1, count($this->getRepository()->getTags()));
326
        $this->assertInstanceOf('GitElephant\Objects\Tag', $this->getRepository()->getTag('test-tag'));
327
        $this->getRepository()->deleteTag('test-tag');
328
        $this->assertEquals(0, count($this->getRepository()->getTags()));
329
        $this->assertNull($this->getRepository()->getTag('a-tag-that-do-not-exists'));
330
    }
331
332
    /**
333
     * test getLastTag
334
     */
335
    public function testGetLastTag(): void
336
    {
337
        $this->getRepository()->init();
338
        $this->addFile('test-file');
339
        $this->getRepository()->commit('test', true);
340
        $this->getRepository()->createTag('0.0.2');
341
        sleep(1);
342
        $this->getRepository()->createTag('0.0.4');
343
        sleep(1);
344
        $this->getRepository()->createTag('0.0.3');
345
        sleep(1);
346
        $this->getRepository()->createTag('0.0.1');
347
        sleep(1);
348
        $this->assertEquals(Tag::pick($this->getRepository(), '0.0.1'), $this->getRepository()->getLastTag());
349
350
        $this->getRepository()->createTag('0.0.05');
351
        $this->assertEquals(Tag::pick($this->getRepository(), '0.0.05'), $this->getRepository()->getLastTag());
352
353
        $this->getRepository()->deleteTag(Tag::pick($this->getRepository(), '0.0.05'));
354
        $this->assertEquals(Tag::pick($this->getRepository(), '0.0.1'), $this->getRepository()->getLastTag());
355
    }
356
357
    /**
358
     * @covers \GitElephant\Repository::getCommit
359
     */
360
    public function testGetCommit(): void
361
    {
362
        $this->getRepository()->init();
363
        $this->addFile('test-file');
364
        $this->getRepository()->commit('test', true);
365
        $this->assertInstanceOf('GitElephant\Objects\Commit', $this->getRepository()->getCommit());
366
    }
367
368
    public function testGetBranchOrTag(): void
369
    {
370
        $this->getRepository()->init();
371
        $this->addFile('test-file');
372
        $this->getRepository()->commit('test', true);
373
        $this->getRepository()->createBranch('branch2');
374
        $this->getRepository()->createTag('tag1');
375
        $this->assertInstanceOf('\GitElephant\Objects\Branch', $this->getRepository()->getBranchOrTag('branch2'));
376
        $this->assertInstanceOf('\GitElephant\Objects\Tag', $this->getRepository()->getBranchOrTag('tag1'));
377
        $this->assertNull($this->getRepository()->getBranchOrTag('not-exists'));
378
    }
379
380
    /**
381
     * @covers \GitElephant\Repository::getObjectLog
382
     */
383
    public function testGetObjectLog(): void
384
    {
385
        $repo = $this->getRepository();
386
        $repo->init();
387
388
        $this->addFolder('test');
389
390
        $this->addFile('A.txt', 'test');
391
        $repo->commit('added A.txt', true);
392
393
        $this->addFile('B.txt', 'test');
394
        $repo->commit('added B.txt', true);
395
396
        $this->addFile('C.txt', 'test');
397
        $repo->commit('added C.txt', true);
398
399
        $this->addFile('D.txt', 'test');
400
        $repo->commit('added D.txt', true);
401
402
        $this->addFile('E.txt', 'test');
403
        $repo->commit('added E.txt', true);
404
405
        $tree = $repo->getTree();
406
        /** @var NodeObject */
407
        $obj = $tree[0];
408
409
        $log = $this->getRepository()->getObjectLog($obj);
410
        $this->assertInstanceOf(Log::class, $log);
411
        $this->assertEquals(1, $log->count());
412
413
        $log = $this->getRepository()->getObjectLog($obj, null, 10);
414
        $this->assertEquals(5, $log->count());
415
416
        $this->assertEquals('added E.txt', $log->first()->getMessage()->toString());
417
        $this->assertEquals('added A.txt', $log->last()->getMessage()->toString());
418
    }
419
420
    /**
421
     * Test logs on different tree objects
422
     *
423
     * @covers \GitElephant\Repository::getObjectLog
424
     */
425
    public function testGetObjectLogFolders(): void
426
    {
427
        $repo = $this->getRepository();
428
        $repo->init();
429
430
        $this->addFolder('A');
431
        $this->addFile('A1.txt', 'A');
432
        $repo->commit('A/A1', true);
433
434
        $this->addFile('A2.txt', 'A');
435
        $repo->commit('A/A2', true);
436
437
        $this->addFolder('B');
438
        $this->addFile('B1.txt', 'B');
439
        $repo->commit('B/B1', true);
440
441
        $this->addFile('B2.txt', 'B');
442
        $repo->commit('B/B2', true);
443
444
        $tree = $repo->getTree();
445
446
        /** @var NodeObject $treeObj */
447
        foreach ($tree as $treeObj) {
448
            $name = $treeObj->getName();
449
            $log = $repo->getObjectLog($treeObj, null, 10);
0 ignored issues
show
Bug introduced by
It seems like $treeObj defined by $treeObj on line 447 can be null; however, GitElephant\Repository::getObjectLog() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
450
451
            $this->assertEquals(2, $log->count());
452
453
            $i = 2;
454
            foreach ($log as $commit) {
455
                $this->assertEquals($name . '/' . $name . $i, $commit->getMessage()->toString());
456
                --$i;
457
            }
458
        }
459
    }
460
461
    /**
462
     * Test logs on different branches
463
     *
464
     * @covers \GitElephant\Repository::getObjectLog
465
     */
466
    public function testGetObjectLogBranches(): void
467
    {
468
        $repo = $this->getRepository();
469
        $repo->init();
470
471
        $this->addFolder('A');
472
        $this->addFile('A1.txt', 'A');
473
        $repo->commit('A/A1', true);
474
475
        $this->addFile('A2.txt', 'A');
476
        $repo->commit('A/A2', true);
477
478
        $repo->createBranch('test-branch');
479
        $repo->checkout('test-branch');
480
481
        $this->addFile('A3.txt', 'A');
482
        $repo->commit('A/A3', true);
483
484
        // master branch
485
        $repo->checkout('master');
486
        $tree = $repo->getTree();
487
        $dir = $tree[0];
488
        $log = $repo->getObjectLog($dir, null, 10);
489
490
        $this->assertEquals(2, $log->count());
491
        $this->assertEquals('A/A2', $log->first()->getMessage()->toString());
492
493
        // test branch
494
        $repo->checkout('test-branch');
495
        $tree = $repo->getTree();
496
        $dir = $tree[0];
497
        $log = $repo->getObjectLog($dir, null, 10);
498
499
        $this->assertEquals(3, $log->count());
500
        $this->assertEquals('A/A3', $log->first()->getMessage()->toString());
501
    }
502
503
    /**
504
     * @covers \GitElephant\Repository::getLog
505
     */
506
    public function testGetLog(): void
507
    {
508
        $this->getRepository()->init();
509
510
        for ($i = 0; $i < 50; $i++) {
511
            $this->addFile('test file ' . $i);
512
            $this->getRepository()->commit('test commit ' . $i, true);
513
        }
514
515
        $log = $this->getRepository()->getLog();
516
        $this->assertInstanceOf('GitElephant\Objects\Log', $this->getRepository()->getLog());
517
        $this->assertGreaterThan(0, $log->count());
518
    }
519
520
    /**
521
     * @covers \GitElephant\Repository::getLog
522
     */
523
    public function testGetLogForBranch(): void
524
    {
525
        $this->getRepository()->init();
526
        $this->addFile('test file 0');
527
        $this->getRepository()->commit('first commit', true);
528
        $this->getRepository()->checkout('test-branch', true);
529
530
        for ($i = 1; $i <= 2; $i++) {
531
            $this->addFile('test file ' . $i);
532
            $this->getRepository()->commit('test commit ' . $i, true);
533
        }
534
535
        $log = $this->getRepository()->getLog(['test-branch', '^master']);
536
        $this->assertInstanceOf('GitElephant\Objects\Log', $this->getRepository()->getLog());
537
        $this->assertEquals(2, $log->count());
538
    }
539
540
    /**
541
     * @covers \GitElephant\Repository::checkout
542
     */
543
    public function testCheckout(): void
544
    {
545
        $this->getRepository()->init();
546
        $this->addFile('test-file');
547
        $this->getRepository()->commit('test', true);
548
        $this->assertEquals('master', $this->getRepository()->getMainBranch()->getName());
549
        $this->getRepository()->createBranch('branch2');
550
        $this->getRepository()->checkout('branch2');
551
        $this->assertEquals('branch2', $this->getRepository()->getMainBranch()->getName());
552
    }
553
554
    /**
555
     * @covers \GitElephant\Repository::checkout
556
     */
557
    public function testCheckoutTag(): void
558
    {
559
        $this->getRepository()->init();
560
        $this->addFile('test-file');
561
        $this->getRepository()->commit('test', true);
562
        $this->getRepository()->createTag('v0.0.1');
563
        $this->addFile('test-file2');
564
        $this->getRepository()->commit('test2', true);
565
        $tag = $this->getRepository()->getTag('v0.0.1');
566
        $this->assertInstanceOf('GitElephant\Objects\Tag', $tag);
567
        $lastCommit = $this->getRepository()->getCommit();
568
        $this->assertStringNotContainsString('detached', implode(' ', $this->getRepository()->getStatusOutput()));
569
        $this->getRepository()->checkout($tag);
0 ignored issues
show
Bug introduced by
It seems like $tag defined by $this->getRepository()->getTag('v0.0.1') on line 565 can be null; however, GitElephant\Repository::checkout() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
570
        $newCommit = $this->getRepository()->getCommit();
571
        $this->assertNotEquals($newCommit->getSha(), $lastCommit->getSha());
572
        $this->assertStringContainsString('detached', implode(' ', $this->getRepository()->getStatusOutput()));
573
    }
574
575
    /**
576
     * @covers \GitElephant\Repository::getTree
577
     * @covers \GitElephant\Objects\Tree
578
     */
579
    public function testGetTree(): void
580
    {
581
        $this->getRepository()->init();
582
        $this->addFile('test');
583
        $this->addFolder('test-folder');
584
        $this->addFile('test2', 'test-folder');
585
586
        $this->getRepository()->stage();
587
        $this->getRepository()->commit('initial import');
588
589
        $tree = $this->getRepository()->getTree();
590
        $this->assertFalse($tree->isBlob());
591
        $this->assertTrue($this->getRepository()->getTree($this->getRepository()->getCommit(), 'test')->isBlob());
592
        $this->assertCount(2, $tree, 'One file in the repository');
593
        $firstNode = $tree[0];
594
        $this->assertInstanceOf(
595
            'GitElephant\Objects\NodeObject',
596
            $firstNode,
597
            'array access on tree should give always a node type'
598
        );
599
        $this->assertEquals(
600
            'test-folder',
601
            $firstNode->getName(),
602
            'First repository file should be named "test"'
603
        );
604
        $secondNode = $tree[1];
605
        $this->assertInstanceOf(
606
            'GitElephant\Objects\NodeObject',
607
            $secondNode,
608
            'array access on tree should give always a node type'
609
        );
610
        $this->assertEquals(
611
            NodeObject::TYPE_BLOB,
612
            $secondNode->getType(),
613
            'second node should be of type tree'
614
        );
615
        $subtree = $this->getRepository()->getTree('master', 'test-folder');
616
        $subnode = $subtree[0];
617
        $this->assertInstanceOf(
618
            'GitElephant\Objects\NodeObject',
619
            $subnode,
620
            'array access on tree should give always a node type'
621
        );
622
        $this->assertEquals(
623
            NodeObject::TYPE_BLOB,
624
            $subnode->getType(),
625
            'subnode should be of type blob'
626
        );
627
        $this->assertEquals(
628
            'test2',
629
            $subnode->getName(),
630
            'subnode should be named "test2"'
631
        );
632
    }
633
634
    /**
635
     * @covers \GitElephant\Repository::getDiff
636
     */
637
    public function testGetDiff(): void
638
    {
639
        $this->getRepository()->init();
640
        $this->addFile('test-file');
641
        $this->getRepository()->commit('commit 1', true);
642
        $commit1 = $this->getRepository()->getCommit();
643
        $this->assertInstanceOf('GitElephant\Objects\Diff\Diff', $this->getRepository()->getDiff($commit1));
644
        $this->addFile('test-file2');
645
        $this->getRepository()->commit('commit 2', true);
646
        $commit2 = $this->getRepository()->getCommit();
647
        $this->assertInstanceOf('GitElephant\Objects\Diff\Diff', $this->getRepository()->getDiff($commit2));
648
        $this->assertInstanceOf('GitElephant\Objects\Diff\Diff', $this->getRepository()->getDiff($commit2, $commit1));
649
        $shaHead = $this->getRepository()->getCommit();
650
        $this->assertInstanceOf('GitElephant\Objects\Diff\Diff', $diff = $this->getRepository()->getDiff($shaHead));
651
    }
652
653
    /**
654
     * testCloneFrom
655
     */
656
    public function testCloneFrom(): void
657
    {
658
        $this->initRepository(null, 0);
659
        $this->initRepository(null, 1);
660
        $remote = $this->getRepository(0);
661
        $remote->init();
662
        $this->addFile('test', null, null, $remote);
663
        $remote->commit('test', true);
664
        $local = $this->getRepository(1);
665
        $local->cloneFrom($remote->getPath(), '.', 'master', 1, false);
666
        $commit = $local->getCommit();
667
        $this->assertEquals($remote->getCommit()->getSha(), $commit->getSha());
668
        $this->assertEquals($remote->getCommit()->getMessage(), $commit->getMessage());
669
    }
670
671
    /**
672
     * testOutputContent
673
     */
674
    public function testOutputContent(): void
675
    {
676
        $this->initRepository();
677
        $this->getRepository()->init();
678
        $this->addFile('file1', null, 'file content');
679
        $this->getRepository()->commit('first commit', true);
680
        $branch = $this->getRepository()->getBranch('master');
681
        $tree = $this->getRepository()->getTree($branch, 'file1');
0 ignored issues
show
Bug introduced by
It seems like $branch defined by $this->getRepository()->getBranch('master') on line 680 can be null; however, GitElephant\Repository::getTree() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
682
        $treeObject = $tree->getBlob();
683
        $this->assertEquals(['file content'], $this->getRepository()->outputContent($treeObject, $branch));
0 ignored issues
show
Bug introduced by
It seems like $treeObject defined by $tree->getBlob() on line 682 can be null; however, GitElephant\Repository::outputContent() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Bug introduced by
It seems like $branch defined by $this->getRepository()->getBranch('master') on line 680 can be null; however, GitElephant\Repository::outputContent() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
684
    }
685
686
    /**
687
     * testMove
688
     */
689
    public function testMove(): void
690
    {
691
        $this->getRepository()->init();
692
        $this->addFile('foo');
693
        $this->getRepository()->commit('commit 1', true);
694
        $this->getRepository()->move('foo', 'bar');
695
        $status = $this->getRepository()->getStatusOutput();
696
        $this->myAssertMatchesRegularExpression('/(.*):    foo -> bar/', implode("\n", $status));
697
    }
698
699
    /**
700
     * testRemove
701
     */
702
    public function testRemove(): void
703
    {
704
        $this->getRepository()->init();
705
        $this->addFile('foo');
706
        $this->getRepository()->commit('commit 1', true);
707
        $this->getRepository()->remove('foo');
708
        $status = $this->getRepository()->getStatusOutput();
709
710
        $this->myAssertMatchesRegularExpression('/(.*):    foo/', implode("\n", $status));
711
    }
712
713
    /**
714
     * testCountCommits
715
     */
716
    public function testCountCommits(): void
717
    {
718
        $this->getRepository()->init();
719
        $this->addFile('foo');
720
        $this->getRepository()->commit('commit 1', true);
721
        $this->assertEquals(1, $this->getRepository()->countCommits());
722
        $this->addFile('foo2');
723
        $this->getRepository()->commit('commit 2', true);
724
        $this->assertEquals(2, $this->getRepository()->countCommits());
725
        $this->getRepository()->createBranch('new-branch');
726
        $this->getRepository()->checkout('new-branch');
727
        $this->assertEquals(2, $this->getRepository()->countCommits());
728
        $this->addFile('bar');
729
        $this->getRepository()->commit('commit 3', true);
730
        $this->assertEquals(3, $this->getRepository()->countCommits());
731
        $this->getRepository()->checkout('master');
732
        $this->assertEquals(2, $this->getRepository()->countCommits());
733
    }
734
735
    /**
736
     * testHumanishName
737
     */
738
    public function testHumanishName(): void
739
    {
740
        $this->initRepository('test-dir');
741
        $this->assertEquals('test-dir', $this->getRepository()->getHumanishName());
742
    }
743
744
    /**
745
     * testCreateFromRemote
746
     */
747
    public function testCreateFromRemote(): void
748
    {
749
        $this->initRepository(null, 0);
750
        $remote = $this->getRepository(0);
751
        $remote->init();
752
        $this->addFile('test', null, null, $remote);
753
        $remote->commit('test', true);
754
        $remote->createBranch('develop');
755
756
        $repo = Repository::createFromRemote($remote->getPath());
757
        $this->assertInstanceOf('GitElephant\Repository', $repo);
758
        $this->assertGreaterThanOrEqual(2, $repo->getBranches());
759
        $branches = $repo->getBranches();
760
        $branchesName = array_map(
761
            function (Branch $b) {
762
                return $b->getName();
763
            },
764
            $branches
765
        );
766
        $this->assertContains('master', $branchesName);
767
        $this->assertContains('develop', $branchesName);
768
    }
769
770
    /**
771
     * testAddRemote
772
     */
773
    public function testRemote(): void
774
    {
775
        $this->initRepository(null, 0);
776
        $remote = $this->getRepository(0);
777
        $remote->init(true);
778
        $this->initRepository();
779
        $this->repository->init();
780
        $this->repository->addRemote('github', $remote->getPath());
781
        $this->assertInstanceOf('GitElephant\Objects\Remote', $this->repository->getRemote('github'));
782
        $this->repository->addRemote('github2', $remote->getPath());
783
        $this->assertCount(2, $this->repository->getRemotes());
784
    }
785
786
    /**
787
     * testFetch, git branch -a should find the branch
788
     */
789
    public function testFetch(): void
790
    {
791
        $this->initRepository(null, 0);
792
        $this->initRepository(null, 1);
793
        $r1 = $this->getRepository(0);
794
        $r1->init();
795
        $this->addFile('test1', null, null, $r1);
796
        $r1->commit('test commit', true);
797
        $r1->createBranch('tag-test');
798
        $this->addFile('test2', null, null, $r1);
799
        $r1->commit('another test commit', true);
800
        $r1->createTag('test-tag');
801
        $r2 = $this->getRepository(1);
802
        $r2->init();
803
        $r2->addRemote('origin', $r1->getPath());
804
        $this->assertEmpty($r2->getBranches(true, true));
805
        $r2->fetch();
806
        $this->assertNotEmpty($r2->getBranches(true, true));
807
        $r2->fetch(null, null, true);
808
        $this->assertNotNull($r2->getTag('test-tag'));
809
    }
810
811
    /**
812
     * test pull
813
     */
814
    public function testPull(): void
815
    {
816
        $this->initRepository(null, 0);
817
        $this->initRepository(null, 1);
818
        $r1 = $this->getRepository(0);
819
        $r1->init();
820
        $this->addFile('test1', null, null, $r1);
821
        $r1->commit('test commit', true);
822
        $r2 = $this->getRepository(1);
823
        $r2->init();
824
        $r2->addRemote('origin', $r1->getPath());
825
        $r2->pull('origin', 'master');
826
        $this->assertEquals('test commit', $r2->getLog()->last()->getMessage());
827
        $this->assertEquals($r1->getMainBranch()->getSha(), $r2->getLog()->last()->getSha());
828
    }
829
830
    /**
831
     * test pull
832
     */
833
    public function testPush(): void
834
    {
835
        $this->initRepository(null, 0);
836
        $this->initRepository(null, 1);
837
        $this->initRepository(null, 2);
838
        // commit on r1
839
        $r1 = $this->getRepository(0);
840
        $r1->init();
841
        $this->addFile('test1', null, null, $r1);
842
        $r1->commit('test commit', true);
843
        // push from r1 to r2
844
        $r2 = $this->getRepository(1);
845
        $r2->init(true);
846
        $r1->addRemote('origin', $r2->getPath());
847
        $r1->push('origin', 'master');
848
        // pull from r2 to r3 should get the same result
849
        $r3 = $this->getRepository(2);
850
        $r3->init();
851
        $r3->addRemote('origin', $r2->getPath());
852
        $r3->pull('origin', 'master');
853
854
        $this->assertEquals('test commit', $r3->getLog()->last()->getMessage());
855
        $this->assertEquals($r1->getMainBranch()->getSha(), $r3->getLog()->last()->getSha());
856
    }
857
858
    public function testRevParse(): void
859
    {
860
        $this->initRepository(null, 0);
861
        $r = $this->getRepository(0);
862
        $r->init();
863
        $this->addFile('test1', null, null, $r);
864
        $r->commit('test commit', true);
865
        $master = $r->getBranch('master');
866
        $revParse = $r->revParse($master, []);
867
        $this->assertEquals($master->getSha(), $revParse[0]);
868
    }
869
870
    public function testIsBare(): void
871
    {
872
        $this->initRepository(null, 0);
873
        $r = $this->getRepository(0);
874
        $r->init();
875
876
        $this->assertEquals(false, $r->isBare());
877
878
        $this->initRepository(null, 1);
879
        $r = $this->getRepository(1);
880
        $r->init(true);
881
882
        $this->assertEquals(true, $r->isBare());
883
    }
884
885
    /**
886
     * test add, remove and get global configs
887
     *
888
     * @covers \GitElephant\Repository::addGlobalConfig
889
     * @covers \GitElephant\Repository::getGlobalConfigs
890
     * @covers \GitElephant\Repository::removeGlobalConfig
891
     */
892
    public function testGlobalConfigs(): void
893
    {
894
        $repo = $this->getRepository();
895
896
        $configs = [
897
            'test1' => true,
898
            'test2' => 1,
899
            'test3' => 'value',
900
        ];
901
        $this->assertEmpty($repo->getGlobalConfigs());
902
903
        foreach ($configs as $configName => $configValue) {
904
            $repo->addGlobalConfig($configName, $configValue);
905
        }
906
        $this->assertSame($configs, $repo->getGlobalConfigs());
907
908
        foreach (array_keys($configs) as $configName) {
909
            $repo->removeGlobalConfig($configName);
910
        }
911
        $this->assertEmpty($repo->getGlobalConfigs());
912
    }
913
914
    /**
915
     * test reset
916
     */
917
    public function testResetHard(): void
918
    {
919
        $this->initRepository();
920
        $repo = $this->getRepository();
921
        $repo->init();
922
        $this->addFile('file1');
923
        $repo->stage();
924
        $repo->commit('message1');
925
        $headCommit = $repo->getCommit();
926
        $this->addFile('file2');
927
        $repo->stage();
928
        $repo->commit('message2');
929
930
        $this->assertEquals(2, $repo->countCommits());
931
        $repo->reset($headCommit, [ResetCommand::OPTION_HARD]);
932
        $this->assertEquals(1, $repo->countCommits());
933
        $this->assertEmpty($repo->getIndexStatus()->added());
934
    }
935
936
    /**
937
     * test reset
938
     */
939
    public function testResetSoft(): void
940
    {
941
        $this->initRepository();
942
        $repo = $this->getRepository();
943
        $repo->init();
944
        $this->addFile('file1');
945
        $repo->stage();
946
        $repo->commit('message1');
947
        $headCommit = $repo->getCommit();
948
        $this->addFile('file2');
949
        $repo->stage();
950
        $repo->commit('message2');
951
952
        $this->assertEquals(2, $repo->countCommits());
953
        $repo->reset($headCommit, [ResetCommand::OPTION_SOFT]);
954
        $this->assertEquals(1, $repo->countCommits());
955
        $this->assertNotEmpty($repo->getIndexStatus()->added());
956
    }
957
958
    /**
959
     * test add, remove and get global options
960
     *
961
     * @covers \GitElephant\Repository::addGlobalOption
962
     * @covers \GitElephant\Repository::getGlobalOptions
963
     * @covers \GitElephant\Repository::removeGlobalOption
964
     */
965
    public function testGlobalOptions(): void
966
    {
967
        $repo = $this->getRepository();
968
969
        $options = [
970
            'test1' => true,
971
            'test2' => 1,
972
            'test3' => 'value',
973
        ];
974
        $this->assertEmpty($repo->getGlobalOptions());
975
976
        foreach ($options as $configName => $configValue) {
977
            $repo->addGlobalOption($configName, $configValue);
978
        }
979
        $this->assertSame($options, $repo->getGlobalOptions());
980
981
        foreach (array_keys($options) as $configName) {
982
            $repo->removeGlobalOption($configName);
983
        }
984
        $this->assertEmpty($repo->getGlobalOptions());
985
    }
986
987
    /**
988
     * test add, remove and get global command arguments
989
     *
990
     * @covers \GitElephant\Repository::addGlobalCommandArgument
991
     * @covers \GitElephant\Repository::getGlobalCommandArguments
992
     * @covers \GitElephant\Repository::removeGlobalCommandArgument
993
     */
994
    public function testGlobalCommandArguments(): void
995
    {
996
        $repo = $this->getRepository();
997
998
        $args = [
999
            true,
1000
            1,
1001
            'value',
1002
        ];
1003
        $this->assertEmpty($repo->getGlobalCommandArguments());
1004
1005
        foreach ($args as $configValue) {
1006
            $repo->addGlobalCommandArgument($configValue);
1007
        }
1008
        $this->assertSame($args, $repo->getGlobalCommandArguments());
1009
1010
        foreach ($args as $configValue) {
1011
            $repo->removeGlobalCommandArgument($configValue);
1012
        }
1013
        $this->assertEmpty($repo->getGlobalCommandArguments());
1014
    }
1015
1016
    /**
1017
     * @covers \GitElephant\Repository::stash
1018
     */
1019
    public function testStashThrowsExceptionIfNoCommits(): void
1020
    {
1021
        $this->getRepository()->init();
1022
        $this->addFile('test');
1023
1024
        $this->expectException('RuntimeException');
1025
        $this->getRepository()->stash('My stash', true);
1026
    }
1027
1028
    /**
1029
     * @covers \GitElephant\Repository::stash
1030
     */
1031
    public function testStash(): void
1032
    {
1033
        $this->getRepository()->init();
1034
        $this->addFile('test');
1035
        $this->getRepository()->commit('Test commit', true);
1036
        $this->addFile('test2');
1037
        $this->getRepository()->stash('My stash', true);
1038
        $this->assertTrue($this->getRepository()->isClean());
1039
        $stashList = $this->getRepository()->stashList();
1040
        $this->assertEquals(1, preg_match('%My stash%', $stashList[0]));
1041
    }
1042
1043
    /**
1044
     * @covers \GitElephant\Repository::stashList
1045
     */
1046
    public function testStashList(): void
1047
    {
1048
        $this->getRepository()->init();
1049
        $this->addFile('test');
1050
        $this->getRepository()->commit('Test commit', true);
1051
        $this->addFile('test2');
1052
        $this->getRepository()->stash('My stash', true);
1053
        $this->assertCount(1, $this->getRepository()->stashList());
1054
    }
1055
1056
    /**
1057
     * @covers \GitElephant\Repository::stashShow
1058
     */
1059
    public function testStashShow(): void
1060
    {
1061
        $this->getRepository()->init();
1062
        $this->addFile('test');
1063
        $this->getRepository()->commit('Test commit', true);
1064
        $this->addFile('test2');
1065
        $this->getRepository()->stash('My stash', true);
1066
        $this->assertIsString($this->getRepository()->stashShow(0));
1067
    }
1068
1069
    /**
1070
     * @covers \GitElephant\Repository::stashDrop
1071
     */
1072
    public function testStashDrop(): void
1073
    {
1074
        $this->getRepository()->init();
1075
        $this->addFile('test');
1076
        $this->getRepository()->commit('Test commit', true);
1077
        $this->addFile('test2');
1078
        $this->getRepository()->stash('My stash', true);
1079
        $this->getRepository()->stashDrop(0);
1080
        $this->assertCount(0, $this->getRepository()->stashList());
1081
    }
1082
1083
    /**
1084
     * @covers \GitElephant\Repository::stashPop
1085
     */
1086
    public function testStashPop(): void
1087
    {
1088
        $this->getRepository()->init();
1089
        $this->addFile('test');
1090
        $this->getRepository()->commit('Test commit', true);
1091
        $this->addFile('test2');
1092
        $this->getRepository()->stash('My stash', true);
1093
        $this->getRepository()->stashPop(0);
1094
        $this->assertTrue($this->getRepository()->isDirty());
1095
        $this->assertCount(0, $this->getRepository()->stashList());
1096
    }
1097
1098
    /**
1099
     * @covers \GitElephant\Repository::stashApply
1100
     */
1101
    public function testStashApply(): void
1102
    {
1103
        $this->getRepository()->init();
1104
        $this->addFile('test');
1105
        $this->getRepository()->commit('Test commit', true);
1106
        $this->addFile('test2');
1107
        $this->getRepository()->stash('My stash', true);
1108
        $this->getRepository()->stashApply(0);
1109
        $this->assertTrue($this->getRepository()->isDirty());
1110
        $this->assertCount(1, $this->getRepository()->stashList());
1111
    }
1112
1113
    /**
1114
     * @covers \GitElephant\Repository::stashBranch
1115
     */
1116
    public function testStashBranch(): void
1117
    {
1118
        $this->getRepository()->init();
1119
        $this->addFile('test');
1120
        $this->getRepository()->commit('Test commit', true);
1121
        $this->addFile('test2');
1122
        $this->getRepository()->stash('My stash', true);
1123
        $this->getRepository()->stashBranch('testbranch', 0);
1124
        $this->assertEquals('testbranch', $this->getRepository()->getMainBranch()->getName());
1125
    }
1126
1127
    /**
1128
     * @covers \GitElephant\Repository::stashCreate
1129
     */
1130
    public function testStashCreate(): void
1131
    {
1132
        $this->getRepository()->init();
1133
        $this->addFile('test');
1134
        $this->getRepository()->commit('Test commit', true);
1135
        $objectName = $this->getRepository()->stashCreate();
1136
        $this->assertIsString($objectName);
1137
    }
1138
1139
    /**
1140
     * @covers \GitElephant\Repository::stashCreate
1141
     */
1142
    public function testStashClear(): void
1143
    {
1144
        $this->getRepository()->init();
1145
        $this->addFile('test');
1146
        $this->getRepository()->commit('Test commit', true);
1147
        $this->addFile('test2');
1148
        $this->getRepository()->stash('My stash', true);
1149
        $this->addFile('test3');
1150
        $this->getRepository()->stash('My stash 2', true);
1151
        $this->getRepository()->stashClear();
1152
        $this->assertCount(0, $this->getRepository()->stashList());
1153
    }
1154
}
1155