Skip to content
This repository has been archived by the owner on Jun 3, 2019. It is now read-only.

Feature/our awesome work #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/Good.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@ public static function milk(): self
return new Milk;
}

public static function wool(): self
{
return new Wool;
}

public function isMilk(): bool
{
return false;
}


public function isWool(): bool
{
return false;
}
}
19 changes: 14 additions & 5 deletions src/Market.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@

final class Market
{
/** @var PriceList[] */
private $priceLists = [];

public function __construct()
{
$this->priceLists[Milk::class] = PriceListBuilder::milkPrices();
$this->priceLists[Wool::class] = PriceListBuilder::woolPrices();
}


public function priceFor(Good $good): Pound
{
return new Pound(5);
return $this->priceLists[get_class($good)]->current();
}

public function sellTo(Offer $offer): Pound
{
return new Pound(
$offer->amount()->amount() *
$this->priceFor($offer->good())->amount()
);
$profit = $this->priceLists[get_class($offer->good())]->current()->multiply($offer->amount());
$this->priceLists[get_class($offer->good())]->increaseStock($offer->amount());
return $profit;
}
}
1 change: 1 addition & 0 deletions src/Milk.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class Milk extends Good
Expand Down
4 changes: 2 additions & 2 deletions src/Offer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ final class Offer
*/
private $good;

public function __construct(Quantity $unit, Good $good)
public function __construct(Quantity $quantity, Good $good)
{
$this->amount = $unit;
$this->amount = $quantity;
$this->good = $good;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Pound.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public function amount(): int
{
return $this->amount;
}

public function multiply(Quantity $quantity): Pound
{
return new Pound($this->amount * $quantity->amount());
}
}
12 changes: 12 additions & 0 deletions src/PriceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ public function current(): Pound
{
return $this->prices[$this->position];
}


public function increaseStock(Quantity $quantity): void
{
$this->position = max($this->position - $quantity->amount(), 0);
}


public function decreaseStock(Quantity $quantity): void
{
$this->position = min($this->position + $quantity->amount(), count($this->prices)-1);
}
}
18 changes: 17 additions & 1 deletion src/PriceListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

final class PriceListBuilder
{
public function milkPrices(): PriceList
public static function milkPrices(): PriceList
{
return PriceList::fromList(
new Pound(3),
Expand All @@ -18,4 +18,20 @@ public function milkPrices(): PriceList
new Pound(8)
);
}

public static function woolPrices(): PriceList
{
return PriceList::fromList(
new Pound(3),
new Pound(3),
new Pound(4),
new Pound(4),
new Pound(5),
new Pound(5),
new Pound(6),
new Pound(6),
new Pound(7),
new Pound(8 )
);
}
}
12 changes: 12 additions & 0 deletions src/Wool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace ClansOfCaledonia;

class Wool extends Good
{

public function isWool(): bool
{
return true;
}
}
3 changes: 2 additions & 1 deletion src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function($class) {
'clansofcaledonia\\pound' => '/Pound.php',
'clansofcaledonia\\pricelist' => '/PriceList.php',
'clansofcaledonia\\pricelistbuilder' => '/PriceListBuilder.php',
'clansofcaledonia\\quantity' => '/Quantity.php'
'clansofcaledonia\\quantity' => '/Quantity.php',
'clansofcaledonia\\wool' => '/Wool.php'
);
}
$cn = strtolower($class);
Expand Down
11 changes: 11 additions & 0 deletions tests/GoodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/**
* @covers \ClansOfCaledonia\Good
* @covers \ClansOfCaledonia\Milk
* @covers \ClansOfCaledonia\Wool
*/
final class GoodTest extends TestCase
{
Expand All @@ -14,5 +15,15 @@ public function testCanBeMilk(): void
$milk = Good::milk();

$this->assertTrue($milk->isMilk());
$this->assertFalse($milk->isWool());
}


public function testCanBeWool(): void
{
$wool = Good::wool();

$this->assertTrue($wool->isWool());
$this->assertFalse($wool->isMilk());
}
}
43 changes: 41 additions & 2 deletions tests/MarketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@

/**
* @covers \ClansOfCaledonia\Market
* @covers \ClansOfCaledonia\PriceListBuilder
*
* @uses \ClansOfCaledonia\Pound
* @uses \ClansOfCaledonia\Good
* @uses \ClansOfCaledonia\Offer
* @uses \ClansOfCaledonia\Quantity
* @uses \ClansOfCaledonia\PriceList
*/
final class MarketTest extends TestCase
{
public function testMilkCosts5PoundsInitially(): void

/**
* @dataProvider initialGoodProvider
*/
public function testInitiallyGoodCost(Good $good, int $amount): void
{
$market = new Market;

$this->assertEquals(new Pound(5), $market->priceFor(Good::milk()));
$this->assertEquals(new Pound($amount), $market->priceFor($good));
}

public function testMilkCanBeSoldToTheMarket(): Market
Expand All @@ -43,4 +49,37 @@ public function testSellingMilkToTheMarketReducesMilkPrice(Market $market): void
{
$this->assertEquals(new Pound(4), $market->priceFor(Good::milk()));
}

public function testWoolCanBeSoldToTheMarket(): Market
{
$market = new Market;

$payment = $market->sellTo(
new Offer(
new Quantity(1),
Good::wool()
)
);

$this->assertEquals(new Pound(4), $payment);

return $market;
}

/**
* @depends testWoolCanBeSoldToTheMarket
*/
public function testSellingWoolToTheMarketReducesWoolPrice(Market $market): void
{
$this->assertEquals(new Pound(4), $market->priceFor(Good::wool()));
}


public function initialGoodProvider()
{
return [
'initialMilkCosts5Pound' => [Good::milk(), 5],
'initialWoolCosts4Pound' => [Good::wool(), 4],
];
}
}
23 changes: 23 additions & 0 deletions tests/PoundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

/**
* @covers \ClansOfCaledonia\Pound
*
* @uses \ClansOfCaledonia\Quantity
*/
final class PoundTest extends TestCase
{
Expand All @@ -16,4 +18,25 @@ public function testHasAmount(): void

$this->assertSame($amount, $p->amount());
}


/**
* @dataProvider multiplyDataProvider
*/
public function testPoundCanMultiply(int $amount, Quantity $factor, int $expected)
{
$p = new Pound($amount);

$this->assertSame($expected, $p->multiply($factor)->amount());
}


public function multiplyDataProvider()
{
return [
[ 5, new Quantity(2), 10],
[ 1, new Quantity(100), 100],
[ -3, new Quantity(4), -12],
];
}
}
46 changes: 46 additions & 0 deletions tests/PriceListTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

use PHPUnit\Framework\TestCase;
Expand All @@ -7,6 +8,7 @@
* @covers \ClansOfCaledonia\PriceList
*
* @uses \ClansOfCaledonia\Pound
* @uses \ClansOfCaledonia\Quantity
*/
final class PriceListTest extends TestCase
{
Expand All @@ -27,4 +29,48 @@ public function testHasInitialPrice(): void

$this->assertEquals(new Pound(4), $prices->current());
}

public function testInreaseStock()
{
$prices = $this->priceList();
$this->assertEquals(new Pound(4), $prices->current());
$prices->increaseStock(new Quantity(1));
$this->assertEquals(new Pound(3), $prices->current());
$prices->increaseStock(new Quantity(2));
$this->assertEquals(new Pound(1), $prices->current());
$prices->increaseStock(new Quantity(1));
$this->assertEquals(new Pound(1), $prices->current());
}

public function testDecreaseStock(): void
{
$prices = $this->priceList();
$this->assertEquals(new Pound(4), $prices->current());
$prices->decreaseStock(new Quantity(1));
$this->assertEquals(new Pound(5), $prices->current());
$prices->decreaseStock(new Quantity(2));
$this->assertEquals(new Pound(7), $prices->current());
$prices->decreaseStock(new Quantity(1));
$this->assertEquals(new Pound(8), $prices->current());
$prices->decreaseStock(new Quantity(2));
$this->assertEquals(new Pound(10), $prices->current());
$prices->decreaseStock(new Quantity(1));
$this->assertEquals(new Pound(10), $prices->current());
}

public function priceList(): PriceList
{
return PriceList::fromList(
new Pound(1),
new Pound(2),
new Pound(3),
new Pound(4),
new Pound(5),
new Pound(6),
new Pound(7),
new Pound(8),
new Pound(9),
new Pound(10)
);
}
}
4 changes: 2 additions & 2 deletions tests/QuantityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ final class QuantityTest extends TestCase
{
public function testHasAmount(): void
{
$unit = new Quantity(1);
$quantity = new Quantity(1);

$this->assertSame(1, $unit->amount());
$this->assertSame(1, $quantity->amount());
}

/**
Expand Down