Completed
Pull Request — develop (#100)
by
unknown
03:44
created

LogTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 149
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 2
A testLogCountable() 0 5 1
A testParents() 0 21 1
B testLogCountLimit() 0 29 1
A testLogOffset() 0 14 1
A testLogIndex() 0 9 1
A testLogToArray() 0 8 1
A testObjectLog() 0 5 1
A testLogCreatedFromOutputLines() 0 11 1
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\Objects;
15
16
use \GitElephant\TestCase;
17
use \GitElephant\Objects\Log;
18
use \GitElephant\Command\LogCommand;
19
20
/**
21
 * LogTest
22
 *
23
 * @author Mathias Geat <[email protected]>
24
 */
25
class LogTest extends TestCase
26
{
27
    /**
28
     * setUp
29
     */
30
    public function setUp()
31
    {
32
        $this->getRepository()->init();
33
34
        for ($i = 0; $i < 10; $i++) {
35
            $this->addFile('test file ' . $i);
36
            $this->getRepository()->commit('test commit index:' . $i, true);
37
        }
38
    }
39
40
    /**
41
     * testLogCountable
42
     */
43
    public function testLogCountable()
44
    {
45
        $log = $this->getRepository()->getLog();
46
        $this->assertEquals($log->count(), count($log));
47
    }
48
49
    /**
50
     * parents created by log
51
     */
52
    public function testParents()
53
    {
54
        $log = $this->getRepository()->getLog();
55
        $lastCommit = $this->repository->getCommit();
56
        $lastLogCommit = $log[0];
57
        $this->assertEquals($lastCommit->getParents(), $lastLogCommit->getParents());
58
        Branch::create($this->repository, 'new-branch');
59
        $this->getRepository()->checkout('new-branch');
60
        $this->addFile('another file');
61
        $this->repository->commit('another commit', true);
62
        $lastCommitOtherBranch = $this->getRepository()->getCommit();
63
        $this->getRepository()->checkout('master');
64
        $this->addFile('another file on master');
65
        $this->getRepository()->commit('new commit on master', true);
66
        $lastCommitOnMaster = $this->getRepository()->getCommit();
67
        $this->getRepository()->merge($this->getRepository()->getBranch('new-branch'));
0 ignored issues
show
Bug introduced by
It seems like $this->getRepository()->getBranch('new-branch') 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...
68
        $log = $this->getRepository()->getLog();
69
        $lastLogCommit = $log[0];
70
        $this->assertContains($lastCommitOnMaster->getSha(), $lastLogCommit->getParents());
71
        $this->assertContains($lastCommitOtherBranch->getSha(), $lastLogCommit->getParents());
72
    }
73
74
    /**
75
     * testLogCountLimit
76
     */
77
    public function testLogCountLimit()
78
    {
79
        $log = $this->getRepository()->getLog(null, null, null);
80
        $this->assertEquals(10, $log->count());
81
82
        $log = $this->getRepository()->getLog(null, null, 10, null);
83
        $this->assertEquals(10, $log->count());
84
85
        $log = $this->getRepository()->getLog(null, null, 50, null);
86
        $this->assertEquals(10, $log->count());
87
88
        $log = $this->getRepository()->getLog(null, null, 60, null);
89
        $this->assertEquals(10, $log->count());
90
91
        $log = $this->getRepository()->getLog(null, null, 1, null);
92
        $this->assertEquals(1, $log->count());
93
94
        $log = $this->getRepository()->getLog(null, null, 0, null);
95
        $this->assertEquals(0, $log->count());
96
97
        $log = $this->getRepository()->getLog(null, null, -1, null);
98
        $this->assertEquals(10, $log->count());
99
100
        $log = $this->getRepository()->getLog(null, "test\ file\ 1", -1, null);
101
        $this->assertEquals(1, $log->count());
102
103
        $log = $this->getRepository()->getLog(null, "test\ file*", -1, null);
104
        $this->assertEquals(10, $log->count());
105
    }
106
107
    /**
108
     * testLogOffset
109
     */
110
    public function testLogOffset()
111
    {
112
        $log = $this->getRepository()->getLog(null, null, null, 0);
113
        $this->assertEquals(10, $log->count());
114
115
        $log = $this->getRepository()->getLog(null, null, null, 5);
116
        $this->assertEquals(5, $log->count());
117
118
        $log = $this->getRepository()->getLog(null, null, null, 50);
119
        $this->assertEquals(0, $log->count());
120
121
        $log = $this->getRepository()->getLog(null, null, null, 100);
122
        $this->assertEquals(0, $log->count());
123
    }
124
125
    /**
126
     * testLogIndex
127
     */
128
    public function testLogIndex()
129
    {
130
        $log = $this->getRepository()->getLog(null, null, null, null);
131
132
        // [0;50[ - 10 = 39
133
        $this->assertEquals('test commit index:7', $log[2]->getMessage()->toString());
134
        $this->assertEquals('test commit index:7', $log->index(2)->getMessage()->toString());
135
        $this->assertEquals('test commit index:7', $log->offsetGet(2)->getMessage()->toString());
136
    }
137
138
    /**
139
     * testLogToArray
140
     */
141
    public function testLogToArray()
142
    {
143
        $log = $this->getRepository()->getLog(null, null, null, null);
144
145
        $this->assertTrue(is_array($log->toArray()));
146
        $this->assertInternalType('array', $log->toArray());
147
        $this->assertEquals($log->count(), count($log->toArray()));
148
    }
149
150
    /**
151
     * testObjectLog
152
     */
153
    public function testObjectLog()
154
    {
155
        $tree = $this->getRepository()->getTree();
156
        $file = $tree[0];
0 ignored issues
show
Unused Code introduced by
$file 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...
157
    }
158
159
    /**
160
     * testLogCreatedFromOutputLines
161
     */
162
    public function testLogCreatedFromOutputLines()
163
    {
164
        $tree = $this->getRepository()->getTree();
165
        $obj = $tree[count($tree) - 1];
166
        $logCommand = new LogCommand();
167
        $command = $logCommand->showObjectLog($obj);
168
        $log = Log::createFromOutputLines($this->getRepository(), $this->caller->execute($command)->getOutputLines());
169
        $this->assertInstanceOf('GitElephant\Objects\Log', $log);
170
        $this->assertCount(1, $log);
171
172
    }
173
}
174