Skip to content

Commit 9c110c5

Browse files
committed
Merge branch 'master' into 3.2-merge
# Conflicts: # .github/workflows/test.yml # src/collection/tests/ArrTest.php # src/pipeline/composer.json # src/pipeline/tests/PipelineTest.php # src/support/src/Fluent.php # src/support/src/Traits/InteractsWithData.php # src/support/tests/FluentTest.php
2 parents 3cb4214 + cc588b9 commit 9c110c5

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"require": {
1919
"php": ">=8.2",
20+
"hyperf/macroable": "~3.1.0",
2021
"psr/container": "^1.0 || ^2.0"
2122
},
2223
"autoload": {
@@ -37,4 +38,4 @@
3738
"dev-master": "3.1-dev"
3839
}
3940
}
40-
}
41+
}

src/Pipeline.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Hyperf\Pipeline;
1414

1515
use Closure;
16+
use Hyperf\Macroable\Macroable;
1617
use Psr\Container\ContainerInterface;
1718

1819
/**
@@ -21,6 +22,8 @@
2122
*/
2223
class Pipeline
2324
{
25+
use Macroable;
26+
2427
/**
2528
* The object being passed through the pipeline.
2629
*/

tests/PipelineTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,58 @@ function ($std) {
330330
}
331331
}
332332

333+
public function testPipelineMacro()
334+
{
335+
Pipeline::macro('customMethod', function ($value) {
336+
return 'custom_' . $value;
337+
});
338+
339+
$pipeline = new Pipeline($this->getContainer());
340+
$this->assertTrue($pipeline->hasMacro('customMethod'));
341+
$this->assertSame('custom_test', $pipeline->customMethod('test'));
342+
}
343+
344+
public function testPipelineMacroWithThis()
345+
{
346+
Pipeline::macro('getPipes', function () {
347+
return $this->pipes;
348+
});
349+
350+
$pipeline = new Pipeline($this->getContainer());
351+
$pipeline->through(['pipe1', 'pipe2']);
352+
353+
$this->assertEquals(['pipe1', 'pipe2'], $pipeline->getPipes());
354+
}
355+
356+
public function testPipelineHasMacro()
357+
{
358+
Pipeline::macro('existingMacro', function () {
359+
return 'exists';
360+
});
361+
362+
$pipeline = new Pipeline($this->getContainer());
363+
364+
$this->assertTrue($pipeline->hasMacro('existingMacro'));
365+
$this->assertFalse($pipeline->hasMacro('nonExistingMacro'));
366+
}
367+
368+
public function testPipelineMacroOverwrite()
369+
{
370+
Pipeline::macro('testMacro', function () {
371+
return 'first';
372+
});
373+
374+
$pipeline = new Pipeline($this->getContainer());
375+
$this->assertSame('first', $pipeline->testMacro());
376+
377+
Pipeline::macro('testMacro', function () {
378+
return 'second';
379+
});
380+
381+
$pipeline2 = new Pipeline($this->getContainer());
382+
$this->assertSame('second', $pipeline2->testMacro());
383+
}
384+
333385
protected function getContainer()
334386
{
335387
$container = Mockery::mock(ContainerInterface::class);

0 commit comments

Comments
 (0)