RemoteTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
18
/**
19
 * Class RemoteTest
20
 *
21
 * Remote Object Test
22
 *
23
 * @package GitElephant\Command
24
 * @author  David Neimeyer <[email protected]>
25
 */
26
class RemoteTest extends TestCase
27
{
28
    /**
29
     * test double branch name
30
     *
31
     * @var string
32
     */
33
    protected $startBranchName = 'test_branch';
34
35
    /**
36
     * test double tag name
37
     *
38
     * @var string
39
     */
40
    protected $startTagName = 'test_start_tag';
41
42
    /**
43
     * test setup
44
     */
45
    public function setUp(): void
46
    {
47
        $this->initRepository();
48
        $repo = $this->getRepository();
49
        $repo->init();
50
        $this->addFile('test');
51
        $repo->commit('test', true);
52
        $repo->createTag($this->startTagName);
53
        $repo->createBranch($this->startBranchName);
54
        $repo->checkout($this->startBranchName);
55
    }
56
57
    /**
58
     * return sample output of git-remote --verbose
59
     *
60
     * @return string
61
     */
62
    public function sampleRemoteVerbose(): string
63
    {
64
        $name = $this->sampleRemoteShowRemoteName();
65
        $fetch = $this->sampleRemoteShowFetchURL();
66
        $push = $this->sampleRemoteShowPushURL();
67
68
        return <<<EOM
69
origin	originFoo (fetch)
70
origin	originBar (push)
71
{$name}	{$fetch} (fetch)
72
{$name}	{$push} (push)
73
EOM;
74
    }
75
76
    /**
77
     * test double fetch URL
78
     *
79
     * @return string
80
     */
81
    public function sampleRemoteShowFetchURL(): string
82
    {
83
        return '[email protected]:blurg/burpfetch.git';
84
    }
85
86
    /**
87
     * test double push URL
88
     *
89
     * @return string
90
     */
91
    public function sampleRemoteShowPushURL(): string
92
    {
93
        return '[email protected]:blurg/burppush.git';
94
    }
95
96
    /**
97
     * test double remote name
98
     *
99
     * @return string
100
     */
101
    public function sampleRemoteShowRemoteName(): string
102
    {
103
        return 'delphi';
104
    }
105
106
    /**
107
     * test double remote HEAD branch
108
     *
109
     * @return string
110
     */
111
    public function sampleRemoteShowRemoteHEAD(): string
112
    {
113
        return 'Apollo';
114
    }
115
116
    /**
117
     * sample output of git-remote show <remoteName>
118
     *
119
     * @return string
120
     */
121
    public function sampleRemoteShow(): string
122
    {
123
        $name = $this->sampleRemoteShowRemoteName();
124
        $fetch = $this->sampleRemoteShowFetchURL();
125
        $push = $this->sampleRemoteShowPushURL();
126
        $head = $this->sampleRemoteShowRemoteHEAD();
127
128
        return <<<EOM
129
* remote {$name}
130
  Fetch URL: {$fetch}
131
  Push  URL: {$push}
132
  HEAD branch: {$head}
133
  Remote branches:
134
    11.30.6                                        tracked
135
    11.32                                          tracked
136
    11.32.0                                        tracked
137
    11.40                                          tracked
138
    master                                         tracked
139
    pbi_4371                                       tracked
140
    refs/remotes/upstream/58120-squashed           stale (use 'git remote prune' to remove)
141
  Local branches configured for 'git pull':
142
    11.30.6 merges with remote 11.30.6
143
    11.32.0 merges with remote 11.32.0
144
    11.40   merges with remote 11.40
145
    master  merges with remote master
146
  Local refs configured for 'git push':
147
    11.30   pushes to 11.30   (local out of date)
148
    11.30.6 pushes to 11.30.6 (up to date)
149
    11.32   pushes to 11.32   (local out of date)
150
    11.32.0 pushes to 11.32.0 (up to date)
151
    11.40   pushes to 11.40   (local out of date)
152
    master  pushes to master  (up to date)
153
EOM;
154
    }
155
156
    /**
157
     * expected branch structure produced when
158
     * parsing the sample output of git-remote show <remoteName>
159
     *
160
     * @return array
161
     */
162
    public function sampleRemoteShowAsArray(): array
163
    {
164
        return [
165
            '11.30' => [
166
                'pushes_to' => '11.30',
167
                'local_state' => '(local out of date)',
168
            ],
169
            '11.30.6' => [
170
                'pushes_to' => '11.30.6',
171
                'local_state' => '(up to date)',
172
                'merges_with' => '11.30.6',
173
                'local_relationship' => 'tracked',
174
            ],
175
            '11.32.0' => [
176
                'pushes_to' => '11.32.0',
177
                'local_state' => '(up to date)',
178
                'merges_with' => '11.32.0',
179
                'local_relationship' => 'tracked',
180
            ],
181
            '11.32' => [
182
                'pushes_to' => '11.32',
183
                'local_state' => '(local out of date)',
184
                'local_relationship' => 'tracked',
185
            ],
186
            '11.40' => [
187
                'pushes_to' => '11.40',
188
                'merges_with' => '11.40',
189
                'local_state' => '(local out of date)',
190
                'local_relationship' => 'tracked',
191
            ],
192
            'master' => [
193
                'pushes_to' => 'master',
194
                'local_state' => '(up to date)',
195
                'merges_with' => 'master',
196
                'local_relationship' => 'tracked',
197
            ],
198
            'pbi_4371' => [
199
                'local_relationship' => 'tracked',
200
            ],
201
            'refs/remotes/upstream/58120-squashed' => [
202
                'local_relationship' => 'stale',
203
            ],
204
        ];
205
    }
206
207
    /**
208
     * test name getter
209
     */
210
    public function testGetName(): void
211
    {
212
        $sample = $this->sampleRemoteShow();
213
        $output = explode("\n", $sample);
214
        $remote = new Remote($this->getRepository());
215
        $remote->parseOutputLines($output);
216
        $actual = $remote->getName();
217
        $expected = $this->sampleRemoteShowRemoteName();
218
        $this->assertEquals($expected, $actual, 'parseOutputLines() proper digests git-remote show <remote> name');
219
    }
220
221
    /**
222
     * test name setter
223
     */
224
    public function testSetName(): void
225
    {
226
        $expected = 'foobar';
227
        $remote = new Remote($this->getRepository());
228
        $remote->setName($expected);
229
        $actual = $remote->getName();
230
        $this->assertEquals($expected, $actual, 'can set remote name');
231
    }
232
233
    /**
234
     * test fetch URL getter
235
     */
236
    public function testGetFetchURL(): void
237
    {
238
        $sample = $this->sampleRemoteShow();
239
        $output = explode("\n", $sample);
240
        $remote = new Remote($this->getRepository());
241
        $remote->parseOutputLines($output);
242
        $actual = $remote->getFetchURL();
243
        $expected = $this->sampleRemoteShowFetchURL();
244
        $this->assertEquals($expected, $actual, 'parseOutputLines() proper digests git-remote show <remote> fetch URL');
245
    }
246
247
    /**
248
     * test fetch URL setter
249
     */
250
    public function testSetFetchURL(): void
251
    {
252
        $expected = 'foobar';
253
        $remote = new Remote($this->getRepository());
254
        $remote->setFetchURL($expected);
255
        $actual = $remote->getFetchURL();
256
        $this->assertEquals($expected, $actual, 'can set fetch URL property');
257
    }
258
259
    /**
260
     * test push URL getter
261
     */
262
    public function testGetPushURL(): void
263
    {
264
        $sample = $this->sampleRemoteShow();
265
        $output = explode("\n", $sample);
266
        $remote = new Remote($this->getRepository());
267
        $remote->parseOutputLines($output);
268
        $actual = $remote->getPushURL();
269
        $expected = $this->sampleRemoteShowPushURL();
270
        $this->assertEquals(
271
            $expected,
272
            $actual,
273
            'parseOutputLines() proper digests git-remote show <remote> push URL'
274
        );
275
    }
276
277
    /**
278
     * test push URL setter
279
     */
280
    public function testSetPushURL(): void
281
    {
282
        $expected = 'foobar';
283
        $remote = new Remote($this->getRepository());
284
        $remote->setPushURL($expected);
285
        $actual = $remote->getPushURL();
286
        $this->assertEquals($expected, $actual, 'can set push URL property');
287
    }
288
289
    /**
290
     * test remote HEAD branch getter
291
     */
292
    public function testGetRemoteHEAD(): void
293
    {
294
        $sample = $this->sampleRemoteShow();
295
        $output = explode("\n", $sample);
296
        $remote = new Remote($this->getRepository());
297
        $remote->parseOutputLines($output);
298
        $actual = $remote->getRemoteHEAD();
299
        $expected = $this->sampleRemoteShowRemoteHEAD();
300
        $this->assertEquals(
301
            $expected,
302
            $actual,
303
            'parseOutputLines() proper digests git-remote show <remote> remote HEAD'
304
        );
305
    }
306
307
    /**
308
     * test remote HEAD branch setter
309
     */
310
    public function testSetRemoteHEAD(): void
311
    {
312
        $expected = 'foobar';
313
        $remote = new Remote($this->getRepository());
314
        $remote->setRemoteHEAD($expected);
315
        $actual = $remote->getRemoteHEAD();
316
        $this->assertEquals($expected, $actual, 'can set remote HEAD property');
317
    }
318
319
    /**
320
     * test branch detail parsing
321
     */
322
    public function testParseOutputLines(): void
323
    {
324
        $sample = $this->sampleRemoteShow();
325
        $output = explode("\n", $sample);
326
        $remote = new Remote($this->getRepository());
327
        $remote->parseOutputLines($output);
328
        $actual = $remote->getBranches();
329
        $expected = $this->sampleRemoteShowAsArray();
330
        $this->assertEquals(
331
            $expected,
332
            $actual,
333
            'parseOutputLines() proper digests git-remote show <remote> branch references'
334
        );
335
    }
336
337
    /**
338
     * helper for getting a mock Remote object
339
     *
340
     * the returned test double will provide the sample output
341
     * defined in other methods of this test class
342
     *
343
     * NOTE: this will do an assertion in the hope to ensure
344
     * sanity of the test double
345
     *
346
     * @return \GitElephant\Objects\Remote
347
     */
348
    public function getMockRemote()
349
    {
350
        $sample = $this->sampleRemoteShow();
351
        $showOutput = explode("\n", $sample);
352
        $sample = $this->sampleRemoteVerbose();
353
        $verboseOutput = explode("\n", $sample);
354
355
        $mockRemote = $this->getMockBuilder(Remote::class)
356
            ->disableOriginalConstructor()
357
            ->setMethods(['getShowOutput', 'getVerboseOutput'])
358
            ->getMock();
359
360
        $mockRemote->expects($this->any())
361
            ->method('getShowOutput')
362
            ->will($this->returnValue($showOutput));
363
        $mockRemote->expects($this->any())
364
            ->method('getVerboseOutput')
365
            ->will($this->returnValue($verboseOutput));
366
367
        $name = $this->sampleRemoteShowRemoteName();
368
        $mockRemote->__construct($this->getRepository(), $name);
369
        $this->assertEquals(
370
            $this->sampleRemoteShowRemoteName(),
371
            $mockRemote->getName(),
372
            'able to create mock object with properly parsed sample data'
373
        );
374
375
        return $mockRemote;
376
    }
377
378
    /**
379
     * verify object
380
     *
381
     * NOTE: this is actually verifying the mock object and
382
     * could be useless if the helper that makes the double
383
     * is flawed.  However, it should do the trick so other tests
384
     * that depend on the double will have supporting test data/failures
385
     */
386
    public function testConstructor(): void
387
    {
388
        $obj = $this->getMockRemote();
389
        $this->assertInstanceOf(Remote::class, $obj);
390
    }
391
392
    /**
393
     * verify name is produced with cast to a string
394
     */
395
    public function testToString(): void
396
    {
397
        $obj = $this->getMockRemote();
398
        $this->assertEquals(
399
            $this->sampleRemoteShowRemoteName(),
400
            (string) $obj,
401
            'magic to string method provides the remote name'
402
        );
403
    }
404
405
    /**
406
     * verify that we always get an array, even if empty, when getting
407
     * data from the underlying implementation
408
     */
409
    public function testGetVerboseOutputReturnArray(): void
410
    {
411
        $remote = new Remote($this->getRepository());
412
        $actual = $remote->getVerboseOutput();
413
        $this->assertTrue(is_array($actual), 'getVerboseOutput() returns array');
414
    }
415
416
    /**
417
     * verify that we always get an array, even if empty, when getting
418
     * data from the underlying implementation
419
     */
420
    public function testGetShowOutputReturnArray(): void
421
    {
422
        $remote = new Remote($this->getRepository());
423
        $actual = $remote->getShowOutput();
424
        $this->assertTrue(is_array($actual), 'getShowOutput() returns array');
425
    }
426
}
427