Passed
Push — master ( c074c3...5fb5b3 )
by Sebastian
02:59 queued 47s
created

EnsureNamingTest::testExecuteCustomSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 24
rs 9.7998
cc 1
nc 1
nop 0
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\Hook\Branch\Action;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO\NullIO;
16
use CaptainHook\App\Mockery;
17
use Exception;
18
use PHPUnit\Framework\TestCase;
19
20
class EnsureNamingTest extends TestCase
21
{
22
    use Mockery;
23
24
    /**
25
     * Tests EnsureNaming::getRestriction
26
     */
27
    public function testConstraint(): void
28
    {
29
        $this->assertTrue(EnsureNaming::getRestriction()->isApplicableFor('pre-commit'));
30
        $this->assertTrue(EnsureNaming::getRestriction()->isApplicableFor('pre-push'));
31
        $this->assertTrue(EnsureNaming::getRestriction()->isApplicableFor('post-checkout'));
32
        $this->assertFalse(EnsureNaming::getRestriction()->isApplicableFor('post-commit'));
33
    }
34
35
    /**
36
     * Tests EnsureNaming::execute
37
     *
38
     * @throws \Exception
39
     */
40
    public function testExecuteDefaultSuccess(): void
41
    {
42
        $io = $this->createPartialMock(NullIO::class, ['write']);
43
        $io->expects($this->once())->method('write')->with('<info>OK</info> Branch name does match regex: #bar#');
44
        /** @var NullIO $io */
45
46
        $config  = new Config(CH_PATH_FILES . '/captainhook.json');
47
        $repo    = $this->createRepositoryMock();
48
        $repo->expects($this->once())->method('getInfoOperator')->willReturn(
49
            $this->createGitInfoOperator('', 'Foo bar baz')
50
        );
51
52
        $action  = new Config\Action(EnsureNaming::class, ['regex' => '#bar#']);
53
54
        $standard = new EnsureNaming();
55
        $standard->execute($config, $io, $repo, $action);
56
57
        $this->assertTrue(true);
58
    }
59
60
    /**
61
     * Tests EnsureNaming::execute
62
     *
63
     * @throws \Exception
64
     */
65
    public function testExecuteCustomSuccess(): void
66
    {
67
        $successMessage = 'Regex matched';
68
        $io             = $this->createPartialMock(NullIO::class, ['write']);
69
        $io->expects($this->once())->method('write')->with($successMessage);
70
        /** @var NullIO $io */
71
72
        $config  = new Config(CH_PATH_FILES . '/captainhook.json');
73
        $repo    = $this->createRepositoryMock();
74
        $repo->expects($this->once())->method('getInfoOperator')->willReturn(
75
            $this->createGitInfoOperator('', 'Foo bar baz')
76
        );
77
        $action  = new Config\Action(
78
            EnsureNaming::class,
79
            [
80
                'regex'   => '#.*#',
81
                'success' => $successMessage
82
            ]
83
        );
84
85
        $standard = new EnsureNaming();
86
        $standard->execute($config, $io, $repo, $action);
87
88
        $this->assertTrue(true);
89
    }
90
91
    /**
92
     * Tests EnsureNaming::execute
93
     *
94
     * @throws \Exception
95
     */
96
    public function testExecuteInvalidOption(): void
97
    {
98
        $this->expectException(Exception::class);
99
100
        $io     = new NullIO();
101
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
102
        $repo    = $this->createRepositoryMock();
103
        $action = new Config\Action(EnsureNaming::class);
104
105
        $standard = new EnsureNaming();
106
        $standard->execute($config, $io, $repo, $action);
107
    }
108
109
    /**
110
     * Tests EnsureNaming::execute
111
     *
112
     * @throws \Exception
113
     */
114
    public function testExecuteNoMatchDefaultErrorMessage(): void
115
    {
116
        $this->expectException(Exception::class);
117
        $this->expectExceptionMessage('<error>FAIL</error> Branch name does not match regex: #FooBarBaz#');
118
119
        $io     = new NullIO();
120
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
121
        $repo   = $this->createRepositoryMock();
122
        $repo->expects($this->once())->method('getInfoOperator')->willReturn(
123
            $this->createGitInfoOperator('', 'Foo bar baz')
124
        );
125
        $action = new Config\Action(EnsureNaming::class, ['regex' => '#FooBarBaz#']);
126
127
        $standard = new EnsureNaming();
128
        $standard->execute($config, $io, $repo, $action);
129
    }
130
131
    /**
132
     * Tests EnsureNaming::execute
133
     *
134
     * @throws \Exception
135
     */
136
    public function testExecuteNoMatchCustomErrorMessage(): void
137
    {
138
        $this->expectException(Exception::class);
139
        $this->expectExceptionMessage('No match for #FooBarBaz#');
140
141
        $io     = new NullIO();
142
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
143
        $repo   = $this->createRepositoryMock();
144
        $repo->expects($this->once())->method('getInfoOperator')->willReturn(
145
            $this->createGitInfoOperator('', 'Foo bar baz')
146
        );
147
        $action = new Config\Action(
148
            EnsureNaming::class,
149
            [
150
                'regex' => '#FooBarBaz#',
151
                'error' => 'No match for %s'
152
            ]
153
        );
154
155
        $standard = new EnsureNaming();
156
        $standard->execute($config, $io, $repo, $action);
157
    }
158
}
159