mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-14 22:21:20 +00:00
To begin taking advantage of new domain, use wrt default items, and fix business logic
This commit is contained in:
parent
dcde3a22a2
commit
d7bb4c9f94
@ -25,24 +25,8 @@ final class GildedRose
|
||||
public function updateQuality(): void
|
||||
{
|
||||
foreach ($this->items as $item) {
|
||||
|
||||
if ($item->name === 'Sulfuras, Hand of Ragnaros' || $item->name === 'Backstage passes to a TAFKAL80ETC concert' || $item->name === 'Aged Brie') {
|
||||
$gildedRoseItem = $this->gildedRoseItemFactory->createGildedRoseItem($item);
|
||||
$gildedRoseItem->ageByOneDay();
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($item->quality > 0) {
|
||||
$item->quality = $item->quality - 1;
|
||||
}
|
||||
|
||||
$item->sellIn = $item->sellIn - 1;
|
||||
|
||||
if ($item->sellIn < 0) {
|
||||
if ($item->quality > 0) {
|
||||
$item->quality = $item->quality - 1;
|
||||
}
|
||||
}
|
||||
$gildedRoseItem = $this->gildedRoseItemFactory->createGildedRoseItem($item);
|
||||
$gildedRoseItem->ageByOneDay();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,9 +14,15 @@ class DefaultItem implements GildedRoseItem
|
||||
public function ageByOneDay(): void
|
||||
{
|
||||
$this->item->sellIn -= 1;
|
||||
$this->item->quality = max(0, $this->getNewQuality($this->item->quality, $this->item->sellIn));
|
||||
}
|
||||
|
||||
if ($this->item->quality >= 1) {
|
||||
$this->item->quality -= 1;
|
||||
private function getNewQuality(int $previousQuality, int $sellIn): int
|
||||
{
|
||||
if ($sellIn < 0) {
|
||||
return $previousQuality - 2;
|
||||
}
|
||||
|
||||
return $previousQuality - 1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,7 +16,13 @@ Feature: Default quality degradation
|
||||
Then the item should have a quality of 15
|
||||
|
||||
|
||||
Scenario: Quality cannot be negative
|
||||
Given an item with a sell-in of 20 and a quality of 0
|
||||
When I update the quality
|
||||
Then the item should have a quality of 0
|
||||
Scenario: Quality cannot be negative
|
||||
Given an item with a sell-in of 20 and a quality of 0
|
||||
When I update the quality
|
||||
Then the item should have a quality of 0
|
||||
|
||||
|
||||
Scenario: Once the sell-by date has passed, quality degrades twice as fast
|
||||
Given an item with a sell-in of 0 and a quality of 20
|
||||
When I update the quality
|
||||
Then the item should have a quality of 18
|
||||
|
||||
@ -31,4 +31,13 @@ class DefaultItemTest extends TestCase
|
||||
|
||||
$this->assertEquals(0, $item->quality);
|
||||
}
|
||||
|
||||
public function testQualityDegradesTwiceAsFastAfterSellBy(): void
|
||||
{
|
||||
$item = new Item('foo', 0, 20);
|
||||
$gildedRoseItem = new DefaultItem($item);
|
||||
$gildedRoseItem->ageByOneDay();
|
||||
|
||||
$this->assertEquals(18, $item->quality);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user