|
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\Repository; |
|
17
|
|
|
use GitElephant\TestCase; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* TreeTest |
|
21
|
|
|
* |
|
22
|
|
|
* Tree class tests |
|
23
|
|
|
* |
|
24
|
|
|
* @author Matteo Giachino <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
class TreeTest extends TestCase |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* setUp |
|
31
|
|
|
*/ |
|
32
|
|
|
public function setUp(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->initRepository(); |
|
35
|
|
|
$this->getRepository()->init(); |
|
36
|
|
|
$this->addFolder('test'); |
|
37
|
|
|
$this->addFile('test/1'); |
|
38
|
|
|
$this->addFile('test/1-2'); |
|
39
|
|
|
$this->addFolder('test/child'); |
|
40
|
|
|
$this->addFile('test/child/2'); |
|
41
|
|
|
$this->addFile('3'); |
|
42
|
|
|
$this->getRepository()->commit('first', true); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* testConstructor |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testConstructor(): void |
|
49
|
|
|
{ |
|
50
|
|
|
$tree = $this->repository->getTree('HEAD'); |
|
51
|
|
|
$this->assertInstanceOf('Traversable', $tree); |
|
52
|
|
|
$this->assertInstanceOf('Countable', $tree); |
|
53
|
|
|
$this->assertCount(2, $tree); |
|
54
|
|
|
$this->addFile('4'); |
|
55
|
|
|
$this->getRepository()->commit('second', true); |
|
56
|
|
|
$tree = $this->repository->getTree('HEAD'); |
|
57
|
|
|
$this->assertCount(3, $tree); |
|
58
|
|
|
$treeObj1 = $tree[0]; |
|
59
|
|
|
$this->assertEquals(NodeObject::TYPE_TREE, $treeObj1->getType()); |
|
60
|
|
|
$treeObj2 = $tree[1]; |
|
61
|
|
|
$this->assertEquals(NodeObject::TYPE_BLOB, $treeObj2->getType()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* testWithPath |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testWithPath(): void |
|
68
|
|
|
{ |
|
69
|
|
|
/** @var Tree $tree */ |
|
70
|
|
|
$tree = $this->repository->getTree('HEAD'); |
|
71
|
|
|
$treeObj1 = $tree[0]; |
|
72
|
|
|
|
|
73
|
|
|
$tree = $this->repository->getTree('HEAD', $treeObj1); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertInstanceOf('Traversable', $tree); |
|
76
|
|
|
$this->assertInstanceOf('Countable', $tree); |
|
77
|
|
|
$this->assertCount(3, $tree); |
|
78
|
|
|
|
|
79
|
|
|
$treeObjChild = $tree[0]; |
|
80
|
|
|
|
|
81
|
|
|
$this->assertEquals(NodeObject::TYPE_TREE, $treeObjChild->getType()); |
|
82
|
|
|
$tree = $this->repository->getTree('HEAD', $treeObjChild); |
|
83
|
|
|
$this->assertCount(1, $tree); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* testSubmodule |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testSubmodule(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$tempDir = realpath(sys_get_temp_dir()) . 'gitelephant_' . md5(uniqid()); |
|
92
|
|
|
// horrible hack because php is beautiful. |
|
93
|
|
|
$tempName = @tempnam($tempDir, 'gitelephant'); |
|
94
|
|
|
$path = $tempName; |
|
95
|
|
|
unlink($path); |
|
96
|
|
|
mkdir($path); |
|
97
|
|
|
$repository = new Repository($path); |
|
98
|
|
|
$repository->init(); |
|
99
|
|
|
$repository->addSubmodule($this->repository->getPath()); |
|
100
|
|
|
$repository->commit('test', true); |
|
101
|
|
|
$tree = $repository->getTree(); |
|
102
|
|
|
$this->assertContainsEquals('.gitmodules', $tree); |
|
103
|
|
|
$this->assertContainsEquals($this->repository->getHumanishName(), $tree); |
|
104
|
|
|
$submodule = $tree[0]; |
|
105
|
|
|
$this->assertEquals(NodeObject::TYPE_LINK, $submodule->getType()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* testIsRoot |
|
110
|
|
|
*/ |
|
111
|
|
|
public function testIsRoot(): void |
|
112
|
|
|
{ |
|
113
|
|
|
$this->initRepository(); |
|
114
|
|
|
$this->getRepository()->init(); |
|
115
|
|
|
$this->addFolder('test'); |
|
116
|
|
|
$this->addFile('test/1'); |
|
117
|
|
|
$this->getRepository()->commit('test', true); |
|
118
|
|
|
$this->assertTrue($this->getRepository()->getTree()->isRoot()); |
|
119
|
|
|
$this->assertFalse($this->getRepository()->getTree('master', 'test')->isRoot()); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* testGetObject |
|
124
|
|
|
*/ |
|
125
|
|
|
public function testGetObject(): void |
|
126
|
|
|
{ |
|
127
|
|
|
$tree = $this->getRepository()->getTree(); |
|
128
|
|
|
$this->assertNull($tree->getObject()); |
|
129
|
|
|
$tree = $this->getRepository()->getTree('HEAD', 'test'); |
|
130
|
|
|
$this->assertNotNull($tree->getObject()); |
|
131
|
|
|
$this->assertEquals(NodeObject::TYPE_TREE, $tree->getObject()->getType()); |
|
132
|
|
|
$tree = $this->getRepository()->getTree('HEAD', 'test/1'); |
|
133
|
|
|
$this->assertEquals(NodeObject::TYPE_BLOB, $tree->getObject()->getType()); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|