Skip to content

Commit f939ef2

Browse files
committed
HooksPipeline tests
1 parent 05bac37 commit f939ef2

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

tests/Datasets/PipelineHooks.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Igorsgm\GitHooks\Tests\Fixtures\HooksPipelineDefaultFixture1;
4+
use Igorsgm\GitHooks\Tests\Fixtures\HooksPipelineInvokableFixture3;
5+
use Igorsgm\GitHooks\Tests\Fixtures\HooksPipelineWithParamsFixture2;
6+
7+
dataset('pipelineHooks', [
8+
'Default Hook' => [
9+
'hook' => HooksPipelineDefaultFixture1::class,
10+
'parameters' => null,
11+
],
12+
'Hook with Params' => [
13+
'hook' => HooksPipelineWithParamsFixture2::class,
14+
'parameters' => [
15+
'param' => 'Hook 2',
16+
],
17+
],
18+
'Invokable Hook Class' => [
19+
'hook' => HooksPipelineInvokableFixture3::class,
20+
'parameters' => null,
21+
],
22+
]);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Tests\Fixtures;
4+
5+
use Igorsgm\GitHooks\Contracts\Hook;
6+
7+
class HooksPipelineDefaultFixture1 implements Hook
8+
{
9+
/**
10+
* Get hook name
11+
*
12+
* @return string
13+
*/
14+
public function getName(): string
15+
{
16+
return 'Hook 1';
17+
}
18+
19+
public function handle(string $message, $next)
20+
{
21+
$message .= ' '.$this->getName();
22+
23+
return $next($message);
24+
}
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Tests\Fixtures;
4+
5+
class HooksPipelineInvokableFixture3
6+
{
7+
/**
8+
* Get hook name
9+
*
10+
* @return string
11+
*/
12+
public function getName(): string
13+
{
14+
return 'Hook 3';
15+
}
16+
17+
public function __invoke($message, $next)
18+
{
19+
$message .= ' Hook 3';
20+
21+
return $next($message);
22+
}
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Tests\Fixtures;
4+
5+
use Igorsgm\GitHooks\Contracts\Hook;
6+
7+
class HooksPipelineWithParamsFixture2 implements Hook
8+
{
9+
/**
10+
* @var array
11+
*/
12+
private $parameters;
13+
14+
public function __construct(array $parameters)
15+
{
16+
$this->parameters = $parameters;
17+
}
18+
19+
/**
20+
* Get hook name
21+
*
22+
* @return string
23+
*/
24+
public function getName(): string
25+
{
26+
return 'Hook 2';
27+
}
28+
29+
public function handle(string $message, $next)
30+
{
31+
$message .= ' '.$this->parameters['param'];
32+
33+
return $next($message);
34+
}
35+
}

tests/Unit/HooksPipelineTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Igorsgm\GitHooks\HooksPipeline;
4+
use Illuminate\Container\Container;
5+
6+
test('Data is sent through Pipes', function ($hook, ?array $parameters = null) {
7+
$parameters = $parameters ?? [];
8+
$container = new Container();
9+
10+
$hookConfig = ! empty($parameters) ? [$hook => $parameters] : [$hook];
11+
$this->config->set('git-hooks.pre-commit', $hookConfig);
12+
13+
$pipeline = new HooksPipeline($container, 'pre-commit');
14+
15+
$message = $pipeline->through($hook)
16+
->send('message')
17+
->thenReturn();
18+
19+
$hookName = resolve($hook, compact('parameters'))->getName();
20+
$this->assertEquals('message '.$hookName, $message);
21+
})->with('pipelineHooks');
22+
23+
test('Data is sent through Pipes with Closure', function () {
24+
$container = new Container();
25+
26+
$closureHook = function ($message, $next) {
27+
$message .= ' Hook 4';
28+
29+
return $next($message);
30+
};
31+
$this->config->set('git-hooks.pre-commit', [
32+
$closureHook,
33+
]);
34+
35+
$pipeline = new HooksPipeline($container, 'pre-commit');
36+
37+
$message = $pipeline->through($closureHook)
38+
->send('message')
39+
->thenReturn();
40+
41+
$this->assertEquals('message Hook 4', $message);
42+
});

0 commit comments

Comments
 (0)