|
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\MvCommand; |
|
17
|
|
|
use \GitElephant\Repository; |
|
18
|
|
|
use \GitElephant\GitBinary; |
|
19
|
|
|
use \GitElephant\Command\Caller\Caller; |
|
20
|
|
|
use \GitElephant\Objects\Commit; |
|
21
|
|
|
use \Symfony\Component\Finder\Finder; |
|
22
|
|
|
use \Symfony\Component\Filesystem\Filesystem; |
|
23
|
|
|
use \Mockery as m; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class TestCase |
|
27
|
|
|
* |
|
28
|
|
|
* @package GitElephant |
|
29
|
|
|
*/ |
|
30
|
|
|
class TestCase extends \PHPUnit\Framework\TestCase |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var \GitElephant\Command\Caller\CallerInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $caller; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var Repository |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $repository; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $path; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var Finder |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $finder; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param null $name |
|
54
|
|
|
* |
|
55
|
|
|
* @return \GitElephant\Repository |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function getRepository($name = null) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($this->repository == null) { |
|
60
|
|
|
$this->initRepository($name); |
|
61
|
|
|
} |
|
62
|
|
|
if (is_null($name)) { |
|
63
|
|
|
return $this->repository; |
|
64
|
|
|
} else { |
|
65
|
|
|
return $this->repository[$name]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return \GitElephant\Command\Caller\Caller |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getCaller() |
|
73
|
|
|
{ |
|
74
|
|
|
if ($this->caller == null) { |
|
75
|
|
|
$this->initRepository(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->caller; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param null|string $name the folder name |
|
83
|
|
|
* @param int $index the repository index (for getting them back) |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function initRepository($name = null, $index = null) |
|
88
|
|
|
{ |
|
89
|
|
|
$tempDir = realpath(sys_get_temp_dir()); |
|
90
|
|
|
$tempName = null === $name ? tempnam($tempDir, 'gitelephant') : $tempDir.DIRECTORY_SEPARATOR.$name; |
|
91
|
|
|
$this->path = $tempName; |
|
92
|
|
|
@unlink($this->path); |
|
|
|
|
|
|
93
|
|
|
$fs = new Filesystem(); |
|
94
|
|
|
$fs->mkdir($this->path); |
|
95
|
|
|
$this->caller = new Caller(new GitBinary(), $this->path); |
|
96
|
|
|
if (is_null($index)) { |
|
97
|
|
|
$this->repository = Repository::open($this->path); |
|
98
|
|
|
$this->assertInstanceOf('GitElephant\Repository', $this->repository); |
|
99
|
|
|
} else { |
|
100
|
|
|
if (!is_array($this->repository)) { |
|
101
|
|
|
$this->repository = array(); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
$this->repository[$index] = Repository::open($this->path); |
|
104
|
|
|
$this->assertInstanceOf('GitElephant\Repository', $this->repository[$index]); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function tearDown() |
|
109
|
|
|
{ |
|
110
|
|
|
$fs = new Filesystem(); |
|
111
|
|
|
if (is_array($this->repository)) { |
|
112
|
|
|
array_map(function (Repository $repo) use ($fs) { |
|
113
|
|
|
$fs->remove($repo->getPath()); |
|
114
|
|
|
}, $this->repository); |
|
115
|
|
|
} else { |
|
116
|
|
|
$fs->remove($this->path); |
|
117
|
|
|
} |
|
118
|
|
|
m::close(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $name file name |
|
123
|
|
|
* @param string|null $folder folder name |
|
124
|
|
|
* @param null $content content |
|
125
|
|
|
* @param Repository $repository repository to add file to |
|
126
|
|
|
* |
|
127
|
|
|
* @return void |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function addFile($name, $folder = null, $content = null, $repository = null) |
|
130
|
|
|
{ |
|
131
|
|
|
if (is_null($repository)) { |
|
132
|
|
|
$path = $this->path; |
|
133
|
|
|
} else { |
|
134
|
|
|
$path = $repository->getPath(); |
|
135
|
|
|
} |
|
136
|
|
|
$filename = $folder == null ? |
|
|
|
|
|
|
137
|
|
|
$path.DIRECTORY_SEPARATOR.$name : |
|
138
|
|
|
$path.DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR.$name; |
|
139
|
|
|
$handle = fopen($filename, 'w'); |
|
140
|
|
|
$fileContent = $content == null ? 'test content' : $content; |
|
141
|
|
|
$this->assertTrue(false !== fwrite($handle, $fileContent), sprintf('unable to write the file %s', $name)); |
|
142
|
|
|
fclose($handle); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* remove file from repo |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $name |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function removeFile($name) |
|
151
|
|
|
{ |
|
152
|
|
|
$filename = $this->path.DIRECTORY_SEPARATOR.$name; |
|
153
|
|
|
$this->assertTrue(unlink($filename)); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* update a file in the repository |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $name file name |
|
160
|
|
|
* @param string $content content |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function updateFile($name, $content) |
|
163
|
|
|
{ |
|
164
|
|
|
$filename = $this->path.DIRECTORY_SEPARATOR.$name; |
|
165
|
|
|
$this->assertTrue(false !== file_put_contents($filename, $content)); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* rename a file in the repository |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $originName file name |
|
172
|
|
|
* @param string $targetName new file name |
|
173
|
|
|
* @param bool $gitMv use git mv, otherwise uses php rename function (with the Filesystem component) |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function renameFile($originName, $targetName, $gitMv = true) |
|
176
|
|
|
{ |
|
177
|
|
|
if ($gitMv) { |
|
178
|
|
|
$this->getRepository()->getCaller()->execute(MvCommand::getInstance()->rename($originName, $targetName)); |
|
179
|
|
|
|
|
180
|
|
|
return; |
|
181
|
|
|
} |
|
182
|
|
|
$origin = $this->path.DIRECTORY_SEPARATOR.$originName; |
|
183
|
|
|
$target = $this->path.DIRECTORY_SEPARATOR.$targetName; |
|
184
|
|
|
$fs = new Filesystem(); |
|
185
|
|
|
$fs->rename($origin, $target); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param string $name name |
|
190
|
|
|
* |
|
191
|
|
|
* @return void |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function addFolder($name) |
|
194
|
|
|
{ |
|
195
|
|
|
$fs = new Filesystem(); |
|
196
|
|
|
$fs->mkdir($this->path.DIRECTORY_SEPARATOR.$name); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
protected function addSubmodule($url, $path) |
|
200
|
|
|
{ |
|
201
|
|
|
$this->getRepository()->addSubmodule($url, $path); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param $classname |
|
206
|
|
|
* |
|
207
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject |
|
208
|
|
|
*/ |
|
209
|
|
|
protected function getMock($classname) |
|
210
|
|
|
{ |
|
211
|
|
|
return $this |
|
212
|
|
|
->getMockBuilder($classname) |
|
213
|
|
|
->disableOriginalConstructor() |
|
214
|
|
|
->getMock(); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* mock the caller |
|
219
|
|
|
* |
|
220
|
|
|
* @param string $command command |
|
221
|
|
|
* @param string $output output |
|
222
|
|
|
* |
|
223
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function getMockCaller($command, $output) |
|
|
|
|
|
|
226
|
|
|
{ |
|
227
|
|
|
$mock = $this->getMock('GitElephant\Command\Caller\CallerInterface'); |
|
228
|
|
|
$mock |
|
229
|
|
|
->expects($this->any()) |
|
230
|
|
|
->method('execute') |
|
231
|
|
|
->will($this->returnValue($mock)); |
|
232
|
|
|
$mock |
|
233
|
|
|
->expects($this->any()) |
|
234
|
|
|
->method('getOutputLines') |
|
235
|
|
|
->will($this->returnValue($output)); |
|
236
|
|
|
|
|
237
|
|
|
return $mock; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
protected function getMockContainer() |
|
241
|
|
|
{ |
|
242
|
|
|
return $this->getMock('GitElephant\Command\CommandContainer'); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
protected function addCommandToMockContainer(\PHPUnit\Framework\MockObject\MockObject $container, $commandName) |
|
246
|
|
|
{ |
|
247
|
|
|
$container |
|
248
|
|
|
->expects($this->any()) |
|
249
|
|
|
->method('get') |
|
250
|
|
|
->with($this->equalTo($commandName)) |
|
251
|
|
|
->will($this->returnValue($this->getMockCommand())); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
protected function addOutputToMockRepo(\PHPUnit\Framework\MockObject\MockObject $repo, $output) |
|
255
|
|
|
{ |
|
256
|
|
|
$repo |
|
257
|
|
|
->expects($this->any()) |
|
258
|
|
|
->method('getCaller') |
|
259
|
|
|
->will($this->returnValue($this->getMockCaller('', $output))); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
protected function getMockCommand() |
|
263
|
|
|
{ |
|
264
|
|
|
$command = $this->getMock('Command', array('showCommit')); |
|
|
|
|
|
|
265
|
|
|
$command |
|
266
|
|
|
->expects($this->any()) |
|
267
|
|
|
->method('showCommit') |
|
268
|
|
|
->will($this->returnValue('')); |
|
269
|
|
|
|
|
270
|
|
|
return $command; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
protected function getMockRepository() |
|
274
|
|
|
{ |
|
275
|
|
|
return $this->getMock( |
|
276
|
|
|
'GitElephant\Repository', |
|
277
|
|
|
array(), |
|
|
|
|
|
|
278
|
|
|
array( |
|
279
|
|
|
$this->repository->getPath(), |
|
280
|
|
|
$this->getMockBinary() |
|
281
|
|
|
) |
|
282
|
|
|
); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
protected function getMockBinary() |
|
286
|
|
|
{ |
|
287
|
|
|
return $this->getMock('GitElephant\GitBinary'); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
protected function doCommitTest( |
|
291
|
|
|
Commit $commit, |
|
292
|
|
|
$sha, |
|
293
|
|
|
$tree, |
|
294
|
|
|
$author, |
|
295
|
|
|
$committer, |
|
296
|
|
|
$emailAuthor, |
|
297
|
|
|
$emailCommitter, |
|
298
|
|
|
$datetimeAuthor, |
|
299
|
|
|
$datetimeCommitter, |
|
300
|
|
|
$message |
|
301
|
|
|
) { |
|
302
|
|
|
$this->assertInstanceOf('GitElephant\Objects\Commit', $commit); |
|
303
|
|
|
$this->assertEquals($sha, $commit->getSha()); |
|
304
|
|
|
$this->assertEquals($tree, $commit->getTree()); |
|
305
|
|
|
$this->assertInstanceOf('GitElephant\Objects\Author', $commit->getAuthor()); |
|
306
|
|
|
$this->assertEquals($author, $commit->getAuthor()->getName()); |
|
307
|
|
|
$this->assertEquals($emailAuthor, $commit->getAuthor()->getEmail()); |
|
308
|
|
|
$this->assertInstanceOf('GitElephant\Objects\Author', $commit->getCommitter()); |
|
309
|
|
|
$this->assertEquals($committer, $commit->getCommitter()->getName()); |
|
310
|
|
|
$this->assertEquals($emailCommitter, $commit->getCommitter()->getEmail()); |
|
311
|
|
|
$this->assertInstanceOf('\Datetime', $commit->getDatetimeAuthor()); |
|
312
|
|
|
$this->assertEquals($datetimeAuthor, $commit->getDatetimeAuthor()->format('U')); |
|
313
|
|
|
$this->assertInstanceOf('\Datetime', $commit->getDatetimeCommitter()); |
|
314
|
|
|
$this->assertEquals($datetimeCommitter, $commit->getDatetimeCommitter()->format('U')); |
|
315
|
|
|
$this->assertEquals($message, $commit->getMessage()->getShortMessage()); |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: