Skip to content

Commit cc588b9

Browse files
authored
Added Macroable trait to Hyperf\Pipeline\Pipeline. (#7492)
1 parent 096d9a9 commit cc588b9

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"require": {
1919
"php": ">=8.1",
20+
"hyperf/macroable": "~3.1.0",
2021
"psr/container": "^1.0 || ^2.0"
2122
},
2223
"autoload": {

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
@@ -211,6 +211,58 @@ public function testHandleCarry()
211211
$this->assertSame($id + 6, $result);
212212
}
213213

214+
public function testPipelineMacro()
215+
{
216+
Pipeline::macro('customMethod', function ($value) {
217+
return 'custom_' . $value;
218+
});
219+
220+
$pipeline = new Pipeline($this->getContainer());
221+
$this->assertTrue($pipeline->hasMacro('customMethod'));
222+
$this->assertSame('custom_test', $pipeline->customMethod('test'));
223+
}
224+
225+
public function testPipelineMacroWithThis()
226+
{
227+
Pipeline::macro('getPipes', function () {
228+
return $this->pipes;
229+
});
230+
231+
$pipeline = new Pipeline($this->getContainer());
232+
$pipeline->through(['pipe1', 'pipe2']);
233+
234+
$this->assertEquals(['pipe1', 'pipe2'], $pipeline->getPipes());
235+
}
236+
237+
public function testPipelineHasMacro()
238+
{
239+
Pipeline::macro('existingMacro', function () {
240+
return 'exists';
241+
});
242+
243+
$pipeline = new Pipeline($this->getContainer());
244+
245+
$this->assertTrue($pipeline->hasMacro('existingMacro'));
246+
$this->assertFalse($pipeline->hasMacro('nonExistingMacro'));
247+
}
248+
249+
public function testPipelineMacroOverwrite()
250+
{
251+
Pipeline::macro('testMacro', function () {
252+
return 'first';
253+
});
254+
255+
$pipeline = new Pipeline($this->getContainer());
256+
$this->assertSame('first', $pipeline->testMacro());
257+
258+
Pipeline::macro('testMacro', function () {
259+
return 'second';
260+
});
261+
262+
$pipeline2 = new Pipeline($this->getContainer());
263+
$this->assertSame('second', $pipeline2->testMacro());
264+
}
265+
214266
protected function getContainer()
215267
{
216268
$container = Mockery::mock(ContainerInterface::class);

0 commit comments

Comments
 (0)