UtilTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 88
rs 10
c 1
b 0
f 0
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A providerValidCommands() 0 7 1
A testIsValid() 0 6 1
A testIsInstallable() 0 6 1
A testFindPreviousHeadFallback() 0 9 1
A testGetHookCommandInvalid() 0 6 1
A providerInvalidCommands() 0 5 1
A testGetHooks() 0 5 1
A testGetHookCommandValid() 0 7 1
A testFindPreviousHeadFromStdIn() 0 8 1
A testGetValidHooks() 0 5 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\Hook;
13
14
use PHPUnit\Framework\Attributes\DataProvider;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Attributes\DataProvider 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...
15
use PHPUnit\Framework\TestCase;
16
use RuntimeException;
17
use CaptainHook\App\Console\IO\Mockery;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CaptainHook\App\Hook\Mockery. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
19
class UtilTest extends TestCase
20
{
21
    use Mockery;
22
23
    public function testIsValid(): void
24
    {
25
        $this->assertTrue(Util::isValid('pre-commit'));
26
        $this->assertTrue(Util::isValid('pre-push'));
27
        $this->assertTrue(Util::isValid('commit-msg'));
28
        $this->assertFalse(Util::isValid('foo'));
29
    }
30
31
    public function testIsInstallable(): void
32
    {
33
        $this->assertTrue(Util::isInstallable('pre-commit'));
34
        $this->assertTrue(Util::isInstallable('pre-push'));
35
        $this->assertTrue(Util::isValid('post-change'));
36
        $this->assertFalse(Util::isInstallable('post-change'));
37
    }
38
39
    public function testGetValidHooks(): void
40
    {
41
        $this->assertArrayHasKey('pre-commit', Util::getValidHooks());
42
        $this->assertArrayHasKey('pre-push', Util::getValidHooks());
43
        $this->assertArrayHasKey('commit-msg', Util::getValidHooks());
44
    }
45
46
    #[DataProvider('providerValidCommands')]
47
    public function testGetHookCommandValid(string $class, string $hook): void
48
    {
49
        $this->assertEquals($class, Util::getHookCommand($hook));
50
        $this->assertEquals('PreCommit', Util::getHookCommand('pre-commit'));
51
        $this->assertEquals('PrepareCommitMsg', Util::getHookCommand('prepare-commit-msg'));
52
        $this->assertEquals('PrePush', Util::getHookCommand('pre-push'));
53
    }
54
55
    public static function providerValidCommands(): array
56
    {
57
        return [
58
            ['CommitMsg', 'commit-msg'],
59
            ['PreCommit', 'pre-commit'],
60
            ['PrepareCommitMsg', 'prepare-commit-msg'],
61
            ['PrePush', 'pre-push'],
62
        ];
63
    }
64
65
    #[DataProvider('providerInvalidCommands')]
66
    public function testGetHookCommandInvalid(string $hook): void
67
    {
68
        $this->expectException(RuntimeException::class);
69
70
        $this->assertEquals('', Util::getHookCommand($hook));
71
    }
72
73
    public static function providerInvalidCommands(): array
74
    {
75
        return [
76
            [''],
77
            ['foo'],
78
        ];
79
    }
80
81
    public function testGetHooks(): void
82
    {
83
        $this->assertContains('pre-commit', Util::getHooks());
84
        $this->assertContains('pre-push', Util::getHooks());
85
        $this->assertContains('commit-msg', Util::getHooks());
86
    }
87
88
    public function testFindPreviousHeadFallback(): void
89
    {
90
        $io = $this->createIOMock();
91
        $io->method('getStandardInput')->willReturn([]);
92
        $io->method('getArgument')->willReturn('HEAD@{1}');
93
94
        $prev = Util::findPreviousHead($io);
95
96
        $this->assertEquals('HEAD@{1}', $prev);
97
    }
98
99
    public function testFindPreviousHeadFromStdIn(): void
100
    {
101
        $io = $this->createIOMock();
102
        $io->method('getStandardInput')->willReturn(['foo a1a1a1a1 Something', 'bar b2b2b2b2 Something else']);
103
104
        $prev = Util::findPreviousHead($io);
105
106
        $this->assertEquals('a1a1a1a1^', $prev);
107
    }
108
}
109