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