Passed
Push — master ( b89ee9...82ea21 )
by Sebastian
03:18
created

CmdTest::createEventMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[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
12
namespace CaptainHook\App\Composer;
13
14
use CaptainHook\App\CH;
15
use CaptainHook\App\Git\DummyRepo;
16
use Composer\Composer;
17
use Composer\IO\NullIO;
18
use Composer\Package\Package;
19
use Composer\Script\Event;
20
use Exception;
21
use org\bovigo\vfs\vfsStream;
22
use PHPUnit\Framework\TestCase;
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
24
class CmdTest extends TestCase
25
{
26
    /**
27
     * Tests Cmd::setup
28
     *
29
     * @throws \Exception
30
     */
31
    public function testSetupConfigExists(): void
32
    {
33
        $repo = new DummyRepo(
34
            [],
35
            [
36
                'composer'   => [
37
                    'autoload.php' => ''
38
                ],
39
                CH::CONFIG => '{"config":{"bootstrap": "composer/autoload.php"}}'
40
            ]
41
        );
42
43
        $config = $repo->getRoot() . '/' . CH::CONFIG;
44
        $event  = $this->createEventMock(['captainhook-config' => $config]);
45
46
        Cmd::setup($event);
47
48
        $this->assertFileExists($repo->getHookDir() . '/pre-commit');
49
        $this->assertFileExists($repo->getHookDir() . '/pre-push');
50
        $this->assertFileExists($repo->getHookDir() . '/commit-msg');
51
    }
52
53
    /**
54
     * Tests Cmd::setup
55
     *
56
     * @throws \Exception
57
     */
58
    public function testSubDirectoryInstall(): void
59
    {
60
        $repo = new DummyRepo(
61
            [],
62
            [
63
                'app' => [
64
                    'custom' => [
65
                        'autoload.php' => ''
66
                    ],
67
                    CH::CONFIG => '{"config":{"bootstrap": "custom/autoload.php"}}'
68
                ]
69
            ]
70
        );
71
72
        $config = $repo->getRoot() . '/app/' . CH::CONFIG;
73
        $event  = $this->createEventMock(['captainhook-config' => $config]);
74
75
        Cmd::setup($event);
76
77
        $this->assertFileExists($repo->getHookDir() . '/pre-commit');
78
        $this->assertFileExists($repo->getHookDir() . '/pre-push');
79
        $this->assertFileExists($repo->getHookDir() . '/commit-msg');
80
    }
81
82
    /**
83
     * Tests Cmd::setup
84
     *
85
     * @throws \Exception
86
     */
87
    public function testGitDirectoryNotFound(): void
88
    {
89
        $this->expectException(Exception::class);
90
91
        $fakeConfig = vfsStream::setup('root', null, [CH::CONFIG => '{}']);
92
        $event      = $this->createEventMock(['captainhook-config' => $fakeConfig->url() . '/' . CH::CONFIG]);
93
94
        Cmd::setup($event);
95
    }
96
97
    /**
98
     * Tests Cmd::setup
99
     *
100
     * @throws \Exception
101
     */
102
    public function testSetupNoConfig(): void
103
    {
104
        $repo   = new DummyRepo(
105
            [],
106
            [
107
                'vendor' => [
108
                    'autoload.php' => ''
109
                ]
110
            ]
111
        );
112
        $config = $repo->getRoot() . '/' . CH::CONFIG;
113
        $extra  = ['captainhook-config' => $config];
114
        $event  = $this->createEventMock($extra);
115
116
        Cmd::setup($event);
117
118
        $this->assertFileExists($extra['captainhook-config']);
119
    }
120
121
    /**
122
     * Create event mock to test composer scripts
123
     *
124
     * @param  array $extra
125
     * @return \Composer\Script\Event&\PHPUnit\Framework\MockObject\MockObject
126
     */
127
    private function createEventMock(array $extra = [])
128
    {
129
        $composer = $this->createComposerMock($extra);
130
        $event    = $this->getMockBuilder(Event::class)
131
                         ->disableOriginalConstructor()
132
                         ->getMock();
133
134
        $event->expects($this->once())->method('getIO')->willReturn(new NullIO());
135
        $event->expects($this->once())->method('getComposer')->willReturn($composer);
136
137
        return $event;
138
    }
139
140
    /**
141
     * Create composer mock to return composer extra config
142
     *
143
     * @param  array $extra
144
     * @return \Composer\Composer&\PHPUnit\Framework\MockObject\MockObject
145
     */
146
    private function createComposerMock(array $extra = [])
147
    {
148
        $package = $this->getMockBuilder(Package::class)
149
                        ->disableOriginalConstructor()
150
                        ->getMock();
151
        $package->expects($this->atLeast(1))->method('getExtra')->willReturn($extra);
152
153
        $composer = $this->getMockBuilder(Composer::class)
154
                         ->disableOriginalConstructor()
155
                         ->getMock();
156
        $composer->expects($this->atLeast(1))->method('getPackage')->willReturn($package);
157
158
        return $composer;
159
    }
160
}
161