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

[#3] - Introduce Facade entry point and tests refactoring #2

Merged
merged 1 commit into from
Jul 26, 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
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Sample cart project

## sample usage

```
<?php

$basket = (new BasketFactory())->createRegularBasket();
$basket->add('G01');
$basket->add('R01');
$basket->add('R01');
$basket->add('R01');
$simpleCart = new SimpleCartFacade();
$simpleCart->add('G01');
$simpleCart->add('R01');
$simpleCart->add('R01');
$simpleCart->add('R01');

$total = $basket->total();
$total = $simpleCart->total();
```

## assumptions:
Expand All @@ -30,15 +31,15 @@
## instalation

1. `docker-compose up -d`
2. inside container `composer install`
2. inside container `composer install`
or simply `docker exec -it php_sample_cart bash -c "composer install"`

## run tests:

- unit
- `docker exec -it php_sample_cart bash -c "vendor/bin/phpunit --configuration phpunit --testsuite 'Unit Tests'"`
- `docker exec -it php_sample_cart bash -c "vendor/bin/phpunit --configuration phpunit.xml --testsuite 'Unit Tests'"`
- integration
- `docker exec -it php_sample_cart bash -c "vendor/bin/phpunit --configuration phpunit --testsuite 'Integration Tests'"`
- `docker exec -it php_sample_cart bash -c "vendor/bin/phpunit --configuration phpunit.xml --testsuite 'Integration Tests'"`
- unit + integration:
- `docker exec -it php_sample_cart bash -c "composer test"`
- static tests (`phpstan`)
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

services:
php:
build:
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
bootstrap="vendor/autoload.php"
colors="true"
Expand Down
13 changes: 13 additions & 0 deletions src/App/CartFacade.php
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;
}
33 changes: 33 additions & 0 deletions src/App/SimpleCartFacade.php
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();
}
}
35 changes: 3 additions & 32 deletions src/Model/BasketFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,7 @@
use App\Repository\JsonProductRepository;
use App\Repository\ProductRepository;

class BasketFactory
interface 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();
}
}
public function createRegularBasket(): Basket;
}
2 changes: 1 addition & 1 deletion src/Model/RegularBasket.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ public function total(): float

return round($total, 2);
}
}
}
41 changes: 41 additions & 0 deletions src/Model/RegularBasketFactory.php
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?php
declare(strict_types=1);

namespace App\Unit\Tests\Model;
namespace App\Unit\Tests\Itegration\App;

use App\Model\Basket;
use App\Model\BasketFactory;
use App\App\CartFacade;
use App\App\SimpleCartFacade;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class RegularBasketTest extends TestCase
class SimpleCartFacadeTest extends TestCase
{
private Basket $sut;

private CartFacade $sut;

protected function setUp(): void
{
$this->sut = (new BasketFactory())->createRegularBasket();
$this->sut = new SimpleCartFacade();

parent::setUp();
}
Expand Down
57 changes: 57 additions & 0 deletions tests/Unit/Model/BaseDiscountOfferCalculatorTest.php
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],
];
}
}
42 changes: 42 additions & 0 deletions tests/Unit/Model/BaseShippingCostCalculatorTest.php
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],
];
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Model/ProductCatalogueTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace App\Tests\Model;
namespace App\Tests\Unit\Model;

use App\Entity\Product;
use App\Exception\ProductNotFoundException;
Expand Down
Loading
Loading