Skip to content

Commit

Permalink
Add setNotation
Browse files Browse the repository at this point in the history
  • Loading branch information
hxtree committed Sep 17, 2021
1 parent e78c006 commit 84aae39
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 58 deletions.
35 changes: 13 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $ composer require ouxsoft/luckbydice
use Ouxsoft\LuckByDice\Factory\TurnFactory;

$turn = TurnFactory::getInstance();
$turn->notation->set('10d10,1d6+3*7,d%');
$turn->setNotation('10d10,1d6+3*7,d%');
echo $turn->roll();

// we should be luckier with this next roll
Expand Down Expand Up @@ -72,55 +72,46 @@ Matthew Heroux<br />
See also the [list of contributors](https://github.com/Ouxsoft/LuckByDice/graphs/contributors) who participated in this project.

### Contributing
LuckByDice is an open source project. If you find a problem or want to discuss new features or improvements
LuckByDice is open source software project. If you find a problem or want to discuss new features or improvements
create an issue, and/or if possible create a pull request.

For local package development use [Docker](https://www.docker.com/products/docker-desktop):

#### Local Setup
Clone Repo
Build Standard container
```
git clone https://github.com/Ouxsoft/LuckByDice.git
cd LuckByDice
```


**Standard container**
```
docker build --target standard --tag luckbydice:latest -f Dockerfile .
# Run Interactive test
docker run -it luckbydice:latest php tests/src/Feature/Cli.php 6d6
```

**Test container**
Build Test container
```
docker build --target test --tag luckbydice:latest -f Dockerfile .
docker run -it --mount type=bind,source="$(pwd)"/,target=/application/ luckbydice:latest composer install
```

# Automated Tests
## Unit Tests using local volume
Run Automated Unit Tests using local volume
```
docker run -it --mount type=bind,source="$(pwd)"/,target=/application/ luckbydice:latest composer test
```

## Benchmark Tests using local volume
Run Automated Benchmark Tests using local volume
```
docker run -it --mount type=bind,source="$(pwd)"/,target=/application/ luckbydice:latest ./vendor/bin/phpbench run tests/src/Benchmark --report=default
```

## Manual Tests
### Feature CLI Test using local volume
Run Manual CLI Feature Test using local volume
```
docker run -it --mount type=bind,source="$(pwd)"/,target=/application/ luckbydice:latest php tests/src/Feature/Cli.php 1d10+4*2 0
```

### Interactive Tests
Creates a server available at [http://localhost/](http://localhost/)
Start test server available at [http://localhost/](http://localhost/)
```
docker run -it -p 80:80 --mount type=bind,source="$(pwd)"/,target=/application luckbydice:latest bash -c 'cd tests/src/Interactive && php -S 0.0.0.0:80'
```

### Build Docs
Build Docs
```
# Build Docs
docker build --target docs --tag luckbydice:docs-latest -f Dockerfile .
docker run -it --mount type=bind,source="$(pwd)"/docs,target=/app/docs luckbydice:docs-latest bash -c "doxygen Doxyfile && doxyphp2sphinx Ouxsoft::LuckByDice"
```
Expand Down
31 changes: 22 additions & 9 deletions src/Turn.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ class Turn implements TurnInterface
* @var Cup
*/
private $cup;

/**
* @var Notation
*/
public $notation;
private $notation;

/**
* @var Luck
*/
Expand Down Expand Up @@ -64,6 +66,23 @@ public function __construct(
}
}

/**
* Get the dice notation for the entire cup
* @return string
*/
public function getNotation() : string
{
return $this->notation->get();
}

/**
* @param $notation
*/
public function setNotation($notation) : void
{
$this->notation->set($notation);
}

/**
* Get Luck
* @return int
Expand All @@ -82,6 +101,7 @@ public function setLuck(int $luck)
$this->luck->set($luck);
}


/**
* Roll each dice group, update luck, and return outcome with luck modifier applied
* @return int total
Expand Down Expand Up @@ -175,12 +195,5 @@ public function getCup() : Cup
return $this->cup;
}

/**
* Get the dice notation for the entire cup
* @return string
*/
public function getNotation() : string
{
return $this->notation->get();
}
}

2 changes: 1 addition & 1 deletion tests/src/Benchmark/TurnBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function tearDown() : void
*/
public function benchTurn($params)
{
$this->turn->notation->set($params['notation']);
$this->turn->setNotation($params['notation']);
$this->turn->roll();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/src/Feature/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

$notation = !empty($argv[1]) ? $argv[1] : '10d6,1d2+12*2,3d3,d%';
$luck = !empty($argv[2]) ? $argv[2] : '0';
$turn->notation->set($notation);
$turn->setNotation($notation);
$turn->setLuck($luck);

do {
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Feature/Cvs/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
echo 'Building data.csv file ...';
$turn = TurnFactory::getInstance();

$turn->notation->set('10d10');
$turn->setNotation('10d10');

$fp = fopen(__DIR__ . '/data.csv', 'w');

Expand Down
2 changes: 1 addition & 1 deletion tests/src/Interactive/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

$iterations = 10000;

$turn->notation->set('10d10');
$turn->setNotation('10d10');

$datasets = [];

Expand Down
2 changes: 1 addition & 1 deletion tests/src/Interactive/api/turn.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$turn = TurnFactory::getInstance();

if(isset($_POST['notation'])){
$turn->notation->set($_POST['notation']);
$turn->setNotation($_POST['notation']);
}

if(isset($_POST['luck'])){
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Unit/Draw/AsciiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testDice()
public function testCollection()
{
$turn = TurnFactory::getInstance();
$turn->notation->set('10d6,1d2+12*2,3d3,d%');
$turn->setNotation('10d6,1d2+12*2,3d3,d%');
$turn->roll();
foreach($turn->getCup() as $collection){
$this->assertIsString($this->draw->collection($collection));
Expand Down
26 changes: 16 additions & 10 deletions tests/src/Unit/NotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

use PHPUnit\Framework\TestCase;
use Ouxsoft\LuckByDice\Factory\TurnFactory;
use Ouxsoft\LuckByDice\Notation;
use Ouxsoft\LuckByDice\Cup;

class NotationTest extends TestCase
{
Expand All @@ -32,7 +34,7 @@ public function tearDown(): void
*/
public function test__construct()
{
$this->assertObjectHasAttribute( 'cup', $this->turn->notation);
$this->assertObjectHasAttribute( 'notation', $this->turn);
}

/**
Expand All @@ -43,11 +45,11 @@ public function test__construct()
public function testSet()
{
$notation = "d4,2d6,3d8+2,4d10*2,5d20+10*2,6d20-2,10d%";
$this->turn->notation->set($notation);
$outcome = $this->turn->notation->get();
$this->turn->setNotation($notation);
$outcome = $this->turn->getNotation();
$this->assertEquals($notation, $outcome);

$outcome = (string) $this->turn->notation;
$outcome = (string) $this->turn->getNotation();
$this->assertEquals($notation, $outcome);
}

Expand All @@ -58,8 +60,8 @@ public function testSet()
public function testGet()
{
$notation = "d4,2d6,3d8+2,4d10*2,5d20+10*2,6d20-2,10d%";
$this->turn->notation->set($notation);
$outcome = $this->turn->notation->get();
$this->turn->setNotation($notation);
$outcome = $this->turn->getNotation();
$this->assertEquals($notation, $outcome);
}

Expand All @@ -68,18 +70,22 @@ public function testGet()
*/
public function testSetSeparator()
{
$cup = new Cup();
$notation = new Notation($cup);
$separator = ';';
$this->turn->notation->setSeparator($separator);
$this->assertEquals($separator, $this->turn->notation->getSeparator($separator));
$notation->setSeparator($separator);
$this->assertEquals($separator, $notation->getSeparator());
}

/**
* @covers \Ouxsoft\LuckByDice\Notation::getSeparator
*/
public function testGetSeparator()
{
$cup = new Cup();
$notation = new Notation($cup);
$separator = ';';
$this->turn->notation->setSeparator($separator);
$this->assertEquals($separator, $this->turn->notation->getSeparator($separator));
$notation->setSeparator($separator);
$this->assertEquals($separator, $notation->getSeparator());
}
}
33 changes: 22 additions & 11 deletions tests/src/Unit/TurnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,29 @@ public function test__construct() {

$notation = 'd6';
$this->turn = TurnFactory::getInstance($notation);
$outcome = $this->turn->notation->get();
$outcome = $this->turn->getNotation();
$this->assertEquals($notation, $outcome);
}

/**
* @covers \Ouxsoft\LuckByDice\Turn::getNotation
*/
public function testGetNotation()
{
$this->assertIsString($this->turn->getNotation());
}

/**
* @covers \Ouxsoft\LuckByDice\Turn::setNotation
*/
public function testSetNotation()
{
$notation = "2d4";
$this->turn->setNotation($notation);
$outcome = $this->turn->getNotation();
$this->assertEquals($outcome, $notation);
}

/**
* @covers \Ouxsoft\LuckByDice\Turn::getLuck
*/
Expand Down Expand Up @@ -68,7 +87,7 @@ public function testSetLuck()
*/
public function testRoll()
{
$this->turn->notation->set('1d4,2d5,6d6+3,d5*2');
$this->turn->setNotation('1d4,2d5,6d6+3,d5*2');
$outcome = $this->turn->roll();
$this->assertIsInt($outcome);
}
Expand All @@ -78,19 +97,11 @@ public function testRoll()
*/
public function testGetCup()
{
$this->turn->notation->set('1d4,2d5,6d6+3,d5*2');
$this->turn->setNotation('1d4,2d5,6d6+3,d5*2');
$this->turn->roll();
$this->assertInstanceOf(Cup::class, $this->turn->getCup());
}

/**
* @covers \Ouxsoft\LuckByDice\Turn::getNotation
*/
public function testGetNotation()
{
$this->assertIsString($this->turn->getNotation());
}

/**
* @covers \Ouxsoft\LuckByDice\Turn::getTotal
*/
Expand Down

0 comments on commit 84aae39

Please sign in to comment.