Completed
Push — develop ( 83ff38...e11d84 )
by
unknown
03:25
created

TestCase::addOutputToMockRepo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object<GitElephant\Repository> of property $repository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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 ?
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $folder of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $command is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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'));
0 ignored issues
show
Unused Code introduced by
The call to TestCase::getMock() has too many arguments starting with array('showCommit').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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(),
0 ignored issues
show
Unused Code introduced by
The call to TestCase::getMock() has too many arguments starting with array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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