From 9884e30e01b457342bceb54069569f149c5843f3 Mon Sep 17 00:00:00 2001 From: bestaff-hirakawa Date: Mon, 8 Aug 2022 09:20:18 +0900 Subject: [PATCH] =?UTF-8?q?Conjured=E3=81=AB=E5=AF=BE=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=80=8C=E8=B2=A9=E5=A3=B2=E3=81=99=E3=82=8B=E3=81=9F=E3=82=81?= =?UTF-8?q?=E3=81=AE=E6=AE=8B=E3=82=8A=E6=97=A5=E6=95=B0=E3=81=8C=E7=84=A1?= =?UTF-8?q?=E3=81=8F=E3=81=AA=E3=82=8B=E3=81=A8=E3=80=81Quality=E5=80=A4?= =?UTF-8?q?=E3=81=AF2=E5=B0=8F=E3=81=95=E3=81=8F=E3=81=AA=E3=82=8A?= =?UTF-8?q?=E3=81=BE=E3=81=99=E3=80=82=E3=80=8D=E4=BB=95=E6=A7=98=E3=81=8C?= =?UTF-8?q?=E6=BC=8F=E3=82=8C=E3=81=A6=E3=81=84=E3=81=9F=E3=81=AE=E3=81=A7?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- php/src/GildedRose.php | 28 +++++++++++++++--- php/tests/GildedRoseTest.php | 56 ++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/php/src/GildedRose.php b/php/src/GildedRose.php index 5751a93e..6e1b1239 100644 --- a/php/src/GildedRose.php +++ b/php/src/GildedRose.php @@ -38,10 +38,11 @@ final class GildedRose [商品:Conjured] ・引数のsell_inを-1する ・引数のqualityを-2する + ・sell_inが0未満の場合、さらにquality-2する [その他商品] ・引数のsell_inを-1する ・引数のqualityを-1する - ・sell_inが0未満の場合、さらにquality-1する ★仕様書に記載なかったがコード上はこのようになっている + ・sell_inが0未満の場合、さらにquality-1する */ /** @@ -119,9 +120,12 @@ final class GildedRose private function conjured(): void { $this->calcSellInSubtraction(); - // 新たな関数は作成せず減算処理を2回実行する - $this->calcQualitySubtraction(); - $this->calcQualitySubtraction(); + $this->calcQualityDoubleSubtraction(); + + // sell_inが0未満の場合、sell_inを再減算する + if ($this->item->sell_in < 0) { + $this->calcQualityDoubleSubtraction(); + } } /** @@ -167,4 +171,20 @@ final class GildedRose --$this->item->quality; } } + + /** + * qualityを2倍の減算を行う + */ + private function calcQualityDoubleSubtraction(): void + { + // 1以上の場合計算 + if ($this->item->quality >= 1) { + $this->item->quality -=2; + } + + // qualityが0未満の場合、0を設定 + if ($this->item->quality < 0) { + $this->item->quality = 0; + } + } } diff --git a/php/tests/GildedRoseTest.php b/php/tests/GildedRoseTest.php index 7ffe28be..e316fd8d 100644 --- a/php/tests/GildedRoseTest.php +++ b/php/tests/GildedRoseTest.php @@ -284,6 +284,62 @@ class GildedRoseTest extends TestCase $this->assertSame(0, $items[0]->quality); } + /** + * Conjured:sell_inが0以下、qualityが50未満 + * 期待値:sell_inが-1、qualityが-4 + */ + public function testConjuredSellIn0OrLess(): void + { + $items = [new Item('Conjured Mana Cake', 0, 10)]; + $gildedRose = new GildedRose($items); + $gildedRose->updateQuality(); + $this->assertSame('Conjured Mana Cake', $items[0]->name); + $this->assertSame(-1, $items[0]->sell_in); + $this->assertSame(6, $items[0]->quality); + + $items = [new Item('Conjured Mana Cake', -1, 10)]; + $gildedRose = new GildedRose($items); + $gildedRose->updateQuality(); + $this->assertSame('Conjured Mana Cake', $items[0]->name); + $this->assertSame(-2, $items[0]->sell_in); + $this->assertSame(6, $items[0]->quality); + } + + /** + * Conjured:sell_inが0以下、qualityが2以下 + * 期待値:sell_inが-1、qualityを減算、0が下限 + */ + public function testConjuredSellIn0OrLessAndQuality2OrLess(): void + { + $items = [new Item('Conjured Mana Cake', 0, 2)]; + $gildedRose = new GildedRose($items); + $gildedRose->updateQuality(); + $this->assertSame('Conjured Mana Cake', $items[0]->name); + $this->assertSame(-1, $items[0]->sell_in); + $this->assertSame(0, $items[0]->quality); + + $items = [new Item('Conjured Mana Cake', 0, 1)]; + $gildedRose = new GildedRose($items); + $gildedRose->updateQuality(); + $this->assertSame('Conjured Mana Cake', $items[0]->name); + $this->assertSame(-1, $items[0]->sell_in); + $this->assertSame(0, $items[0]->quality); + } + + /** + * Conjured:sell_inが0、qualityが0 + * 期待値:sell_inが-1、qualityは変更なし + */ + public function testConjuredSellIn0AndQuality0(): void + { + $items = [new Item('Conjured Mana Cake', 0, 0)]; + $gildedRose = new GildedRose($items); + $gildedRose->updateQuality(); + $this->assertSame('Conjured Mana Cake', $items[0]->name); + $this->assertSame(-1, $items[0]->sell_in); + $this->assertSame(0, $items[0]->quality); + } + /** * 複数商品 */