Passed
Push — master ( a6662b...0e2829 )
by Sebastian
03:17
created

HookTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace CaptainHook\App\Console\Application;
11
12
use CaptainHook\App\CH;
13
use CaptainHook\App\Config;
14
use CaptainHook\App\Git\DummyRepo;
15
use \Exception;
16
use Symfony\Component\Console\Input\ArrayInput;
17
use Symfony\Component\Console\Output\NullOutput;
18
use PHPUnit\Framework\TestCase;
19
20
class HookTest extends TestCase
21
{
22
    private $repo;
23
24
    /**
25
     * Create fake dummy repo
26
     */
27
    public function setUp(): void
28
    {
29
        $this->repo = new DummyRepo();
30
        $this->repo->setup();
31
    }
32
33
    /**
34
     * Cleanup the fake repo
35
     */
36
    public function tearDown(): void
37
    {
38
        $this->repo->cleanup();
39
    }
40
41
    /**
42
     * Tests Hook::run
43
     */
44
    public function testRun(): void
45
    {
46
        if (\defined('PHP_WINDOWS_VERSION_MAJOR')) {
47
            $this->markTestSkipped('not tested on windows');
48
        }
49
50
        $config = new Config(CH_PATH_FILES . '/config/valid.json');
51
        $output = new NullOutput();
52
        $input  = new ArrayInput([
53
            'file' => CH_PATH_FILES . '/git/message/valid.txt',
54
        ]);
55
        $app = new Hook();
56
        $app->setConfigFile($config->getPath());
57
        $app->setRepositoryPath($this->repo->getPath());
58
        $app->setHook('commit-msg');
59
        $app->setAutoExit(false);
60
        $app->run($input, $output);
61
62
        $this->assertTrue(true);
63
    }
64
65
    /**
66
     * Test new error handling
67
     *
68
     * @throws \Exception
69
     */
70
    public function testHookFailure(): void
71
    {
72
        if (\defined('PHP_WINDOWS_VERSION_MAJOR')) {
73
            $this->markTestSkipped('not tested on windows');
74
        }
75
76
        $output = $this->createMock(NullOutput::class);
77
        $output->expects($this->once())->method('isDebug')->willReturn(false);
78
        $output->expects($this->once())->method('isVerbose')->willReturn(true);
79
        $output->expects($this->exactly(2))->method('writeLn');
80
81
        $app    = new Hook();
82
        $config = new Config(CH_PATH_FILES . '/config/valid-but-failing.json');
83
        $input  = new ArrayInput([]);
84
85
        $app->setConfigFile($config->getPath());
86
        $app->setRepositoryPath($this->repo->getPath());
87
        $app->setAutoExit(false);
88
        $app->setHook('pre-commit');
89
        $app->run($input, $output);
90
91
92
        $this->assertTrue(true);
93
    }
94
95
96
    /**
97
     * Test new error handling
98
     *
99
     * @throws \Exception
100
     */
101
    public function testHookFailureWhileDebugging(): void
102
    {
103
        if (\defined('PHP_WINDOWS_VERSION_MAJOR')) {
104
            $this->markTestSkipped('not tested on windows');
105
        }
106
107
        $output = $this->createMock(NullOutput::class);
108
        $output->expects($this->once())->method('isDebug')->willReturn(true);
109
110
        $app    = new Hook();
111
        $config = new Config(CH_PATH_FILES . '/config/valid-but-failing.json');
112
        $input  = new ArrayInput([]);
113
114
        $app->setConfigFile($config->getPath());
115
        $app->setRepositoryPath($this->repo->getPath());
116
        $app->setAutoExit(false);
117
        $app->setHook('pre-commit');
118
        $app->run($input, $output);
119
120
        $this->assertTrue(true);
121
    }
122
123
    /**
124
     * Tests Hook::executeHook
125
     */
126
    public function testRunInvalidHook(): void
127
    {
128
        $this->expectException(\Exception::class);
129
130
        $app = new Hook();
131
        $app->setHook('pre-foo');
132
    }
133
134
    /**
135
     * Tests Hook::executeHook
136
     */
137
    public function testRunNoHook(): void
138
    {
139
        $input  = new ArrayInput([]);
140
        $output = new NullOutput();
141
        $app    = new Hook();
142
        $app->setAutoExit(false);
143
        $exit = $app->run($input, $output);
144
145
        $this->assertTrue($exit !== 0);
146
    }
147
148
    /**
149
     * Tests Hook::getRepositoryPath
150
     */
151
    public function testGetRepositoryPath(): void
152
    {
153
        $app = new Hook();
154
155
        $this->assertEquals(getcwd(), $app->getRepositoryPath());
156
    }
157
158
    /**
159
     * Tests Hook::getConfigFile
160
     */
161
    public function testGetConfigFile(): void
162
    {
163
        $app = new Hook();
164
165
        $this->assertEquals(getcwd()  . DIRECTORY_SEPARATOR . CH::CONFIG, $app->getConfigFile());
166
    }
167
168
    /**
169
     * Tests Application::getHelp
170
     */
171
    public function testGetHelp(): void
172
    {
173
        $hook = new Hook();
174
        $help = $hook->getHelp();
175
176
        $this->assertStringContainsString('$$$$b ^ceeeee.  4$$ECL.F*$$$$$$$', $help);
177
    }
178
}
179