mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-19 08:21:37 +00:00
Approval Test passed
This commit is contained in:
parent
bee064f7c6
commit
d4aed50b88
@ -133,4 +133,4 @@ PHPUnit `composer phpstan` can be run:
|
|||||||
ps
|
ps
|
||||||
```
|
```
|
||||||
|
|
||||||
**Happy coding**!
|
## ffff
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
"name": "emilybache/gilded-rose-refactoring-kata",
|
"name": "emilybache/gilded-rose-refactoring-kata",
|
||||||
"description": "A kata to practice refactoring, tests and polymorphism",
|
"description": "A kata to practice refactoring, tests and polymorphism",
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^7.3 || ^8.0"
|
"php": "^8.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
@ -23,13 +23,13 @@
|
|||||||
"approvals/approval-tests": "^1.4"
|
"approvals/approval-tests": "^1.4"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"checkcode": "phpcs src tests --standard=PSR12",
|
"checkcode": "php vendor/bin/phpcs src tests --standard=PSR12",
|
||||||
"fixcode": "phpcbf src tests --standard=PSR12",
|
"fixcode": "php vendor/bin/phpcbf src tests --standard=PSR12",
|
||||||
"test": "phpunit",
|
"test": "php vendor/bin/phpunit",
|
||||||
"tests": "phpunit",
|
"tests": "php vendor/bin/phpunit",
|
||||||
"test-coverage": "phpunit --coverage-html build/coverage",
|
"test-coverage": "php vendor/bin/phpunit --coverage-html build/coverage",
|
||||||
"check-cs": "ecs check",
|
"check-cs": "php vendor/bin/ecs check",
|
||||||
"fix-cs": "ecs check --fix",
|
"fix-cs": "php vendor/bin/ecs check --fix",
|
||||||
"phpstan": "phpstan analyse --ansi"
|
"phpstan": "php vendor/bin/phpstan analyse --ansi"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,7 +24,7 @@ $items = [
|
|||||||
|
|
||||||
$app = new GildedRose($items);
|
$app = new GildedRose($items);
|
||||||
|
|
||||||
$days = 2;
|
$days = 31;
|
||||||
if (count($argv) > 1) {
|
if (count($argv) > 1) {
|
||||||
$days = (int) $argv[1];
|
$days = (int) $argv[1];
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,69 +1,127 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
declare(strict_types=1);
|
/*
|
||||||
|
======================================
|
||||||
|
Gilded Rose Requirements Specification
|
||||||
|
======================================
|
||||||
|
|
||||||
|
Hi and welcome to team Gilded Rose. As you know, we are a small inn with a prime location in a
|
||||||
|
prominent city ran by a friendly innkeeper named Allison. We also buy and sell only the finest goods.
|
||||||
|
Unfortunately, our goods are constantly degrading in quality as they approach their sell by date. We
|
||||||
|
have a system in place that updates our inventory for us. It was developed by a no-nonsense type named
|
||||||
|
Leeroy, who has moved on to new adventures. Your task is to add the new feature to our system so that
|
||||||
|
we can begin selling a new category of items. First an introduction to our system:
|
||||||
|
|
||||||
|
- All items have a SellIn value which denotes the number of days we have to sell the item
|
||||||
|
- All items have a Quality value which denotes how valuable the item is
|
||||||
|
- At the end of each day our system lowers both values for every item
|
||||||
|
|
||||||
|
Pretty simple, right? Well this is where it gets interesting:
|
||||||
|
|
||||||
|
- Once the sell by date has passed, Quality degrades twice as fast
|
||||||
|
- The Quality of an item is never negative
|
||||||
|
- "Aged Brie" actually increases in Quality the older it gets
|
||||||
|
- The Quality of an item is never more than 50
|
||||||
|
- "Sulfuras", being a legendary item, never has to be sold or decreases in Quality
|
||||||
|
- "Backstage passes", like aged brie, increases in Quality as its SellIn value approaches;
|
||||||
|
Quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less but
|
||||||
|
Quality drops to 0 after the concert
|
||||||
|
|
||||||
|
We have recently signed a supplier of conjured items. This requires an update to our system:
|
||||||
|
|
||||||
|
- "Conjured" items degrade in Quality twice as fast as normal items
|
||||||
|
|
||||||
|
Feel free to make any changes to the UpdateQuality method and add any new code as long as everything
|
||||||
|
still works correctly. However, do not alter the Item class or Items property as those belong to the
|
||||||
|
goblin in the corner who will insta-rage and one-shot you as he doesn't believe in shared code
|
||||||
|
ownership (you can make the UpdateQuality method and Items property static if you like, we'll cover
|
||||||
|
for you).
|
||||||
|
|
||||||
|
Just for clarification, an item can never have its Quality increase above 50, however "Sulfuras" is a
|
||||||
|
legendary item and as such its Quality is 80 and it never alters.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
namespace GildedRose;
|
namespace GildedRose;
|
||||||
|
|
||||||
final class GildedRose
|
final class GildedRose
|
||||||
{
|
{
|
||||||
/**
|
public function __construct(private array $items)
|
||||||
* @var Item[]
|
|
||||||
*/
|
|
||||||
private $items;
|
|
||||||
|
|
||||||
public function __construct(array $items)
|
|
||||||
{
|
{
|
||||||
$this->items = $items;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateQuality(): void
|
public function updateQuality(): void
|
||||||
{
|
{
|
||||||
|
/** @var Item $item */
|
||||||
foreach ($this->items as $item) {
|
foreach ($this->items as $item) {
|
||||||
if ($item->name != 'Aged Brie' and $item->name != 'Backstage passes to a TAFKAL80ETC concert') {
|
//conjured quality -2 + test. test first!
|
||||||
if ($item->quality > 0) {
|
// if ($item->name === 'Conjured Mana Cake') {
|
||||||
if ($item->name != 'Sulfuras, Hand of Ragnaros') {
|
// $this->lowerQuality($item, 1);
|
||||||
$item->quality = $item->quality - 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 {
|
} else {
|
||||||
if ($item->quality < 50) {
|
if ($item->quality > 0) {
|
||||||
$item->quality = $item->quality + 1;
|
$this->lowerQuality($item, 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->name != 'Sulfuras, Hand of Ragnaros') {
|
if ($item->sell_in <= 0) {
|
||||||
$item->sell_in = $item->sell_in - 1;
|
if ($item->name === 'Aged Brie') {
|
||||||
}
|
if ($item->quality < 50) {
|
||||||
|
$this->increaseQuality($item, 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;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ($item->quality < 50) {
|
if ($item->name === 'Backstage passes to a TAFKAL80ETC concert') {
|
||||||
$item->quality = $item->quality + 1;
|
$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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,7 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Tests;
|
namespace Tests;
|
||||||
|
|
||||||
use GildedRose\GildedRose;
|
use GildedRose\GildedRose;
|
||||||
@ -15,6 +13,81 @@ class GildedRoseTest extends TestCase
|
|||||||
$items = [new Item('foo', 0, 0)];
|
$items = [new Item('foo', 0, 0)];
|
||||||
$gildedRose = new GildedRose($items);
|
$gildedRose = new GildedRose($items);
|
||||||
$gildedRose->updateQuality();
|
$gildedRose->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],
|
||||||
|
]]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user