その他商品のリファクタリング

This commit is contained in:
bestaff-hirakawa 2022-08-07 03:19:03 +09:00
parent 01eb701417
commit 1f64334566
2 changed files with 85 additions and 0 deletions

View File

@ -35,6 +35,10 @@ final class GildedRose
・計算後sell_inが10未満の場合、さらにquality+1する ・計算後sell_inが10未満の場合、さらにquality+1する
・計算後sell_inが5未満の場合、さらにquality+1する ・計算後sell_inが5未満の場合、さらにquality+1する
・計算後sell_inが0未満の場合、qualityを0にする ・計算後sell_inが0未満の場合、qualityを0にする
[その他商品]
・引数のsell_inを-1する
・引数のqualityを-1する
・sell_inが0未満の場合、さらにquality-1する ★仕様書に記載なかったがコード上はこのようになっている
*/ */
/** /**
@ -52,6 +56,9 @@ final class GildedRose
} elseif ($this->item->name === 'Backstage passes to a TAFKAL80ETC concert') { } elseif ($this->item->name === 'Backstage passes to a TAFKAL80ETC concert') {
// 商品Backstage passesの処理 // 商品Backstage passesの処理
$this->backstagePasses(); $this->backstagePasses();
} else {
// その他商品の処理
$this->others();
} }
} }
} }
@ -100,6 +107,20 @@ final class GildedRose
} }
} }
/**
* その他商品
*/
private function others(): void
{
$this->calcSellInSubtraction();
$this->calcQualitySubtraction();
// sell_inが0未満の場合、sell_inを再減算する
if ($this->item->sell_in < 0) {
$this->calcQualitySubtraction();
}
}
/** /**
* sell_inの減算を行う * sell_inの減算を行う
*/ */
@ -118,5 +139,16 @@ final class GildedRose
++$this->item->quality; ++$this->item->quality;
} }
} }
/**
* qualityの減算を行う
*/
private function calcQualitySubtraction(): void
{
// 1以上の場合計算
if ($this->item->quality >= 1) {
--$this->item->quality;
}
}
} }

View File

@ -193,6 +193,55 @@ class GildedRoseTest extends TestCase
$this->assertSame(50, $items[0]->quality); $this->assertSame(50, $items[0]->quality);
} }
/**
* Foosell_inが1以上、qualityが50未満
* 期待値sell_inが-1、qualityが-1
*/
public function testFooNormal(): void
{
$items = [new Item('Foo', 5, 10)];
$gildedRose = new GildedRose($items);
$gildedRose->updateQuality();
$this->assertSame('Foo', $items[0]->name);
$this->assertSame(4, $items[0]->sell_in);
$this->assertSame(9, $items[0]->quality);
}
/**
* Foosell_inが0以下、qualityが50未満
* 期待値sell_inが-1、qualityが-2
*/
public function testFooSellIn0OrLess(): void
{
$items = [new Item('Foo', 0, 10)];
$gildedRose = new GildedRose($items);
$gildedRose->updateQuality();
$this->assertSame('Foo', $items[0]->name);
$this->assertSame(-1, $items[0]->sell_in);
$this->assertSame(8, $items[0]->quality);
$items = [new Item('Foo', -1, 10)];
$gildedRose = new GildedRose($items);
$gildedRose->updateQuality();
$this->assertSame('Foo', $items[0]->name);
$this->assertSame(-2, $items[0]->sell_in);
$this->assertSame(8, $items[0]->quality);
}
/**
* Foosell_inが1以上、qualityが0
* 期待値sell_inが-1、qualityは変更なし
*/
public function testFooQuality0(): void
{
$items = [new Item('Foo', 5, 0)];
$gildedRose = new GildedRose($items);
$gildedRose->updateQuality();
$this->assertSame('Foo', $items[0]->name);
$this->assertSame(4, $items[0]->sell_in);
$this->assertSame(0, $items[0]->quality);
}
/** /**
* 複数商品 * 複数商品
*/ */
@ -202,6 +251,7 @@ class GildedRoseTest extends TestCase
new Item('Aged Brie', 5, 10), new Item('Aged Brie', 5, 10),
new Item('Sulfuras, Hand of Ragnaros', 5, 80), new Item('Sulfuras, Hand of Ragnaros', 5, 80),
new Item('Backstage passes to a TAFKAL80ETC concert', 5, 10), new Item('Backstage passes to a TAFKAL80ETC concert', 5, 10),
new Item('Foo', 5, 10),
]; ];
$gildedRose = new GildedRose($items); $gildedRose = new GildedRose($items);
$gildedRose->updateQuality(); $gildedRose->updateQuality();
@ -214,6 +264,9 @@ class GildedRoseTest extends TestCase
$this->assertSame('Backstage passes to a TAFKAL80ETC concert', $items[2]->name); $this->assertSame('Backstage passes to a TAFKAL80ETC concert', $items[2]->name);
$this->assertSame(4, $items[2]->sell_in); $this->assertSame(4, $items[2]->sell_in);
$this->assertSame(13, $items[2]->quality); $this->assertSame(13, $items[2]->quality);
$this->assertSame('Foo', $items[3]->name);
$this->assertSame(4, $items[3]->sell_in);
$this->assertSame(9, $items[3]->quality);
} }
// テストエラーの原因が特定できないので後で調査する // テストエラーの原因が特定できないので後で調査する