diff --git a/php/README.md b/php/README.md index 91ca1475..d67cfec1 100644 --- a/php/README.md +++ b/php/README.md @@ -133,4 +133,4 @@ PHPUnit `composer phpstan` can be run: ps ``` -**Happy coding**! +## ffff diff --git a/php/composer.json b/php/composer.json index 1e0ecb7d..71eff6c3 100644 --- a/php/composer.json +++ b/php/composer.json @@ -2,7 +2,7 @@ "name": "emilybache/gilded-rose-refactoring-kata", "description": "A kata to practice refactoring, tests and polymorphism", "require": { - "php": "^7.3 || ^8.0" + "php": "^8.0" }, "autoload": { "psr-4": { @@ -23,13 +23,13 @@ "approvals/approval-tests": "^1.4" }, "scripts": { - "checkcode": "phpcs src tests --standard=PSR12", - "fixcode": "phpcbf src tests --standard=PSR12", - "test": "phpunit", - "tests": "phpunit", - "test-coverage": "phpunit --coverage-html build/coverage", - "check-cs": "ecs check", - "fix-cs": "ecs check --fix", - "phpstan": "phpstan analyse --ansi" + "checkcode": "php vendor/bin/phpcs src tests --standard=PSR12", + "fixcode": "php vendor/bin/phpcbf src tests --standard=PSR12", + "test": "php vendor/bin/phpunit", + "tests": "php vendor/bin/phpunit", + "test-coverage": "php vendor/bin/phpunit --coverage-html build/coverage", + "check-cs": "php vendor/bin/ecs check", + "fix-cs": "php vendor/bin/ecs check --fix", + "phpstan": "php vendor/bin/phpstan analyse --ansi" } } diff --git a/php/fixtures/texttest_fixture.php b/php/fixtures/texttest_fixture.php index 7363c773..c8d13c20 100644 --- a/php/fixtures/texttest_fixture.php +++ b/php/fixtures/texttest_fixture.php @@ -24,7 +24,7 @@ $items = [ $app = new GildedRose($items); -$days = 2; +$days = 31; if (count($argv) > 1) { $days = (int) $argv[1]; } diff --git a/php/src/GildedRose.php b/php/src/GildedRose.php index cfb7f78a..552a94c5 100644 --- a/php/src/GildedRose.php +++ b/php/src/GildedRose.php @@ -1,69 +1,127 @@ items = $items; } public function updateQuality(): void { + /** @var Item $item */ foreach ($this->items as $item) { - if ($item->name != 'Aged Brie' and $item->name != 'Backstage passes to a TAFKAL80ETC concert') { - if ($item->quality > 0) { - if ($item->name != 'Sulfuras, Hand of Ragnaros') { - $item->quality = $item->quality - 1; + //conjured quality -2 + test. test first! +// if ($item->name === 'Conjured Mana Cake') { +// $this->lowerQuality($item, 1); +// } + + //spare Sulfuras from quality drop + if ($item->name === 'Sulfuras, Hand of Ragnaros') { + continue; + } + + $disabledNames = ['Aged Brie', 'Backstage passes to a TAFKAL80ETC concert']; + if (in_array($item->name, $disabledNames)) { + if ($item->quality < 50) { + $this->increaseQuality($item, 1); + if ($item->name === 'Backstage passes to a TAFKAL80ETC concert') { + if ($item->sell_in <= 10 && $item->sell_in > 5) { + $this->increaseQuality($item, 1); + } + elseif ($item->sell_in <= 5) { + $this->increaseQuality($item, 2); + } } } } else { - if ($item->quality < 50) { - $item->quality = $item->quality + 1; - if ($item->name == 'Backstage passes to a TAFKAL80ETC concert') { - if ($item->sell_in < 11) { - if ($item->quality < 50) { - $item->quality = $item->quality + 1; - } - } - if ($item->sell_in < 6) { - if ($item->quality < 50) { - $item->quality = $item->quality + 1; - } - } - } + if ($item->quality > 0) { + $this->lowerQuality($item, 1); } } - if ($item->name != 'Sulfuras, Hand of Ragnaros') { - $item->sell_in = $item->sell_in - 1; - } - - if ($item->sell_in < 0) { - if ($item->name != 'Aged Brie') { - if ($item->name != 'Backstage passes to a TAFKAL80ETC concert') { - if ($item->quality > 0) { - if ($item->name != 'Sulfuras, Hand of Ragnaros') { - $item->quality = $item->quality - 1; - } - } - } else { - $item->quality = $item->quality - $item->quality; + if ($item->sell_in <= 0) { + if ($item->name === 'Aged Brie') { + if ($item->quality < 50) { + $this->increaseQuality($item, 1); } } else { - if ($item->quality < 50) { - $item->quality = $item->quality + 1; + if ($item->name === 'Backstage passes to a TAFKAL80ETC concert') { + $this->lowerQuality($item, $item->quality); + } else { + if ($item->quality > 0) { + $this->lowerQuality($item, 1); + } } } } + $this->lowerSellIn($item, 1); } } -} + + private function lowerQuality(Item $item, int $amount) + { + if ($item->quality - $amount > 0) { + $item->quality -= $amount; + } else { + $item->quality = 0; + } + } + + private function increaseQuality(Item $item, int $amount) + { + if ($item->quality + $amount <= 50) { + $item->quality += $amount; + } else { + $item->quality = 50; + } + } + + private function lowerSellIn(Item $item, int $amount) + { + $item->sell_in -= $amount; + } +} \ No newline at end of file diff --git a/php/tests/GildedRoseTest.php b/php/tests/GildedRoseTest.php index 52e7ddc6..18d389bd 100644 --- a/php/tests/GildedRoseTest.php +++ b/php/tests/GildedRoseTest.php @@ -1,7 +1,5 @@ updateQuality(); - $this->assertSame('fixme', $items[0]->name); + self::assertSame('foo', $items[0]->name); + self::assertSame(-1, $items[0]->sell_in); + } -} + + public function testNotBelowZero(): void + { + $items = [new Item('foo', 0, 1)]; + $gildedRose = new GildedRose($items); + $gildedRose->updateQuality(); + self::assertSame(0, $items[0]->quality); + self::assertSame(-1, $items[0]->sell_in); + } + + public function testQualityDropsTwice(): void + { + $items = [new Item('foo', 0, 2)]; + $gildedRose = new GildedRose($items); + $gildedRose->updateQuality(); + self::assertSame(0, $items[0]->quality); + self::assertSame(-1, $items[0]->sell_in); + } + + public function testAgedBrieQualityIncreases(): void + { + $items = [new Item('Aged Brie', 0, 1)]; + $gildedRose = new GildedRose($items); + $gildedRose->updateQuality(); + self::assertSame(3, $items[0]->quality); + self::assertSame(-1, $items[0]->sell_in); + } + + public function testQualityNotMoreThan50(): void + { + $items = [new Item('Aged Brie', -1, 47)]; + $gildedRose = new GildedRose($items); + $gildedRose->updateQuality(); + self::assertSame(49, $items[0]->quality); + self::assertSame(-2, $items[0]->sell_in); + } + + public function testSulfurasQualityAndSellinStays(): void + { + $items = [new Item('Sulfuras, Hand of Ragnaros', 1, 47)]; + $gildedRose = new GildedRose($items); + $gildedRose->updateQuality(); + self::assertSame(47, $items[0]->quality); + self::assertSame(1, $items[0]->sell_in); + } + + /** @dataProvider BackstageScenario */ + public function testBackstageQuality(array $scenario): void + { + $items = [new Item('Backstage passes to a TAFKAL80ETC concert', $scenario['data']['sell_in'], $scenario['data']['quality'])]; + $gildedRose = new GildedRose($items); + $gildedRose->updateQuality(); + self::assertSame($scenario['expect']['quality'], $items[0]->quality); + self::assertSame($scenario['expect']['sell_in'], $items[0]->sell_in); + } + + public function BackstageScenario(): array + { + return [ + [[ + 'data' => ['sell_in' => 1, 'quality' => 47], + 'expect' => ['sell_in' => 0, 'quality' => 50], + ]], + [[ + 'data' => ['sell_in' => 5, 'quality' => 50], + 'expect' => ['sell_in' => 4, 'quality' => 50], + ]], + [[ + 'data' => ['sell_in' => 9, 'quality' => 40], + 'expect' => ['sell_in' => 8, 'quality' => 42], + ]] + ]; + } +} \ No newline at end of file