Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
namespace PromotionService; | ||
|
||
class Item | ||
{ | ||
public $name; | ||
public $price; | ||
public $tax; | ||
|
||
public function __construct($name, $price, $tax) | ||
{ | ||
$this->name = $name; | ||
$this->price = $price; | ||
$this->tax = $tax; | ||
} | ||
} | ||
|
||
class PromotionService | ||
{ | ||
|
||
public function applyPromotionTo(Item $item) | ||
{ | ||
$result = []; | ||
$result[] = new UserMessage(sprintf("Total before promotion: %.1F", $item->price + $item->price * $item->tax)); | ||
|
||
$item->price -= $this->standardDiscount(); | ||
if ($item->price > 122) { | ||
$item->tax /= 2; | ||
} | ||
|
||
$this->persist($item); | ||
|
||
$result[] = new UserMessage(sprintf("Total after promotion: %.1F", $item->price + $item->price * $item->tax)); | ||
return $result; | ||
} | ||
|
||
// This method can't be moved to another class, used by other code in this class. | ||
private function standardDiscount() | ||
{ | ||
return 2; | ||
} | ||
|
||
private function persist(Item $item) | ||
{ | ||
// Item is persisted to storage. | ||
} | ||
|
||
// ... There is more code in this class. | ||
} |
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,24 @@ | ||
<?php | ||
namespace PromotionService; | ||
|
||
class UserMessage | ||
{ | ||
|
||
private $value; | ||
|
||
public function __construct($value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public function getValue() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
} |
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 | ||
namespace PromotionService; | ||
|
||
class PromotionServiceTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
/** @test */ | ||
public function bookPromotion() | ||
{ | ||
$promotionService = new PromotionService(); | ||
$messages = $promotionService->applyPromotionTo( | ||
new Item("Functional programming with C++", 10, 0.2)); | ||
|
||
assertThat($messages, equalTo([ | ||
new UserMessage("Total before promotion: 12.0"), | ||
new UserMessage("Total after promotion: 9.6"), | ||
])); | ||
} | ||
|
||
/** @test */ | ||
public function expensiveBookPromotion() | ||
{ | ||
$promotionService = new PromotionService(); | ||
$messages = $promotionService->applyPromotionTo( | ||
new Item("Functional programming with all languages", 210, 0.2)); | ||
|
||
assertThat($messages, equalTo([ | ||
new UserMessage("Total before promotion: 252.0"), | ||
new UserMessage("Total after promotion: 228.8"), | ||
])); | ||
} | ||
|
||
} |