Passed
Push — master ( ecf9f1...fcd036 )
by Sebastian
02:58
created

RulesTest::testExecuteClassNotFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
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\Message\Action;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO\NullIO;
16
use CaptainHook\App\Hook\Message\Rule\CapitalizeSubject;
17
use CaptainHook\App\Hook\Message\Rule\LimitSubjectLength;
18
use CaptainHook\App\Mockery;
19
use Exception;
20
use SebastianFeldmann\Git\CommitMessage;
21
use PHPUnit\Framework\TestCase;
22
23
class RulesTest extends TestCase
24
{
25
    use Mockery;
26
27
    /**
28
     * Tests Rulebook::execute
29
     *
30
     * @throws \Exception
31
     */
32
    public function testExecuteEmptyRules(): void
33
    {
34
        $io     = new NullIO();
35
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
36
        $action = new Config\Action(Rules::class);
37
        $repo   = $this->createRepositoryMock();
38
        $repo->method('getCommitMsg')->willReturn(new CommitMessage('Foo bar baz'));
1 ignored issue
show
Bug introduced by
The method method() does not exist on SebastianFeldmann\Git\Repository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        $repo->/** @scrutinizer ignore-call */ 
39
               method('getCommitMsg')->willReturn(new CommitMessage('Foo bar baz'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
40
        $standard = new Rules();
41
        $standard->execute($config, $io, $repo, $action);
42
43
        $this->assertTrue(true);
44
    }
45
46
    /**
47
     * Tests Rulebook::execute
48
     *
49
     * @throws \Exception
50
     */
51
    public function testNoValidationOnMerging(): void
52
    {
53
        $io     = new NullIO();
54
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
55
        $action = new Config\Action(Rules::class);
56
        $repo   = $this->createRepositoryMock();
57
        $repo->expects($this->once())->method('isMerging')->willReturn(true);
58
59
        $standard = new Rules();
60
        $standard->execute($config, $io, $repo, $action);
61
62
        $this->assertTrue(true);
63
    }
64
65
    /**
66
     * Tests Rulebook::execute
67
     */
68
    public function testExecuteClassNotFound(): void
69
    {
70
        $this->expectException(Exception::class);
71
72
        $io     = new NullIO();
73
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
74
        $repo   = $this->createRepositoryMock();
75
        $action = new Config\Action(Rules::class, [Foo::class]);
0 ignored issues
show
Bug introduced by
The type CaptainHook\App\Hook\Message\Action\Foo 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...
76
77
        $standard = new Rules();
78
        $standard->execute($config, $io, $repo, $action);
79
    }
80
81
    /**
82
     * Tests Rulebook::execute
83
     *
84
     * @throws \Exception
85
     */
86
    public function testExecuteInvalidClass(): void
87
    {
88
        $this->expectException(Exception::class);
89
90
        $io     = new NullIO();
91
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
92
        $action = new Config\Action(Rules::class, [Validator::class]);
0 ignored issues
show
Bug introduced by
The type CaptainHook\App\Hook\Message\Action\Validator 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...
93
        $repo   = $this->createRepositoryMock();
94
95
        $standard = new Rules();
96
        $standard->execute($config, $io, $repo, $action);
97
    }
98
99
    /**
100
     * Tests Rulebook::execute
101
     *
102
     * @throws \Exception
103
     */
104
    public function testExecuteValidRule(): void
105
    {
106
        $io     = new NullIO();
107
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
108
        $action = new Config\Action(Rules::class, [CapitalizeSubject::class]);
109
        $repo   = $this->createRepositoryMock();
110
        $repo->method('getCommitMsg')->willReturn(new CommitMessage('Foo bar baz'));
111
112
        $standard = new Rules();
113
        $standard->execute($config, $io, $repo, $action);
114
115
        $this->assertTrue(true);
116
    }
117
118
    /**
119
     * Tests Rulebook::execute
120
     *
121
     * @throws \Exception
122
     */
123
    public function testExecuteValidRuleWithArguments(): void
124
    {
125
        $io     = new NullIO();
126
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
127
        $action = new Config\Action(Rules::class, [[LimitSubjectLength::class, [10]]]);
128
        $repo   = $this->createRepositoryMock();
129
        $repo->method('getCommitMsg')->willReturn(new CommitMessage('Foo bar baz'));
130
131
        try {
132
            $standard = new Rules();
133
            $standard->execute($config, $io, $repo, $action);
134
135
            // exception should be thrown before this
136
            $this->assterTrue(false);
137
        } catch (Exception $e) {
138
            $this->assertTrue(true);
139
        }
140
    }
141
142
    /**
143
     * Tests Rule::execute
144
     *
145
     * @throws \Exception
146
     */
147
    public function testNoRule(): void
148
    {
149
        $this->expectException(Exception::class);
150
151
        $io     = new NullIO();
152
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
153
        $action = new Config\Action(Rules::class, [NoRule::class]);
154
        $repo   = $this->createRepositoryMock();
155
        $repo->method('getCommitMsg')->willReturn(new CommitMessage('Foo bar baz'));
156
157
        $standard = new Rules();
158
        $standard->execute($config, $io, $repo, $action);
159
    }
160
161
    /**
162
     * Tests Rule::execute
163
     *
164
     * @throws \Exception
165
     */
166
    public function testInvalidComplexRule(): void
167
    {
168
        $this->expectException(Exception::class);
169
170
        $io     = new NullIO();
171
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
172
        $action = new Config\Action(Rules::class, [[LimitSubjectLength::class, 'foo']]);
173
        $repo   = $this->createRepositoryMock();
174
175
        $standard = new Rules();
176
        $standard->execute($config, $io, $repo, $action);
177
    }
178
}
179