Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pipeline #17

Merged
merged 8 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ root = true

[*]
charset = utf-8
indent_size = 2
indent_size = 4
indent_style = space
end_of_line = lf
trim_trailing_whitespace = true
Expand Down
23 changes: 9 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Laravel Pipeline with DB transaction support, events and additional methods.

The package requires `PHP 8` or higher and `Laravel 9` or higher.
The package requires `PHP 8.1` or higher and `Laravel 10` or higher.

---

Expand Down Expand Up @@ -49,14 +49,7 @@ Pipeline::make()
});
```

You can use the `pipeline` helper:
```php
pipeline($data, [
// pipes
])->thenReturn();
```

You can as well instantiate the pipeline using IoC or manually:
You can as well instantiate the pipeline using the service container or manually:
```php
app(Pipeline::class)
...
Expand All @@ -69,12 +62,14 @@ app(Pipeline::class)
...
```

You can use the `run` helper to execute a single pipeline-compatible action:
You can use the `run` method to execute a single pipe:
```php
run(MyAction::class, $data);
$pipeline = Pipeline::make();

$pipeline->run(Pipe::class, $data);
```

By default, `run` uses the `handle` method in your action classes, but if you use a different method name in your pipelines, you can fix that by adding code to your ServiceProvider:
By default, `run` uses the `handle` method in your class as an entry point, but if you use a different method name in your pipelines, you can fix that by adding code to your ServiceProvider:
```php
$this->app->resolving(Pipeline::class, function ($pipeline) {
return $pipeline->via('execute');
Expand All @@ -93,8 +88,8 @@ Usage of `withTransaction` method will enable a [`manual DB transaction`](https:
Usage of `withEvents` method will enable [`Laravel Events`](https://laravel.com/docs/9.x/events#introduction) throughout the pipeline execution.

#### Available events
- [`PipeStarted`](https://github.com/michael-rubel/laravel-enhanced-pipeline/blob/main/src/Events/PipeStarted.php) - fired **before** execution of pipe;
- [`PipePassed`](https://github.com/michael-rubel/laravel-enhanced-pipeline/blob/main/src/Events/PipePassed.php) - fired **after** execution of pipe.
- [`PipeExecutionStarted`](https://github.com/michael-rubel/laravel-enhanced-pipeline/blob/main/src/Events/PipeExecutionStarted.php) - fired **before** execution of the pipe;
- [`PipeExecutionFinished`](https://github.com/michael-rubel/laravel-enhanced-pipeline/blob/main/src/Events/PipeExecutionFinished.php) - fired **after** execution of the pipe.

## Testing
```bash
Expand Down
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
"autoload": {
"psr-4": {
"MichaelRubel\\EnhancedPipeline\\": "src"
},
"files": [
"src/Helpers/helpers.php"
]
}
},
"autoload-dev": {
"psr-4": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MichaelRubel\EnhancedPipeline\Events;

class PipeStarted
class PipeExecutionFinished
{
/**
* @param mixed $pipe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MichaelRubel\EnhancedPipeline\Events;

class PipePassed
class PipeExecutionStarted
{
/**
* @param mixed $pipe
Expand Down
40 changes: 0 additions & 40 deletions src/Helpers/helpers.php

This file was deleted.

26 changes: 19 additions & 7 deletions src/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
use Illuminate\Container\Container as ContainerConcrete;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract;
use MichaelRubel\EnhancedPipeline\Events\PipePassed;
use MichaelRubel\EnhancedPipeline\Events\PipeStarted;
use Illuminate\Support\Traits\Conditionable;
use MichaelRubel\EnhancedPipeline\Events\PipeExecutionFinished;
use MichaelRubel\EnhancedPipeline\Events\PipeExecutionStarted;
use MichaelRubel\EnhancedPipeline\Traits\HasDatabaseTransactions;
use MichaelRubel\EnhancedPipeline\Traits\HasEvents;
use RuntimeException;
use Throwable;

class Pipeline implements PipelineContract
{
use HasDatabaseTransactions, HasEvents;
use Conditionable, HasDatabaseTransactions, HasEvents;

/**
* The container implementation.
Expand Down Expand Up @@ -73,7 +74,7 @@ public static function make(?Container $container = null): Pipeline
$container = ContainerConcrete::getInstance();
}

return new Pipeline($container);
return $container->make(static::class);
}

/**
Expand Down Expand Up @@ -189,15 +190,15 @@ protected function carry()
{
return function ($stack, $pipe) {
return function ($passable) use ($stack, $pipe) {
$this->fireEvent(PipeStarted::class, $pipe, $passable);
$this->fireEvent(PipeExecutionStarted::class, $pipe, $passable);

if (is_callable($pipe)) {
// If the pipe is a callable, then we will call it directly, but otherwise we
// will resolve the pipes out of the dependency container and call it with
// the appropriate method and arguments, returning the results back out.
$result = $pipe($passable, $stack);

$this->fireEvent(PipePassed::class, $pipe, $passable);
$this->fireEvent(PipeExecutionFinished::class, $pipe, $passable);

return $result;
} elseif (! is_object($pipe)) {
Expand All @@ -220,7 +221,7 @@ protected function carry()
? $pipe->{$this->method}(...$parameters)
: $pipe(...$parameters);

$this->fireEvent(PipePassed::class, $pipe, $passable);
$this->fireEvent(PipeExecutionFinished::class, $pipe, $passable);

return $this->handleCarry($carry);
};
Expand Down Expand Up @@ -302,6 +303,17 @@ public function onFailure(Closure $callback)
return $this;
}

/**
* Run a single pipe.
*/
public function run(string $pipe, mixed $data = true): mixed
{
return $this
->send($data)
->through([$pipe])
->thenReturn();
}

/**
* Handle the value returned from each pipe before passing it to the next.
*
Expand Down
30 changes: 30 additions & 0 deletions tests/OriginalPipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,36 @@ public function testPipelineThenReturnMethodRunsPipelineThenReturnsPassable()

unset($_SERVER['__test.pipe.one']);
}

public function testPipelineConditionable()
{
$result = (new Pipeline(new Container))
->send('foo')
->when(true, function (Pipeline $pipeline) {
$pipeline->pipe([PipelineTestPipeOne::class]);
})
->then(function ($piped) {
return $piped;
});

$this->assertSame('foo', $result);
$this->assertSame('foo', $_SERVER['__test.pipe.one']);
unset($_SERVER['__test.pipe.one']);

$_SERVER['__test.pipe.one'] = null;
$result = (new Pipeline(new Container))
->send('foo')
->when(false, function (Pipeline $pipeline) {
$pipeline->pipe([PipelineTestPipeOne::class]);
})
->then(function ($piped) {
return $piped;
});

$this->assertSame('foo', $result);
$this->assertNull($_SERVER['__test.pipe.one']);
unset($_SERVER['__test.pipe.one']);
}
}

class PipelineTestPipeOne
Expand Down
12 changes: 6 additions & 6 deletions tests/PipelineEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Illuminate\Support\Facades\Event;
use MichaelRubel\EnhancedPipeline\EnhancedPipelineServiceProvider;
use MichaelRubel\EnhancedPipeline\Events\PipePassed;
use MichaelRubel\EnhancedPipeline\Events\PipeStarted;
use MichaelRubel\EnhancedPipeline\Events\PipeExecutionFinished;
use MichaelRubel\EnhancedPipeline\Events\PipeExecutionStarted;
use MichaelRubel\EnhancedPipeline\Pipeline;

class PipelineEventsTest extends TestCase
Expand Down Expand Up @@ -41,7 +41,7 @@ public function testFiresPipeStartedEvents()
])
->thenReturn();

Event::assertDispatched(function (PipeStarted $event) {
Event::assertDispatched(function (PipeExecutionStarted $event) {
$this->assertInstanceOf(TestPipe::class, app($event->pipe));
$this->assertSame('data', $event->passable);

Expand All @@ -59,14 +59,14 @@ public function testFiresPipeStartedEventsButFailsToPass()
->onFailure(fn () => true)
->thenReturn();

Event::assertDispatched(function (PipeStarted $event) {
Event::assertDispatched(function (PipeExecutionStarted $event) {
$this->assertInstanceOf(PipelineWithException::class, app($event->pipe));
$this->assertSame('data', $event->passable);

return true;
});

Event::assertNotDispatched(PipePassed::class);
Event::assertNotDispatched(PipeExecutionFinished::class);
}

/** @test */
Expand All @@ -81,7 +81,7 @@ public function testFiresPipePassedEvents()
])
->thenReturn();

Event::assertDispatched(function (PipePassed $event) {
Event::assertDispatched(function (PipeExecutionFinished $event) {
$this->assertInstanceOf(TestPipe::class, app($event->pipe));
$this->assertSame('data', $event->passable);

Expand Down
23 changes: 16 additions & 7 deletions tests/PipelineRunHelperTest.php → tests/PipelineRunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,40 @@

use MichaelRubel\EnhancedPipeline\Pipeline;

class PipelineRunHelperTest extends TestCase
class PipelineRunTest extends TestCase
{
public function testRunHelperWithoutParams()
public function testRunWithoutParams()
{
$executed = run(Action::class);
$executed = Pipeline::make()->run(Action::class);

$this->assertTrue($executed);
}

public function testRunHelperActionReturnsPassedData()
public function testRunReturnsPassedData()
{
$data = ['test' => 'yeah'];

$executed = run(Action::class, with($data));
$executed = Pipeline::make()->run(Action::class, with($data));

$this->assertSame('yeah', $executed['test']);
}

public function testRunHelperHasCustomizableMethod()
public function testRunHasCustomizableMethod()
{
$executed = Pipeline::make()
->via('execute')
->run(ActionExecute::class);

$this->assertTrue($executed);
}

public function testRunHasCustomizableMethodViaContainer()
{
$this->app->resolving(Pipeline::class, function ($pipeline) {
return $pipeline->via('execute');
});

$executed = run(ActionExecute::class);
$executed = Pipeline::make()->run(ActionExecute::class);

$this->assertTrue($executed);
}
Expand Down
23 changes: 0 additions & 23 deletions tests/PipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,6 @@ public function rollsTheDatabaseTransactionBackOnFailureWhenOnFailureMethodUsed(
$database->shouldHaveReceived('rollBack')->once();
}

/** @test */
public function testPipelineHelper()
{
$test = pipeline('test', fn ($data, $next) => $next($data))
->thenReturn();

$this->assertSame('test', $test);
}

/** @test */
public function testPipelineHelperWithoutParameters()
{
$test = pipeline()
->send('data')
->through(TestPipe::class)
->thenReturn();

$this->assertSame('data', $test);
}

/** @test */
public function testCanOverrideOriginalPipeline()
{
Expand All @@ -185,9 +165,6 @@ public function testCanOverrideEnhancedPipeline()

$pipeline = app(Pipeline::class);
$this->assertInstanceOf(OriginalPipeline::class, $pipeline);

$pipeline = pipeline();
$this->assertInstanceOf(OriginalPipeline::class, $pipeline);
}
}

Expand Down
Loading