generated from descom-es/php-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
201 additions
and
23 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
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,20 @@ | ||
<?php | ||
|
||
namespace Descom\Pipeline; | ||
|
||
class PipeLineOptions | ||
{ | ||
private array $options = []; | ||
|
||
public function __construct(array $options) | ||
{ | ||
foreach ($options as $key => $value) { | ||
$this->options[$key] = $value; | ||
} | ||
} | ||
|
||
public function get(string $key) | ||
{ | ||
return $this->options[$key] ?? null; | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
namespace Descom\Pipeline\Test; | ||
|
||
use Descom\Pipeline\Tests\Support\ArgumentPipeline; | ||
use Descom\Pipeline\Tests\Support\ArgumentPipelineSample; | ||
use Descom\Pipeline\Tests\Support\ArgumentStage; | ||
use Descom\Pipeline\Tests\Support\DoubleArgumentStage; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ArgumentTest extends TestCase | ||
{ | ||
public function testPipelineWithArguments() | ||
{ | ||
ArgumentPipeline::getInstance()->pipe(new ArgumentStage()); | ||
|
||
$this->assertEquals( | ||
9.35, | ||
ArgumentPipeline::getInstance() | ||
->process(10, [ | ||
'discountPercent' => 15, | ||
'taxPercent' => 10, | ||
]) | ||
); | ||
|
||
$this->assertEquals( | ||
9.2, | ||
ArgumentPipeline::getInstance() | ||
->process(10, [ | ||
'discountPercent' => 20, | ||
'taxPercent' => 15, | ||
]) | ||
); | ||
} | ||
|
||
public function testPipelineWithArgumentsSample() | ||
{ | ||
ArgumentPipelineSample::getInstance()->pipe(new DoubleArgumentStage()); | ||
|
||
$this->assertEquals( | ||
80, | ||
ArgumentPipelineSample::getInstance() | ||
->process(10, [ | ||
'twiceDouble' => 3 | ||
]) | ||
); | ||
|
||
$this->assertEquals( | ||
20, | ||
ArgumentPipelineSample::getInstance() | ||
->process(10) | ||
); | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace Descom\Pipeline\Tests\Support; | ||
|
||
use Descom\Pipeline\PipeLine; | ||
|
||
class ArgumentPipeline extends PipeLine | ||
{ | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace Descom\Pipeline\Tests\Support; | ||
|
||
use Descom\Pipeline\PipeLine; | ||
|
||
class ArgumentPipelineSample extends PipeLine | ||
{ | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace Descom\Pipeline\Tests\Support; | ||
|
||
use Descom\Pipeline\Stage; | ||
|
||
class ArgumentStage extends Stage | ||
{ | ||
public function handle($payload) | ||
{ | ||
$priceWithDiscount = $this->applyDiscount($payload); | ||
|
||
return round($this->applyTax($priceWithDiscount), 2); | ||
} | ||
|
||
private function applyDiscount($price) | ||
{ | ||
return $price * (1 - $this->option('discountPercent') / 100); | ||
} | ||
|
||
private function applyTax($price) | ||
{ | ||
return $price * (1 + $this->option('taxPercent') / 100); | ||
} | ||
} |
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 Descom\Pipeline\Tests\Support; | ||
|
||
use Descom\Pipeline\Stage; | ||
|
||
class DoubleArgumentStage extends Stage | ||
{ | ||
public function handle($payload): int | ||
{ | ||
$twiceDouble = $this->option('twiceDouble') ?? 1; | ||
|
||
return $payload * pow(2, $twiceDouble); | ||
} | ||
} |
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