PHPUnit 6.2.4 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 136 ms, Memory: 14.00MB
OK (3 tests, 3 assertions)
-
-
Save gemmadlou/7480003c4d9d2812bd6ccca45039e790 to your computer and use it in GitHub Desktop.
Understanding Laravel pipelines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Features; | |
use App\Features\FirstTask; | |
use App\Features\SecondTask; | |
use Illuminate\Pipeline\Pipeline; | |
// *Naming things is hard* ... So, this is a class called `ProcessClass` that `run()` some text ¯\_(ツ)_/¯ | |
class ProcessClass | |
{ | |
protected $pipes; | |
public function __construct() | |
{ | |
$this->pipes = $this->loadPipes(); | |
} | |
public function loadPipes() | |
{ | |
return [ | |
FirstTask::class, | |
SecondTask::class | |
]; | |
} | |
public function run($text) | |
{ | |
return app(Pipeline::class) | |
->send($text) | |
->through($this->pipes) | |
->then(function ($text) { | |
return $text . "finally"; | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Features; | |
use Closure; | |
class FirstTask | |
{ | |
/** | |
* Changes the given text and return the result | |
**/ | |
public function handle($text, Closure $next) | |
{ | |
return $next($text . ' [first task] '); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Features; | |
use Closure; | |
class SecondTask | |
{ | |
/** | |
* Changes the given text and return the result | |
**/ | |
public function handle($text, Closure $next) | |
{ | |
return $next($text . ' [second task] '); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Feature; | |
use Tests\TestCase; | |
use App\Features\ProcessClass; | |
class PipelineTest extends TestCase | |
{ | |
public function testExample() | |
{ | |
$text = 'Initial test'; | |
$object = new ProcessClass($text); | |
$result = $object->run($text); | |
$this->assertEquals('Initial test [first task] [second task] finally', $result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment