From e87c3ff61b6d159480900e19570a7cb6052e3b8d Mon Sep 17 00:00:00 2001 From: Hans-Helge Buerger Date: Mon, 3 Jun 2019 14:38:44 +0200 Subject: [PATCH 1/8] IDEAga .ideaga .idea --- .idea/php.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.idea/php.xml b/.idea/php.xml index f6159c8..8b29ba5 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -1,4 +1,9 @@ + + + + + \ No newline at end of file From a4323fa6a9cd9acd31082bf8c0ecfef9ab11b253 Mon Sep 17 00:00:00 2001 From: Hans-Helge Buerger Date: Mon, 3 Jun 2019 14:39:29 +0200 Subject: [PATCH 2/8] Add multiply method for pound --- src/Pound.php | 5 +++++ tests/PoundTest.php | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Pound.php b/src/Pound.php index 1be124e..7131dc3 100644 --- a/src/Pound.php +++ b/src/Pound.php @@ -17,4 +17,9 @@ public function amount(): int { return $this->amount; } + + public function multiply(Unit $unit): Pound + { + return new Pound($this->amount * $unit->amount()); + } } diff --git a/tests/PoundTest.php b/tests/PoundTest.php index 3d3ec6d..362b09e 100644 --- a/tests/PoundTest.php +++ b/tests/PoundTest.php @@ -16,4 +16,25 @@ public function testHasAmount(): void $this->assertSame($amount, $p->amount()); } + + + /** + * @dataProvider multiplyDataProvider + */ + public function testPoundCanMultiply(int $amount, Unit $factor, int $expected) + { + $p = new Pound($amount); + + $this->assertSame($expected, $p->multiply($factor)->amount()); + } + + + public function multiplyDataProvider() + { + return [ + [ 5, new Unit(2), 10], + [ 1, new Unit(100), 100], + [ -3, new Unit(4), -12], + ]; + } } From fc97df4c1adfa9b5072cc7fae5c90c53a57356d0 Mon Sep 17 00:00:00 2001 From: Hans-Helge Buerger Date: Mon, 3 Jun 2019 15:09:53 +0200 Subject: [PATCH 3/8] Increase Stock --- src/Market.php | 18 +++++++++++++----- src/PriceList.php | 6 ++++++ src/PriceListBuilder.php | 2 +- tests/PriceListTest.php | 19 ++++++++++++++++++- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/Market.php b/src/Market.php index 3d43053..c589550 100644 --- a/src/Market.php +++ b/src/Market.php @@ -3,16 +3,24 @@ final class Market { + /** @var PriceList[] */ + private $priceLists = []; + + public function __construct() + { + $this->priceLists['milk'] = PriceListBuilder::milkPrices(); + } + + public function priceFor(Good $good): Pound { - return new Pound(5); + return $this->priceLists['milk']->current(); } public function sellTo(Offer $offer): Pound { - return new Pound( - $offer->amount()->amount() * - $this->priceFor($offer->good())->amount() - ); + $profit = $this->priceLists['milk']->current()->multiply($offer->amount()); + $this->priceLists['milk']->increaseStock($offer->amount()); + return $profit; } } diff --git a/src/PriceList.php b/src/PriceList.php index 8a6a231..dc020f3 100644 --- a/src/PriceList.php +++ b/src/PriceList.php @@ -30,4 +30,10 @@ public function current(): Pound { return $this->prices[$this->position]; } + + + public function increaseStock(Unit $unit): void + { + $this->position = max($this->position - $unit->amount(), 0); + } } diff --git a/src/PriceListBuilder.php b/src/PriceListBuilder.php index bc0da06..7d33360 100644 --- a/src/PriceListBuilder.php +++ b/src/PriceListBuilder.php @@ -3,7 +3,7 @@ final class PriceListBuilder { - public function milkPrices(): PriceList + public static function milkPrices(): PriceList { return PriceList::fromList( new Pound(3), diff --git a/tests/PriceListTest.php b/tests/PriceListTest.php index aba7abb..1f3d1ec 100644 --- a/tests/PriceListTest.php +++ b/tests/PriceListTest.php @@ -10,7 +10,7 @@ */ final class PriceListTest extends TestCase { - public function testHasInitialPrice(): void + public function testHasInitialPrice(): PriceList { $prices = PriceList::fromList( new Pound(1), @@ -26,5 +26,22 @@ public function testHasInitialPrice(): void ); $this->assertEquals(new Pound(4), $prices->current()); + + return $prices; + } + + + /** + * @depends testHasInitialPrice + */ + public function testInreaseStock(PriceList $prices) + { + $this->assertEquals(new Pound(4), $prices->current()); + $prices->increaseStock(new Unit(1)); + $this->assertEquals(new Pound(3), $prices->current()); + $prices->increaseStock(new Unit(2)); + $this->assertEquals(new Pound(1), $prices->current()); + $prices->increaseStock(new Unit(1)); + $this->assertEquals(new Pound(1), $prices->current()); } } From 7c344aff22c36fb7dd984da05d6d536975788747 Mon Sep 17 00:00:00 2001 From: Hans-Helge Buerger Date: Mon, 3 Jun 2019 15:40:58 +0200 Subject: [PATCH 4/8] Implement decreaesPosition of priceList --- src/PriceList.php | 6 ++++++ tests/PriceListTest.php | 43 +++++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/PriceList.php b/src/PriceList.php index dc020f3..fd28f46 100644 --- a/src/PriceList.php +++ b/src/PriceList.php @@ -36,4 +36,10 @@ public function increaseStock(Unit $unit): void { $this->position = max($this->position - $unit->amount(), 0); } + + + public function decreaseStock(Unit $unit): void + { + $this->position = min($this->position + $unit->amount(), count($this->prices)-1); + } } diff --git a/tests/PriceListTest.php b/tests/PriceListTest.php index 1f3d1ec..d7865ba 100644 --- a/tests/PriceListTest.php +++ b/tests/PriceListTest.php @@ -10,7 +10,7 @@ */ final class PriceListTest extends TestCase { - public function testHasInitialPrice(): PriceList + public function testHasInitialPrice(): void { $prices = PriceList::fromList( new Pound(1), @@ -26,16 +26,11 @@ public function testHasInitialPrice(): PriceList ); $this->assertEquals(new Pound(4), $prices->current()); - - return $prices; } - - /** - * @depends testHasInitialPrice - */ - public function testInreaseStock(PriceList $prices) + public function testInreaseStock() { + $prices = $this->priceList(); $this->assertEquals(new Pound(4), $prices->current()); $prices->increaseStock(new Unit(1)); $this->assertEquals(new Pound(3), $prices->current()); @@ -44,4 +39,36 @@ public function testInreaseStock(PriceList $prices) $prices->increaseStock(new Unit(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 Unit(1)); + $this->assertEquals(new Pound(5), $prices->current()); + $prices->decreaseStock(new Unit(2)); + $this->assertEquals(new Pound(7), $prices->current()); + $prices->decreaseStock(new Unit(1)); + $this->assertEquals(new Pound(8), $prices->current()); + $prices->decreaseStock(new Unit(2)); + $this->assertEquals(new Pound(10), $prices->current()); + $prices->decreaseStock(new Unit(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) + ); + } } From 61f3ed27540aa47e1b2abdc7f8b00889b8c1fbfa Mon Sep 17 00:00:00 2001 From: Hans-Helge Buerger Date: Mon, 3 Jun 2019 15:50:38 +0200 Subject: [PATCH 5/8] Implement Wool --- src/Good.php | 11 +++++++++++ src/Milk.php | 1 + src/Wool.php | 12 ++++++++++++ src/autoload.php | 3 ++- tests/GoodTest.php | 10 ++++++++++ tests/PriceListTest.php | 1 + 6 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/Wool.php diff --git a/src/Good.php b/src/Good.php index 2e540c6..f0b6968 100644 --- a/src/Good.php +++ b/src/Good.php @@ -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; + } } diff --git a/src/Milk.php b/src/Milk.php index 8689abe..7714b98 100644 --- a/src/Milk.php +++ b/src/Milk.php @@ -1,4 +1,5 @@ '/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); diff --git a/tests/GoodTest.php b/tests/GoodTest.php index f44ff47..69c2842 100644 --- a/tests/GoodTest.php +++ b/tests/GoodTest.php @@ -14,5 +14,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()); } } diff --git a/tests/PriceListTest.php b/tests/PriceListTest.php index d7865ba..213c468 100644 --- a/tests/PriceListTest.php +++ b/tests/PriceListTest.php @@ -1,4 +1,5 @@ Date: Mon, 3 Jun 2019 16:10:09 +0200 Subject: [PATCH 6/8] Fix rebase issues with quantity rename stuff, which is really annoying! --- src/Offer.php | 4 ++-- src/Pound.php | 4 ++-- src/PriceList.php | 8 ++++---- src/PriceListBuilder.php | 16 ++++++++++++++++ tests/MarketTest.php | 41 ++++++++++++++++++++++++++++++++++++++-- tests/PoundTest.php | 8 ++++---- tests/PriceListTest.php | 16 ++++++++-------- tests/QuantityTest.php | 4 ++-- 8 files changed, 77 insertions(+), 24 deletions(-) diff --git a/src/Offer.php b/src/Offer.php index 431103b..974a024 100644 --- a/src/Offer.php +++ b/src/Offer.php @@ -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; } diff --git a/src/Pound.php b/src/Pound.php index 7131dc3..9313c3f 100644 --- a/src/Pound.php +++ b/src/Pound.php @@ -18,8 +18,8 @@ public function amount(): int return $this->amount; } - public function multiply(Unit $unit): Pound + public function multiply(Quantity $quantity): Pound { - return new Pound($this->amount * $unit->amount()); + return new Pound($this->amount * $quantity->amount()); } } diff --git a/src/PriceList.php b/src/PriceList.php index fd28f46..3dd91c0 100644 --- a/src/PriceList.php +++ b/src/PriceList.php @@ -32,14 +32,14 @@ public function current(): Pound } - public function increaseStock(Unit $unit): void + public function increaseStock(Quantity $quantity): void { - $this->position = max($this->position - $unit->amount(), 0); + $this->position = max($this->position - $quantity->amount(), 0); } - public function decreaseStock(Unit $unit): void + public function decreaseStock(Quantity $quantity): void { - $this->position = min($this->position + $unit->amount(), count($this->prices)-1); + $this->position = min($this->position + $quantity->amount(), count($this->prices)-1); } } diff --git a/src/PriceListBuilder.php b/src/PriceListBuilder.php index 7d33360..01bfca5 100644 --- a/src/PriceListBuilder.php +++ b/src/PriceListBuilder.php @@ -18,4 +18,20 @@ public static function milkPrices(): PriceList new Pound(8) ); } + + public static function woolPrices(): PriceList + { + return PriceList::fromList( + new Pound(1), + new Pound(2), + new Pound(3), + new Pound(3), + new Pound(4), + new Pound(5), + new Pound(6), + new Pound(7), + new Pound(33), + new Pound(99) + ); + } } diff --git a/tests/MarketTest.php b/tests/MarketTest.php index 466685d..e519537 100644 --- a/tests/MarketTest.php +++ b/tests/MarketTest.php @@ -13,11 +13,15 @@ */ 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 @@ -43,4 +47,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(3), $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], + 'initialWoolCosts3Pound' => [Good::wool(), 3], + ]; + } } diff --git a/tests/PoundTest.php b/tests/PoundTest.php index 362b09e..322f3bc 100644 --- a/tests/PoundTest.php +++ b/tests/PoundTest.php @@ -21,7 +21,7 @@ public function testHasAmount(): void /** * @dataProvider multiplyDataProvider */ - public function testPoundCanMultiply(int $amount, Unit $factor, int $expected) + public function testPoundCanMultiply(int $amount, Quantity $factor, int $expected) { $p = new Pound($amount); @@ -32,9 +32,9 @@ public function testPoundCanMultiply(int $amount, Unit $factor, int $expected) public function multiplyDataProvider() { return [ - [ 5, new Unit(2), 10], - [ 1, new Unit(100), 100], - [ -3, new Unit(4), -12], + [ 5, new Quantity(2), 10], + [ 1, new Quantity(100), 100], + [ -3, new Quantity(4), -12], ]; } } diff --git a/tests/PriceListTest.php b/tests/PriceListTest.php index 213c468..9b8c481 100644 --- a/tests/PriceListTest.php +++ b/tests/PriceListTest.php @@ -33,11 +33,11 @@ public function testInreaseStock() { $prices = $this->priceList(); $this->assertEquals(new Pound(4), $prices->current()); - $prices->increaseStock(new Unit(1)); + $prices->increaseStock(new Quantity(1)); $this->assertEquals(new Pound(3), $prices->current()); - $prices->increaseStock(new Unit(2)); + $prices->increaseStock(new Quantity(2)); $this->assertEquals(new Pound(1), $prices->current()); - $prices->increaseStock(new Unit(1)); + $prices->increaseStock(new Quantity(1)); $this->assertEquals(new Pound(1), $prices->current()); } @@ -45,15 +45,15 @@ public function testDecreaseStock(): void { $prices = $this->priceList(); $this->assertEquals(new Pound(4), $prices->current()); - $prices->decreaseStock(new Unit(1)); + $prices->decreaseStock(new Quantity(1)); $this->assertEquals(new Pound(5), $prices->current()); - $prices->decreaseStock(new Unit(2)); + $prices->decreaseStock(new Quantity(2)); $this->assertEquals(new Pound(7), $prices->current()); - $prices->decreaseStock(new Unit(1)); + $prices->decreaseStock(new Quantity(1)); $this->assertEquals(new Pound(8), $prices->current()); - $prices->decreaseStock(new Unit(2)); + $prices->decreaseStock(new Quantity(2)); $this->assertEquals(new Pound(10), $prices->current()); - $prices->decreaseStock(new Unit(1)); + $prices->decreaseStock(new Quantity(1)); $this->assertEquals(new Pound(10), $prices->current()); } diff --git a/tests/QuantityTest.php b/tests/QuantityTest.php index a705965..a9bf0c1 100644 --- a/tests/QuantityTest.php +++ b/tests/QuantityTest.php @@ -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()); } /** From 479a5516ca5d35a02c7444d7b06f85c539335dc5 Mon Sep 17 00:00:00 2001 From: Hans-Helge Buerger Date: Mon, 3 Jun 2019 16:14:43 +0200 Subject: [PATCH 7/8] Implement Wool in Market --- src/Market.php | 9 +++++---- src/PriceListBuilder.php | 8 ++++---- tests/MarketTest.php | 4 ++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Market.php b/src/Market.php index c589550..2de72d9 100644 --- a/src/Market.php +++ b/src/Market.php @@ -8,19 +8,20 @@ final class Market public function __construct() { - $this->priceLists['milk'] = PriceListBuilder::milkPrices(); + $this->priceLists[Milk::class] = PriceListBuilder::milkPrices(); + $this->priceLists[Wool::class] = PriceListBuilder::woolPrices(); } public function priceFor(Good $good): Pound { - return $this->priceLists['milk']->current(); + return $this->priceLists[get_class($good)]->current(); } public function sellTo(Offer $offer): Pound { - $profit = $this->priceLists['milk']->current()->multiply($offer->amount()); - $this->priceLists['milk']->increaseStock($offer->amount()); + $profit = $this->priceLists[get_class($offer->good())]->current()->multiply($offer->amount()); + $this->priceLists[get_class($offer->good())]->increaseStock($offer->amount()); return $profit; } } diff --git a/src/PriceListBuilder.php b/src/PriceListBuilder.php index 01bfca5..b458c99 100644 --- a/src/PriceListBuilder.php +++ b/src/PriceListBuilder.php @@ -22,16 +22,16 @@ public static function milkPrices(): PriceList public static function woolPrices(): PriceList { return PriceList::fromList( - new Pound(1), - new Pound(2), 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(33), - new Pound(99) + new Pound(8 ) ); } } diff --git a/tests/MarketTest.php b/tests/MarketTest.php index e519537..c136708 100644 --- a/tests/MarketTest.php +++ b/tests/MarketTest.php @@ -59,7 +59,7 @@ public function testWoolCanBeSoldToTheMarket(): Market ) ); - $this->assertEquals(new Pound(3), $payment); + $this->assertEquals(new Pound(4), $payment); return $market; } @@ -77,7 +77,7 @@ public function initialGoodProvider() { return [ 'initialMilkCosts5Pound' => [Good::milk(), 5], - 'initialWoolCosts3Pound' => [Good::wool(), 3], + 'initialWoolCosts4Pound' => [Good::wool(), 4], ]; } } From a281a591ffb6f060aad3d92363d20ae76bac5324 Mon Sep 17 00:00:00 2001 From: Hans-Helge Buerger Date: Mon, 3 Jun 2019 16:34:47 +0200 Subject: [PATCH 8/8] Improve coverage statements --- tests/GoodTest.php | 1 + tests/MarketTest.php | 2 ++ tests/PoundTest.php | 2 ++ tests/PriceListTest.php | 1 + 4 files changed, 6 insertions(+) diff --git a/tests/GoodTest.php b/tests/GoodTest.php index 69c2842..0b777fd 100644 --- a/tests/GoodTest.php +++ b/tests/GoodTest.php @@ -6,6 +6,7 @@ /** * @covers \ClansOfCaledonia\Good * @covers \ClansOfCaledonia\Milk + * @covers \ClansOfCaledonia\Wool */ final class GoodTest extends TestCase { diff --git a/tests/MarketTest.php b/tests/MarketTest.php index c136708..4332121 100644 --- a/tests/MarketTest.php +++ b/tests/MarketTest.php @@ -5,11 +5,13 @@ /** * @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 { diff --git a/tests/PoundTest.php b/tests/PoundTest.php index 322f3bc..af87e4b 100644 --- a/tests/PoundTest.php +++ b/tests/PoundTest.php @@ -5,6 +5,8 @@ /** * @covers \ClansOfCaledonia\Pound + * + * @uses \ClansOfCaledonia\Quantity */ final class PoundTest extends TestCase { diff --git a/tests/PriceListTest.php b/tests/PriceListTest.php index 9b8c481..01b2d8a 100644 --- a/tests/PriceListTest.php +++ b/tests/PriceListTest.php @@ -8,6 +8,7 @@ * @covers \ClansOfCaledonia\PriceList * * @uses \ClansOfCaledonia\Pound + * @uses \ClansOfCaledonia\Quantity */ final class PriceListTest extends TestCase {