From 35b224efb59c05922dc13903381959961b6fa69c Mon Sep 17 00:00:00 2001 From: Matthew Heroux Date: Sat, 20 Feb 2021 22:54:29 -0600 Subject: [PATCH] rename Parser Notation --- src/Contract/NotationInterface.php | 11 ++++++ src/Contract/ParserInterface.php | 10 ----- src/Contract/TurnInterface.php | 3 +- src/Cup.php | 10 ++--- src/Dice.php | 9 +++++ src/Factory/ConcreteFactory.php | 6 +-- src/Factory/ContainerFactory.php | 2 +- src/Factory/TurnFactory.php | 2 +- src/{Parser.php => Notation.php} | 15 ++++++-- src/Turn.php | 12 +++--- tests/src/Unit/App.php | 0 tests/src/Unit/DiceTest.php | 62 ++++++++++++++++++++++++++++++ 12 files changed, 112 insertions(+), 30 deletions(-) create mode 100644 src/Contract/NotationInterface.php delete mode 100644 src/Contract/ParserInterface.php rename src/{Parser.php => Notation.php} (79%) delete mode 100644 tests/src/Unit/App.php create mode 100644 tests/src/Unit/DiceTest.php diff --git a/src/Contract/NotationInterface.php b/src/Contract/NotationInterface.php new file mode 100644 index 0000000..d5d43f4 --- /dev/null +++ b/src/Contract/NotationInterface.php @@ -0,0 +1,11 @@ +container[$this->index]; } - public function next() + public function next() : void { - $this->index ++; + $this->index++; } public function key() @@ -61,12 +61,12 @@ public function key() return $this->index; } - public function valid() + public function valid(): bool { return isset($this->container[$this->key()]); } - public function rewind() + public function rewind() : void { $this->index = 0; } diff --git a/src/Dice.php b/src/Dice.php index a655a5a..cd46d6b 100644 --- a/src/Dice.php +++ b/src/Dice.php @@ -16,6 +16,7 @@ /** * Class Dice + * A dice has two or more sides and can be roll * @package Ouxsoft\LuckByDice */ class Dice @@ -42,4 +43,12 @@ public function roll() : int { return mt_rand(1, $this->sides); } + + /** + * @return int + */ + public function getSides() : int + { + return $this->sides; + } } diff --git a/src/Factory/ConcreteFactory.php b/src/Factory/ConcreteFactory.php index 61a4012..5eafe3f 100644 --- a/src/Factory/ConcreteFactory.php +++ b/src/Factory/ConcreteFactory.php @@ -13,7 +13,7 @@ namespace Ouxsoft\LuckByDice\Factory; use Ouxsoft\LuckByDice\Contract\AbstractFactoryInterface; -use Ouxsoft\LuckByDice\Parser; +use Ouxsoft\LuckByDice\Notation; use Ouxsoft\LuckByDice\Cup; use Ouxsoft\LuckByDice\Luck; @@ -21,9 +21,9 @@ class ConcreteFactory implements AbstractFactoryInterface { - public function makeParser(Container &$container): Parser + public function makeNotation(Container &$container): Notation { - return new Parser(); + return new Notation(); } public function makeCup(Container &$container): Cup diff --git a/src/Factory/ContainerFactory.php b/src/Factory/ContainerFactory.php index d4a485b..9f6b6dc 100644 --- a/src/Factory/ContainerFactory.php +++ b/src/Factory/ContainerFactory.php @@ -26,7 +26,7 @@ public static function buildContainer( ): Container { $container = new Container(); - $container['parser'] = $abstractFactory->makeParser($container); + $container['notation'] = $abstractFactory->makeNotation($container); $container['cup'] = $abstractFactory->makeCup($container); $container['luck'] = $abstractFactory->makeLuck($container); diff --git a/src/Factory/TurnFactory.php b/src/Factory/TurnFactory.php index c7264f4..b8a1feb 100644 --- a/src/Factory/TurnFactory.php +++ b/src/Factory/TurnFactory.php @@ -26,7 +26,7 @@ public static function getInstance(): Turn $container = ContainerFactory::buildContainer($abstractFactory); return new Turn( - $container['parser'], + $container['notation'], $container['cup'] ); } diff --git a/src/Parser.php b/src/Notation.php similarity index 79% rename from src/Parser.php rename to src/Notation.php index 97e725a..13d700c 100644 --- a/src/Parser.php +++ b/src/Notation.php @@ -10,15 +10,24 @@ namespace Ouxsoft\LuckByDice; -use Ouxsoft\LuckByDice\Contract\ParserInterface; +use Ouxsoft\LuckByDice\Contract\NotationInterface; -class Parser implements ParserInterface +class Notation implements NotationInterface { + /** + * @param Cup $cup + * @return string + */ + public function encode(Cup $cup) : string + { + + } + /** * @param $expression * @return array */ - public function run($expression): array + public function decode(string $expression): array { $diceGroups = []; $diceGroupExpressions = explode(',', strtolower($expression)); diff --git a/src/Turn.php b/src/Turn.php index 7bd03a2..0b2facf 100644 --- a/src/Turn.php +++ b/src/Turn.php @@ -25,9 +25,9 @@ class Turn implements TurnInterface */ private $cup; /** - * @var Parser + * @var Notation */ - private $parser; + private $notation; /** * @var int */ @@ -35,17 +35,17 @@ class Turn implements TurnInterface /** * Turn constructor. - * @param Parser $parser + * @param Notation $notation * @param Cup $cup * @param string|null $expression * @see Turn::setByString() */ public function __construct( - Parser $parser, + Notation $notation, Cup $cup, string $expression = null ) { - $this->parser = $parser; + $this->notation = $notation; $this->cup = $cup; if ($expression !== null) { @@ -59,7 +59,7 @@ public function __construct( */ public function set(string $expression) : void { - $this->cup = $this->parser->run($expression); + $this->cup = $this->notation->decode($expression); } /** diff --git a/tests/src/Unit/App.php b/tests/src/Unit/App.php deleted file mode 100644 index e69de29..0000000 diff --git a/tests/src/Unit/DiceTest.php b/tests/src/Unit/DiceTest.php new file mode 100644 index 0000000..1f2992d --- /dev/null +++ b/tests/src/Unit/DiceTest.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Ouxsoft\LivingMarkup\Tests\Unit; + +use Laminas\Http\Exception\OutOfRangeException; +use Ouxsoft\LuckByDice\Dice; +use PHPUnit\Framework\TestCase; +use Ouxsoft\LuckByDice\Factory\TurnFactory; + +class DiceTest extends TestCase +{ + private $dice; + + public function setUp(): void + { + $this->dice = new Dice(6); + } + + public function tearDown(): void + { + unset($this->dice); + } + + + /** + * @covers \Ouxsoft\LuckByDice\Dice::__construct + */ + public function test__construct() + { + $slides = $this->dice->getSides(); + $this->assertEquals(6, $slides); + + $this->expectException(\OutOfRangeException::class); + $this->dice = new Dice(1); + } + + /** + * @covers \Ouxsoft\LuckByDice\Dice::roll + */ + public function testRoll() + { + $outcome = $this->dice->roll(); + $this->assertIsInt($outcome); + } + + /** + * @covers \Ouxsoft\LuckByDice\Dice::getSides + */ + public function testGetSides() + { + $slides = $this->dice->getSides(); + $this->assertEquals(6, $slides); + } +}