-
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.
Merge pull request #2 from arosiek/feature/3-introduce-facade
[#3] - Introduce Facade entry point and tests refactoring
- Loading branch information
Showing
13 changed files
with
304 additions
and
51 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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
version: '3.8' | ||
|
||
services: | ||
php: | ||
build: | ||
|
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,13 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace App\App; | ||
|
||
use App\Model\BasketFactory; | ||
|
||
interface CartFacade | ||
{ | ||
public function add(string $code): void; | ||
|
||
public function total(): float; | ||
} |
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,33 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace App\App; | ||
|
||
use App\Model\Basket; | ||
use App\Model\BasketFactory; | ||
use App\Model\RegularBasketFactory; | ||
|
||
class SimpleCartFacade implements CartFacade | ||
{ | ||
private Basket $regularBasket; | ||
|
||
public function __construct() | ||
{ | ||
$this->regularBasket = $this->createBasketFactory()->createRegularBasket(); | ||
} | ||
|
||
public function add(string $code): void | ||
{ | ||
$this->regularBasket->add($code); | ||
} | ||
|
||
public function total(): float | ||
{ | ||
return $this->regularBasket->total(); | ||
} | ||
|
||
private function createBasketFactory(): BasketFactory | ||
{ | ||
return new RegularBasketFactory(); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -36,4 +36,4 @@ public function total(): float | |
|
||
return round($total, 2); | ||
} | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace App\Model; | ||
|
||
use App\Repository\JsonProductRepository; | ||
use App\Repository\ProductRepository; | ||
|
||
class RegularBasketFactory implements BasketFactory | ||
{ | ||
public function createRegularBasket(): Basket | ||
{ | ||
return new RegularBasket( | ||
$this->createProductCatalogueFilterable(), | ||
$this->createShippingCostCalculator(), | ||
$this->createDiscountOfferCalculator(), | ||
); | ||
} | ||
|
||
private function createProductCatalogueFilterable(): ProductCatalogueFilterable | ||
{ | ||
return new ProductCatalogue( | ||
$this->createProductRepository(), | ||
); | ||
} | ||
|
||
private function createProductRepository(): ProductRepository | ||
{ | ||
return new JsonProductRepository(); | ||
} | ||
|
||
private function createShippingCostCalculator(): ShippingCostCalculator | ||
{ | ||
return new BaseShippingCostCalculator(); | ||
} | ||
|
||
private function createDiscountOfferCalculator(): DiscountOfferCalculator | ||
{ | ||
return new BaseDiscountOfferCalculator(); | ||
} | ||
} |
13 changes: 7 additions & 6 deletions
13
...s/Integration/Model/RegularBasketTest.php → .../Integration/App/SimpleCartFacadeTest.php
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,57 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Unit\Model; | ||
|
||
use App\Entity\Product; | ||
use App\Model\BaseDiscountOfferCalculator; | ||
use App\Model\DiscountOfferCalculator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class BaseDiscountOfferCalculatorTest extends TestCase | ||
{ | ||
private DiscountOfferCalculator $sut; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->sut = new BaseDiscountOfferCalculator(); | ||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* @param array<Product> $productList | ||
*/ | ||
#[DataProvider('totalDataProvider')] | ||
public function testShippingCostStrategiesProvidesCorrectValue(array $productList, float $expectedDiscount): void | ||
{ | ||
$actualShippingCosts = $this->sut->calculate($productList); | ||
|
||
$this->assertEquals($expectedDiscount, $actualShippingCosts); | ||
} | ||
|
||
/** | ||
* @return array<string, array<string,array<Product>|float>> | ||
*/ | ||
public static function totalDataProvider(): array | ||
{ | ||
$aRedProduct = new Product(); | ||
$aRedProduct->setCode('R01'); | ||
$aRedProduct->setName('RedOne'); | ||
$aRedProduct->setPrice(10.0); | ||
|
||
|
||
$aGreenProduct = new Product(); | ||
$aGreenProduct->setCode('G01'); | ||
$aGreenProduct->setName('GreenOne'); | ||
$aGreenProduct->setPrice(100.0); | ||
|
||
return [ | ||
'Two red products gives discount' => ['productList' => [$aRedProduct, $aRedProduct], 'expectedDiscount' => 5.0], | ||
'Three red products gives discount once' => ['productList' => [$aRedProduct, $aRedProduct, $aRedProduct], 'expectedDiscount' => 5.0], | ||
'Four red products gives discount once' => ['productList' => [$aRedProduct, $aRedProduct, $aRedProduct, $aRedProduct], 'expectedDiscount' => 10.0], | ||
'Two green products dont gives discount' => ['productList' => [$aGreenProduct, $aGreenProduct], 'expectedDiscount' => .0], | ||
'Mixed products dont gives discount' => ['productList' => [$aRedProduct, $aGreenProduct], 'expectedDiscount' => .0], | ||
]; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Unit\Model; | ||
|
||
use App\Model\BaseShippingCostCalculator; | ||
use App\Model\ShippingCostCalculator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class BaseShippingCostCalculatorTest extends TestCase | ||
{ | ||
private ShippingCostCalculator $sut; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->sut = new BaseShippingCostCalculator(); | ||
parent::setUp(); | ||
} | ||
|
||
#[DataProvider('totalDataProvider')] | ||
public function testShippingCostStrategiesProvidesCorrectValue(float $total, float $expectedShippingCost): void | ||
{ | ||
$actualShippingCosts = $this->sut->calculate($total); | ||
|
||
$this->assertEquals($expectedShippingCost, $actualShippingCosts); | ||
} | ||
|
||
/** | ||
* @return array<string, array<string,float>> | ||
*/ | ||
public static function totalDataProvider(): array | ||
{ | ||
return [ | ||
'Low shipping cost strategy is calculated' => ['total' => 10.0, 'expectedShippingCost' => 4.95], | ||
'Medium shipping cost strategy is calculated on boundary value' => ['total' => 50.0, 'expectedShippingCost' => 2.95], | ||
'Medium shipping cost strategy is calculated' => ['total' => 51.0, 'expectedShippingCost' => 2.95], | ||
'High shipping cost strategy is calculated on boundary value' => ['total' => 90.0, 'expectedShippingCost' => .0], | ||
'High shipping cost strategy is calculated' => ['total' => 91.0, 'expectedShippingCost' => .0], | ||
]; | ||
} | ||
} |
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
Oops, something went wrong.