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

add changes, i know not a good commit #5

Open
wants to merge 1 commit 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
69 changes: 65 additions & 4 deletions src/Market.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,79 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class Market
{
/**
* @var PriceList
*/
private $milkPriceList;

public function __construct(PriceListBuilder $priceListBuilder)
{
$this->milkPriceList = $priceListBuilder->milkPrices();
}

public function priceFor(Good $good): Pound
{
return new Pound(5);
if ($good->isMilk()) {
return $this->milkPriceList->current();
}

$this->throwInvalidGoodException($good);
}

public function buyTo(Offer $offer)
{
$pound = new Pound(0);

$pound->calc($offer, $this->priceFor($offer->good()));

$this->increasePrice($offer->good());

return $pound;
}

private function reducePrice(Good $good): void
{
if ($good->isMilk()) {
$this->milkPriceList->down();

return;
}

$this->throwInvalidGoodException($good);
}

private function increasePrice(Good $good): void
{
if ($good->isMilk()) {
$this->milkPriceList->up();

return;
}

$this->throwInvalidGoodException($good);
}

public function sellTo(Offer $offer): Pound
{
return new Pound(
$offer->amount()->amount() *
$this->priceFor($offer->good())->amount()
$pound = new Pound(0);

$pound->calc($offer, $this->priceFor($offer->good()));

$this->reducePrice($offer->good());

return $pound;
}

private function throwInvalidGoodException(Good $good)
{
throw new UnknownGoodException(
sprintf(
'Good with name: "%s" is unkown.',
get_class($good)
)
);
}
}
5 changes: 3 additions & 2 deletions src/Offer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class Offer
Expand All @@ -15,8 +16,8 @@ final class Offer

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

public function amount(): Quantity
Expand Down
6 changes: 6 additions & 0 deletions src/Pound.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class Pound
Expand All @@ -17,4 +18,9 @@ public function amount(): int
{
return $this->amount;
}

public function calc(Offer $offer, Pound $courrentPrice): void
{
$this->amount += $courrentPrice->amount() * $offer->amount()->amount();
}
}
15 changes: 15 additions & 0 deletions src/PriceList.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class PriceList
Expand Down Expand Up @@ -30,4 +31,18 @@ public function current(): Pound
{
return $this->prices[$this->position];
}

public function up()
{
if (\count($this->prices) !== $this->position + 1) {
$this->position++;
}
}

public function down()
{
if (-1 !== $this->position - 1) {
$this->position--;
}
}
}
6 changes: 6 additions & 0 deletions src/UnknownGoodException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php declare(strict_types=1);
namespace ClansOfCaledonia;

final class UnknownGoodException extends \InvalidArgumentException implements Exception
{
}
50 changes: 44 additions & 6 deletions tests/MarketTest.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

use PHPUnit\Framework\TestCase;

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

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

public function testMilkCanBeSoldToTheMarket(): Market
{
$market = new Market;
$market = new Market(new PriceListBuilder());
$this->assertEquals(new Pound(5), $market->priceFor(Good::milk()));

$payment = $market->sellTo(
new Offer(
Expand All @@ -41,6 +46,39 @@ public function testMilkCanBeSoldToTheMarket(): Market
*/
public function testSellingMilkToTheMarketReducesMilkPrice(Market $market): void
{
//simulate a second sell because the current price is 5 and equels the default price
$market->sellTo(
new Offer(
new Quantity(2),
Good::milk()
)
);

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

public function testBuyGoods(): Market
{
$market = new Market(new PriceListBuilder());
$this->assertEquals(new Pound(5), $market->priceFor(Good::milk()));

$pound = $market->buyTo(
new Offer(
new Quantity(2),
Good::milk()
)
);

$this->assertEquals(new Pound(10), $pound);

return $market;
}

/**
* @depends testBuyGoods
*/
public function testBuyGoodsIncreasePrice(Market $market)
{
$this->assertEquals(new Pound(6), $market->priceFor(Good::milk()));
}
}
13 changes: 11 additions & 2 deletions tests/OfferTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

use PHPUnit\Framework\TestCase;

/**
* @covers \ClansOfCaledonia\Offer
*
* @uses \ClansOfCaledonia\Good
* @uses \ClansOfCaledonia\Quantity
* @uses \ClansOfCaledonia\Good
* @uses \ClansOfCaledonia\Quantity
*/
final class OfferTest extends TestCase
{
Expand All @@ -17,4 +18,12 @@ public function testHasAmount(): void

$this->assertEquals(new Quantity(1), $offer->amount());
}

public function testHasGood(): void
{
$offer = new Offer(new Quantity(1), Good::milk());

$this->assertEquals(new Milk(), $offer->good());
}

}
21 changes: 21 additions & 0 deletions tests/PoundTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

use PHPUnit\Framework\TestCase;

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

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

public function testCalcIncreaseThePoundAndThePrice(): void
{
$pound = new Pound(100);

$pound->calc(
new Offer(
new Quantity(2),
Good::milk()
),
new Pound(2)
);

$this->assertSame(104, $pound->amount());
}
}
56 changes: 54 additions & 2 deletions tests/PriceListTest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

use PHPUnit\Framework\TestCase;

/**
* @covers \ClansOfCaledonia\PriceList
*
* @uses \ClansOfCaledonia\Pound
* @uses \ClansOfCaledonia\Pound
*/
final class PriceListTest extends TestCase
{
public function testHasInitialPrice(): void
public function testHasInitialPrice(): PriceList
{
$prices = PriceList::fromList(
new Pound(1),
Expand All @@ -26,5 +27,56 @@ public function testHasInitialPrice(): void
);

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

return $prices;
}

/**
* @depends testHasInitialPrice
*/
public function testCountUp(PriceList $prices): PriceList
{
$prices->up();

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

return $prices;
}

/**
* @depends testCountUp
*/
public function testCountUpAndRunsNotOutOfIndex(PriceList $prices): PriceList
{
for ($x = 0; $x < 10; $x++) {
$prices->up();
}

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

return $prices;
}

/**
* @depends testCountUpAndRunsNotOutOfIndex
*/
public function testCountDown(PriceList $prices): PriceList
{
$prices->down();

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

return $prices;
}
/**
* @depends testCountUpAndRunsNotOutOfIndex
*/
public function testCountDownRunsNotOutOfIndex(PriceList $prices): void
{
for ($x = 0; $x < 20; $x++) {
$prices->down();
}

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