generated from michael-rubel/laravel-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from michael-rubel/feature/events
Events
- Loading branch information
Showing
7 changed files
with
184 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace MichaelRubel\EnhancedPipeline\Events; | ||
|
||
class PipePassed | ||
{ | ||
/** | ||
* @param mixed $pipe | ||
* @param mixed $passable | ||
*/ | ||
public function __construct(public $pipe, public $passable) | ||
{ | ||
// | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace MichaelRubel\EnhancedPipeline\Events; | ||
|
||
class PipeStarted | ||
{ | ||
/** | ||
* @param mixed $pipe | ||
* @param mixed $passable | ||
*/ | ||
public function __construct(public $pipe, public $passable) | ||
{ | ||
// | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MichaelRubel\EnhancedPipeline\Traits; | ||
|
||
trait HasEvents | ||
{ | ||
/** | ||
* Determines whether pipeline uses events. | ||
* | ||
* @var bool | ||
*/ | ||
protected bool $useEvents = false; | ||
|
||
/** | ||
* Enable events in pipeline. | ||
* | ||
* @return static | ||
*/ | ||
public function withEvents(): static | ||
{ | ||
$this->useEvents = true; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Fire the event if enabled. | ||
* | ||
* @param string $event | ||
* @param string|callable|mixed $pipe | ||
* @param mixed $passable | ||
* | ||
* @return void | ||
*/ | ||
protected function fireEvent(string $event, $pipe, $passable): void | ||
{ | ||
if (! $this->useEvents) { | ||
return; | ||
} | ||
|
||
if (is_object($pipe)) { | ||
/** @var object $pipe */ | ||
$pipe = $pipe::class; | ||
} | ||
|
||
event(new $event($pipe, $passable)); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace MichaelRubel\EnhancedPipeline\Tests; | ||
|
||
use Illuminate\Support\Facades\Event; | ||
use MichaelRubel\EnhancedPipeline\Events\PipePassed; | ||
use MichaelRubel\EnhancedPipeline\Events\PipeStarted; | ||
use MichaelRubel\EnhancedPipeline\Pipeline; | ||
|
||
class PipelineEventsTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Event::fake(); | ||
} | ||
|
||
/** @test */ | ||
public function testFiresPipeStartedEvents() | ||
{ | ||
app(Pipeline::class) | ||
->withEvents() | ||
->send('data') | ||
->through([ | ||
TestPipe::class, | ||
TestPipe::class, | ||
]) | ||
->thenReturn(); | ||
|
||
Event::assertDispatched(function (PipeStarted $event) { | ||
$this->assertInstanceOf(TestPipe::class, app($event->pipe)); | ||
$this->assertSame('data', $event->passable); | ||
|
||
return true; | ||
}, 2); | ||
} | ||
|
||
/** @test */ | ||
public function testFiresPipeStartedEventsButFailsToPass() | ||
{ | ||
app(Pipeline::class) | ||
->withEvents() | ||
->send('data') | ||
->through(PipelineWithException::class) | ||
->onFailure(fn () => true) | ||
->thenReturn(); | ||
|
||
Event::assertDispatched(function (PipeStarted $event) { | ||
$this->assertInstanceOf(PipelineWithException::class, app($event->pipe)); | ||
$this->assertSame('data', $event->passable); | ||
|
||
return true; | ||
}); | ||
|
||
Event::assertNotDispatched(PipePassed::class); | ||
} | ||
|
||
/** @test */ | ||
public function testFiresPipePassedEvents() | ||
{ | ||
app(Pipeline::class) | ||
->withEvents() | ||
->send('data') | ||
->through([ | ||
TestPipe::class, | ||
TestPipe::class, | ||
]) | ||
->thenReturn(); | ||
|
||
Event::assertDispatched(function (PipePassed $event) { | ||
$this->assertInstanceOf(TestPipe::class, app($event->pipe)); | ||
$this->assertSame('data', $event->passable); | ||
|
||
return true; | ||
}, 2); | ||
} | ||
} |