BranchTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 110
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructor() 0 17 1
A testBranchCreate() 0 9 1
A testToString() 0 8 1
A testCreate() 0 11 1
A testGetMatches() 0 24 1
A testGetMatchesShortShaError() 0 6 1
A testGetMatchesNoSpaceError() 0 5 1
1
<?php
2
3
/**
4
 * User: matteo
5
 * Date: 05/01/13
6
 * Time: 0.18
7
 *
8
 * Just for fun...
9
 */
10
11
namespace GitElephant\Objects;
12
13
use GitElephant\TestCase;
14
15
/**
16
 * Branch tests
17
 */
18
class BranchTest extends TestCase
19
{
20
    /**
21
     * testGetMatches
22
     */
23
    public function testGetMatches(): void
24
    {
25
        $matches = Branch::getMatches('* develop 45eac8c31adfbbf633824cee6ce8cc5040b33513 test message');
26
        $this->assertEquals('develop', $matches[1]);
27
        $this->assertEquals('45eac8c31adfbbf633824cee6ce8cc5040b33513', $matches[2]);
28
        $this->assertEquals('test message', $matches[3]);
29
30
        $matches = Branch::getMatches('  develop 45eac8c31adfbbf633824cee6ce8cc5040b33513 test message');
31
        $this->assertEquals('develop', $matches[1]);
32
        $this->assertEquals('45eac8c31adfbbf633824cee6ce8cc5040b33513', $matches[2]);
33
        $this->assertEquals('test message', $matches[3]);
34
35
        $matches = Branch::getMatches('  test/branch 45eac8c31adfbbf633824cee6ce8cc5040b33513 test "message" with?');
36
        $this->assertEquals('test/branch', $matches[1]);
37
        $this->assertEquals('45eac8c31adfbbf633824cee6ce8cc5040b33513', $matches[2]);
38
        $this->assertEquals('test "message" with?', $matches[3]);
39
40
        $matches = Branch::getMatches(
41
            "* (detached from 7a02066) 7a020660489a31ed9d0d6a42d5a0f0379334ba82 Merge branch 'PERFORM' into 'master'"
42
        );
43
        $this->assertEquals('detached', $matches[1]);
44
        $this->assertEquals('7a020660489a31ed9d0d6a42d5a0f0379334ba82', $matches[2]);
45
        $this->assertEquals("Merge branch 'PERFORM' into 'master'", $matches[3]);
46
    }
47
48
    /**
49
     * testGetMatchesErrors
50
     */
51
    public function testGetMatchesShortShaError(): void
52
    {
53
        // short sha
54
        $this->expectException(\InvalidArgumentException::class);
55
        Branch::getMatches('* develop 45eac8c31adfbbf633824cee6ce8cc5040b3351 test message');
56
    }
57
58
    /**
59
     * testGetMatchesErrors
60
     */
61
    public function testGetMatchesNoSpaceError(): void
62
    {
63
        $this->expectException(\InvalidArgumentException::class);
64
        Branch::getMatches('* develop 45eac8c31adfbbf633824cee6ce8cc5040b33511test message');
65
    }
66
67
    /**
68
     * test constructor
69
     */
70
    public function testConstructor(): void
71
    {
72
        $this->getRepository()->init();
73
        $this->addFile('test');
74
        $this->getRepository()->commit('test commit', true);
75
        $b = new Branch($this->getRepository(), 'master');
76
        $this->assertEquals('master', $b->getName());
77
        $this->assertEquals('test commit', $b->getComment());
78
        $this->assertTrue($b->getCurrent());
79
        $this->getRepository()->createBranch('develop');
80
        $b = new Branch($this->getRepository(), 'develop');
81
        $this->assertEquals('develop', $b->getName());
82
        $this->assertEquals('test commit', $b->getComment());
83
        $this->assertFalse($b->getCurrent());
84
        $this->expectException('GitElephant\Exception\InvalidBranchNameException');
85
        $this->fail(Branch::checkout($this->getRepository(), 'non-existent'));
86
    }
87
88
    /**
89
     * testBranchCreate
90
     */
91
    public function testBranchCreate(): void
92
    {
93
        $this->getRepository()->init();
94
        $this->addFile('test');
95
        $this->getRepository()->commit('test', true);
96
        Branch::create($this->getRepository(), 'test-branch');
97
        $this->assertCount(2, $this->getRepository()->getBranches());
98
        $this->assertContains('test-branch', $this->getRepository()->getBranches(true));
99
    }
100
101
    /**
102
     * __toString
103
     */
104
    public function testToString(): void
105
    {
106
        $this->getRepository()->init();
107
        $this->addFile('test');
108
        $this->getRepository()->commit('test commit', true);
109
        $b = Branch::checkout($this->getRepository(), 'master');
110
        $this->assertEquals($this->getRepository()->getLog()->last()->getSha(), $b->__toString());
111
    }
112
113
    /**
114
     * testCreate
115
     */
116
    public function testCreate(): void
117
    {
118
        $this->getRepository()->init();
119
        $this->addFile('test');
120
        $this->repository->commit('test', true);
121
        $this->assertCount(1, $this->repository->getBranches(true));
122
        Branch::create($this->repository, 'test-branch');
0 ignored issues
show
Bug introduced by
It seems like $this->repository can also be of type array<integer,object<GitElephant\Repository>>; however, GitElephant\Objects\Branch::create() does only seem to accept object<GitElephant\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
123
        $this->assertCount(2, $this->repository->getBranches(true));
124
        Branch::create($this->repository, 'test-branch2', 'test-branch');
0 ignored issues
show
Bug introduced by
It seems like $this->repository can also be of type array<integer,object<GitElephant\Repository>>; however, GitElephant\Objects\Branch::create() does only seem to accept object<GitElephant\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
125
        $this->assertCount(3, $this->repository->getBranches(true));
126
    }
127
}
128