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\TestCase; |
||
15 | use RuntimeException; |
||
16 | use CaptainHook\App\Console\IO\Mockery; |
||
0 ignored issues
–
show
|
|||
17 | |||
18 | class UtilTest extends TestCase |
||
19 | { |
||
20 | use Mockery; |
||
21 | |||
22 | /** |
||
23 | * Tests Util::isValid |
||
24 | */ |
||
25 | public function testIsValid(): void |
||
26 | { |
||
27 | $this->assertTrue(Util::isValid('pre-commit')); |
||
28 | $this->assertTrue(Util::isValid('pre-push')); |
||
29 | $this->assertTrue(Util::isValid('commit-msg')); |
||
30 | $this->assertFalse(Util::isValid('foo')); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Tests Util::isInstallable |
||
35 | */ |
||
36 | public function testIsInstallable(): void |
||
37 | { |
||
38 | $this->assertTrue(Util::isInstallable('pre-commit')); |
||
39 | $this->assertTrue(Util::isInstallable('pre-push')); |
||
40 | $this->assertTrue(Util::isValid('post-change')); |
||
41 | $this->assertFalse(Util::isInstallable('post-change')); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Tests Util::getValidHooks |
||
46 | */ |
||
47 | public function testGetValidHooks(): void |
||
48 | { |
||
49 | $this->assertArrayHasKey('pre-commit', Util::getValidHooks()); |
||
50 | $this->assertArrayHasKey('pre-push', Util::getValidHooks()); |
||
51 | $this->assertArrayHasKey('commit-msg', Util::getValidHooks()); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Tests Util::getHookCommand |
||
56 | * |
||
57 | * @dataProvider providerValidCommands |
||
58 | * |
||
59 | * @param string $class |
||
60 | * @param string $hook |
||
61 | */ |
||
62 | public function testGetHookCommandValid(string $class, string $hook): void |
||
63 | { |
||
64 | $this->assertEquals($class, Util::getHookCommand($hook)); |
||
65 | $this->assertEquals('PreCommit', Util::getHookCommand('pre-commit')); |
||
66 | $this->assertEquals('PrepareCommitMsg', Util::getHookCommand('prepare-commit-msg')); |
||
67 | $this->assertEquals('PrePush', Util::getHookCommand('pre-push')); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @return array |
||
72 | */ |
||
73 | public function providerValidCommands(): array |
||
74 | { |
||
75 | return [ |
||
76 | ['CommitMsg', 'commit-msg'], |
||
77 | ['PreCommit', 'pre-commit'], |
||
78 | ['PrepareCommitMsg', 'prepare-commit-msg'], |
||
79 | ['PrePush', 'pre-push'], |
||
80 | ]; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Tests Util::getHookCommand |
||
85 | * |
||
86 | * @dataProvider providerInvalidCommands |
||
87 | */ |
||
88 | public function testGetHookCommandInvalid(string $hook): void |
||
89 | { |
||
90 | $this->expectException(RuntimeException::class); |
||
91 | |||
92 | $this->assertEquals('', Util::getHookCommand($hook)); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return array |
||
97 | */ |
||
98 | public function providerInvalidCommands(): array |
||
99 | { |
||
100 | return [ |
||
101 | [''], |
||
102 | ['foo'], |
||
103 | ]; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Tests Util::getHooks |
||
108 | */ |
||
109 | public function testGetHooks(): void |
||
110 | { |
||
111 | $this->assertContains('pre-commit', Util::getHooks()); |
||
112 | $this->assertContains('pre-push', Util::getHooks()); |
||
113 | $this->assertContains('commit-msg', Util::getHooks()); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Tests Util::findPreviousHead |
||
118 | */ |
||
119 | public function testFindPreviousHeadFallback(): void |
||
120 | { |
||
121 | $io = $this->createIOMock(); |
||
122 | $io->method('getStandardInput')->willReturn([]); |
||
123 | $io->method('getArgument')->willReturn('HEAD@{1}'); |
||
124 | |||
125 | $prev = Util::findPreviousHead($io); |
||
126 | |||
127 | $this->assertEquals('HEAD@{1}', $prev); |
||
128 | } |
||
129 | /** |
||
130 | * Tests Util::findPreviousHead |
||
131 | */ |
||
132 | public function testFindPreviousHeadFromStdIn(): void |
||
133 | { |
||
134 | $io = $this->createIOMock(); |
||
135 | $io->method('getStandardInput')->willReturn(['foo a1a1a1a1 Something', 'bar b2b2b2b2 Something else']); |
||
136 | |||
137 | $prev = Util::findPreviousHead($io); |
||
138 | |||
139 | $this->assertEquals('a1a1a1a1^', $prev); |
||
140 | } |
||
141 | } |
||
142 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: