Skip to content

Commit be331ac

Browse files
authored
Merge pull request #12 from igorsgm/feature/readding-pest-tests
Pest Tests
2 parents 3374074 + d1a79bc commit be331ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1242
-1253
lines changed

composer.json

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@
2222
],
2323
"require": {
2424
"php": "^7.2|^8.0",
25-
"illuminate/container": "^5.6|^6.0|^7.0|^8.0|^9.0",
26-
"illuminate/config": "^5.6|^6.0|^7.0|^8.0|^9.0",
27-
"illuminate/support": "^5.6|^6.0|^7.0|^8.0|^9.0",
28-
"illuminate/contracts": "^5.6|^6.0|^7.0|^8.0|^9.0",
29-
"illuminate/console": "^5.6|^6.0|^7.0|^8.0|^9.0",
30-
"illuminate/pipeline": "^5.6|^6.0|^7.0|^8.0|^9.0"
25+
"illuminate/config": "^6.0|^7.0|^8.0|^9.0",
26+
"illuminate/console": "^6.0|^7.0|^8.0|^9.0",
27+
"illuminate/container": "^6.0|^7.0|^8.0|^9.0",
28+
"illuminate/contracts": "^6.0|^7.0|^8.0|^9.0",
29+
"illuminate/pipeline": "^6.0|^7.0|^8.0|^9.0",
30+
"illuminate/support": "^6.0|^7.0|^8.0|^9.0"
3131
},
3232
"require-dev": {
33-
"orchestra/testbench": "^6.0",
34-
"phpunit/phpunit": "^9.0",
35-
"mockery/mockery": "^1.4.4",
36-
"laravel/pint": "^1.2"
33+
"laravel/pint": "^1.2",
34+
"mockery/mockery": "^1.5.1",
35+
"orchestra/testbench": "^7.0",
36+
"pestphp/pest": "^1.22",
37+
"pestphp/pest-plugin-mock": "^1.0",
38+
"phpunit/phpunit": "^9.0"
3739
},
3840
"autoload": {
3941
"psr-4": {
@@ -50,15 +52,18 @@
5052
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
5153
},
5254
"config": {
53-
"sort-packages": true
55+
"sort-packages": true,
56+
"allow-plugins": {
57+
"pestphp/pest-plugin": true
58+
}
5459
},
5560
"extra": {
5661
"laravel": {
5762
"providers": [
5863
"Igorsgm\\GitHooks\\GitHooksServiceProvider"
5964
],
6065
"aliases": {
61-
"GitHooks": "Igorsgm\\GitHooks\\GitHooksFacade"
66+
"GitHooks": "Igorsgm\\GitHooks\\Facades\\GitHooks"
6267
}
6368
}
6469
},

config/git-hooks.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
| Pre-Rebase Hooks
7979
|-----------------------------------------------------------------
8080
|
81+
| @TODO Implement support to pre-rebase hooks
82+
|
8183
| The pre-rebase hook runs before you rebase anything and can halt the process by exiting non-zero.
8284
| You can use this hook to disallow rebasing any commits that have already been pushed.
8385
| The example pre-rebase hook that Git installs does this, although it makes some assumptions that may not
@@ -93,6 +95,8 @@
9395
| Post-Rewrite Hooks
9496
|-----------------------------------------------------------------
9597
|
98+
| @TODO Implement support to post-rewrite hook
99+
|
96100
| The post-rewrite hook is run by commands that replace commits, such as git commit --amend and git rebase
97101
| (though not by git filter-branch). Its single argument is which command triggered the rewrite, and it receives
98102
| a list of rewrites on stdin. This hook has many of the same uses as the post-checkout and post-merge hooks.
@@ -107,6 +111,8 @@
107111
| Post-Checkout Hooks
108112
|-----------------------------------------------------------------
109113
|
114+
| @TODO Implement support to post-checkout hook
115+
|
110116
| After you run a successful git checkout, the post-checkout hook runs; you can use it to set up your working
111117
| directory properly for your project environment. This may mean moving in large binary files that you don't want
112118
| source controlled, auto-generating documentation, or something along those lines.
@@ -121,10 +127,13 @@
121127
| Post-Merge Hooks
122128
|-----------------------------------------------------------------
123129
|
130+
| @TODO Implement support to post-merge hook
131+
|
124132
| The post-merge hook runs after a successful merge command. You can use it to restore data in the working tree
125133
| that Git can't track, such as permissions data. This hook can likewise validate the presence of files external
126134
| to Git control that you may want copied in when the working tree changes.
127135
|
136+
|
128137
*/
129138
'post-merge' => [
130139

phpunit.xml

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
backupGlobals="false"
4-
backupStaticAttributes="false"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
54
bootstrap="vendor/autoload.php"
65
colors="true"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
10-
processIsolation="false"
11-
stopOnFailure="false"
12-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
13-
<coverage>
14-
<include>
15-
<directory suffix=".php">./src</directory>
16-
</include>
17-
</coverage>
6+
>
187
<testsuites>
19-
<testsuite name="Unit">
8+
<testsuite name="Test Suite">
209
<directory suffix="Test.php">./tests</directory>
2110
</testsuite>
2211
</testsuites>
12+
<coverage processUncoveredFiles="true">
13+
<include>
14+
<directory suffix=".php">./app</directory>
15+
<directory suffix=".php">./src</directory>
16+
</include>
17+
</coverage>
2318
</phpunit>

src/Console/Commands/PreCommit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use Igorsgm\GitHooks\Contracts\HookCommand;
66
use Igorsgm\GitHooks\Exceptions\HookFailException;
7+
use Igorsgm\GitHooks\Facades\GitHooks;
78
use Igorsgm\GitHooks\Git\ChangedFiles;
8-
use Igorsgm\GitHooks\Git\GitHelper;
99
use Igorsgm\GitHooks\Traits\WithPipeline;
1010
use Illuminate\Console\Command;
1111

@@ -45,7 +45,7 @@ public function handle()
4545
try {
4646
$this->sendChangedFilesThroughHooks(
4747
new ChangedFiles(
48-
GitHelper::getListOfChangedFiles()
48+
GitHooks::getListOfChangedFiles()
4949
)
5050
);
5151
} catch (HookFailException $e) {

src/Console/Commands/RegisterHooks.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Igorsgm\GitHooks\Console\Commands;
44

5+
use Exception;
56
use Igorsgm\GitHooks\GitHooks;
67
use Illuminate\Console\Command;
78

@@ -12,22 +13,22 @@ class RegisterHooks extends Command
1213
*
1314
* @var string
1415
*/
15-
protected $signature = 'git-hooks:register-hooks';
16+
protected $signature = 'git-hooks:register';
1617

1718
/**
1819
* The console command description.
1920
*
2021
* @var string
2122
*/
22-
protected $description = 'Register git hooks for application';
23+
protected $description = 'Register or re-register git hooks for application';
2324

2425
/**
2526
* Execute the console command.
2627
*
2728
* @param GitHooks $gitHooks
2829
* @return void
2930
*
30-
* @throws \Exception
31+
* @throws Exception
3132
*/
3233
public function handle(GitHooks $gitHooks)
3334
{
@@ -37,6 +38,6 @@ public function handle(GitHooks $gitHooks)
3738
$gitHooks->install($hook);
3839
}
3940

40-
$this->info('Git hooks have been successfully created');
41+
$this->info('Git hooks have been successfully installed.');
4142
}
4243
}

src/GitHooksFacade.php renamed to src/Facades/GitHooks.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

3-
namespace Igorsgm\GitHooks;
3+
namespace Igorsgm\GitHooks\Facades;
44

55
use Illuminate\Support\Facades\Facade;
66

77
/**
88
* @see \Igorsgm\GitHooks\GitHooks
99
*/
10-
class GitHooksFacade extends Facade
10+
class GitHooks extends Facade
1111
{
1212
/**
1313
* Get the registered name of the component.

src/GitHooks.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace Igorsgm\GitHooks;
44

55
use Exception;
6+
use Igorsgm\GitHooks\Traits\GitHelper;
67

78
class GitHooks
89
{
10+
use GitHelper;
11+
912
/**
1013
* Get all supported git hooks
1114
*/
@@ -44,7 +47,7 @@ public function getAvailableHooks()
4447
*
4548
* @throws Exception
4649
*/
47-
public function install($hookName)
50+
public function install(string $hookName)
4851
{
4952
if (! is_dir($this->getGitHooksDir())) {
5053
throw new Exception('Git not initialized in this project.');

src/HooksPipeline.php

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ class HooksPipeline extends Pipeline
1212
/**
1313
* @var Closure
1414
*/
15-
protected $callback;
15+
protected $pipeStartCallback;
1616

1717
/**
1818
* @var Closure
1919
*/
20-
protected $exceptionCallback;
20+
protected $pipeEndCallback;
2121

2222
/**
2323
* @var string
@@ -38,9 +38,9 @@ public function __construct(Container $container, string $hook)
3838
* @param Closure $callback
3939
* @return $this
4040
*/
41-
public function withCallback(Closure $callback)
41+
public function withPipeStartCallback(Closure $callback)
4242
{
43-
$this->callback = $callback;
43+
$this->pipeStartCallback = $callback;
4444

4545
return $this;
4646
}
@@ -49,9 +49,9 @@ public function withCallback(Closure $callback)
4949
* @param Closure $callback
5050
* @return $this
5151
*/
52-
public function withExceptionCallback(Closure $callback)
52+
public function withPipeEndCallback(Closure $callback)
5353
{
54-
$this->exceptionCallback = $callback;
54+
$this->pipeEndCallback = $callback;
5555

5656
return $this;
5757
}
@@ -79,8 +79,10 @@ protected function carry()
7979
// execute the pipe function giving in the parameters that are required.
8080
$pipe = $this->getContainer()->make($pipe, ['parameters' => $hookParameters]);
8181

82-
if ($this->callback) {
83-
call_user_func_array($this->callback, [$pipe]);
82+
$this->handlePipeEnd(true);
83+
84+
if ($this->pipeStartCallback) {
85+
call_user_func_array($this->pipeStartCallback, [$pipe]);
8486
}
8587

8688
$parameters = [$passable, $stack];
@@ -92,8 +94,8 @@ protected function carry()
9294
}
9395

9496
$carry = method_exists($pipe, $this->method)
95-
? $pipe->{$this->method}(...$parameters)
96-
: $pipe(...$parameters);
97+
? $pipe->{$this->method}(...$parameters)
98+
: $pipe(...$parameters);
9799

98100
return $this->handleCarry($carry);
99101
} catch (Throwable $e) {
@@ -104,13 +106,43 @@ protected function carry()
104106
}
105107

106108
/**
107-
* {@inheritDoc}
109+
* Handle the call back call once a specific pipe has finished or errored.
110+
*
111+
* @param bool $success
112+
* @return void
108113
*/
109-
protected function handleException($passable, $e)
114+
protected function handlePipeEnd($success)
110115
{
111-
if ($this->exceptionCallback) {
112-
return call_user_func_array($this->exceptionCallback, [$e]);
116+
if ($this->pipeEndCallback) {
117+
call_user_func_array($this->pipeEndCallback, [$success]);
113118
}
119+
}
120+
121+
/**
122+
* Handle the value returned from each pipe before passing it to the next.
123+
*
124+
* @param mixed $carry
125+
* @return mixed
126+
*/
127+
protected function handleCarry($carry)
128+
{
129+
$this->handlePipeEnd(true);
130+
131+
return $carry;
132+
}
133+
134+
/**
135+
* Handle the given exception.
136+
*
137+
* @param mixed $passable
138+
* @param \Throwable $e
139+
* @return mixed
140+
*
141+
* @throws \Throwable
142+
*/
143+
protected function handleException($passable, Throwable $e)
144+
{
145+
$this->handlePipeEnd(false);
114146

115147
throw $e;
116148
}

src/Git/GitHelper.php renamed to src/Traits/GitHelper.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
<?php
22

3-
namespace Igorsgm\GitHooks\Git;
3+
namespace Igorsgm\GitHooks\Traits;
44

5-
use Igorsgm\GitHooks\Traits\ProcessHelper;
65
use Symfony\Component\Process\Exception\ProcessFailedException;
76

8-
class GitHelper
7+
trait GitHelper
98
{
109
use ProcessHelper;
1110

1211
/**
1312
* @return string
1413
*/
15-
public static function getListOfChangedFiles()
14+
public function getListOfChangedFiles()
1615
{
17-
return self::execAndGetCommandOutput('git status --short');
16+
return $this->runCommandAndGetOutput('git status --short');
1817
}
1918

2019
/**
2120
* @return string
2221
*/
23-
public static function getLastCommitFromLog()
22+
public function getLastCommitFromLog()
2423
{
25-
return self::execAndGetCommandOutput('git log -1 HEAD');
24+
return $this->runCommandAndGetOutput('git log -1 HEAD');
2625
}
2726

2827
/**
2928
* @param string|array $commands
3029
* @return string
3130
*/
32-
private static function execAndGetCommandOutput($commands)
31+
private function runCommandAndGetOutput($commands)
3332
{
34-
$process = self::execCommands($commands);
33+
$process = $this->runCommands($commands);
3534

3635
if (! $process->isSuccessful()) {
3736
throw new ProcessFailedException($process);
@@ -46,7 +45,7 @@ private static function execAndGetCommandOutput($commands)
4645
* @param string $filePath
4746
* @return string
4847
*/
49-
public static function getCommitMessageContentFromFile(string $filePath): string
48+
public function getCommitMessageContentFromFile(string $filePath): string
5049
{
5150
return file_get_contents($filePath);
5251
}
@@ -57,7 +56,7 @@ public static function getCommitMessageContentFromFile(string $filePath): string
5756
* @param string $path
5857
* @param string $message
5958
*/
60-
public static function updateCommitMessageContentInFile(string $path, string $message): void
59+
public function updateCommitMessageContentInFile(string $path, string $message): void
6160
{
6261
file_put_contents($path, $message);
6362
}

0 commit comments

Comments
 (0)