-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: phpstan,php-cs-fixer config, Unit test
- Loading branch information
Showing
11 changed files
with
256 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# EditorConfig is awesome: https://EditorConfig.org | ||
|
||
# top-most EditorConfig file | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 4 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/tests export-ignore | ||
/.editorconfig export-ignore | ||
/.php-cs-fixer.dist.php export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/phpstan.neon.dist export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: PHP Composer | ||
|
||
on: | ||
push: | ||
branches: [ "1.x" ] | ||
pull_request: | ||
branches: [ "1.x" ] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Validate composer.json and composer.lock | ||
run: composer validate --strict | ||
|
||
- name: Cache Composer packages | ||
id: composer-cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: vendor | ||
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-php- | ||
- name: Install dependencies | ||
run: composer install --prefer-dist --no-progress | ||
|
||
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" | ||
# Docs: https://getcomposer.org/doc/articles/scripts.md | ||
|
||
# - name: Run test suite | ||
# run: composer run-script test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/vendor/ | ||
/composer.lock | ||
/bin/* | ||
/phpunit.xml | ||
/.phpunit.cache/ | ||
/.php-cs-fixer.php | ||
/.php-cs-fixer.cache | ||
/phpstan.neon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
$header = <<<HEADER | ||
This file is part of holistic-agency/dice-roll. | ||
(c) JamesRezo <[email protected]> | ||
For the full copyright and license information, please view the LICENSE | ||
file that was distributed with this source code. | ||
HEADER; | ||
|
||
$finder = PhpCsFixer\Finder::create() | ||
->in([__DIR__ . '/src', __DIR__ . '/tests']) | ||
; | ||
|
||
$config = new PhpCsFixer\Config(); | ||
return $config->setRules([ | ||
'@PER-CS2.0' => true, | ||
'strict_param' => true, | ||
'array_syntax' => ['syntax' => 'short'], | ||
'header_comment' => ['header' => $header], | ||
]) | ||
->setFinder($finder) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"files.exclude": { | ||
"**/.git": true, | ||
"**/.svn": true, | ||
"**/.hg": true, | ||
"**/CVS": true, | ||
"**/.DS_Store": true, | ||
"**/Thumbs.db": true, | ||
".mule": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
} | ||
}, | ||
"require-dev": { | ||
"symfony/var-dumper": "^7.1" | ||
"symfony/var-dumper": "^7.1", | ||
"phpunit/phpunit": "^11.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
parameters: | ||
phpVersion: 080100 | ||
paths: | ||
- src | ||
level: max |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
cacheDirectory=".phpunit.cache" | ||
executionOrder="depends,defects" | ||
shortenArraysForExportThreshold="10" | ||
requireCoverageMetadata="true" | ||
beStrictAboutCoverageMetadata="true" | ||
beStrictAboutOutputDuringTests="true" | ||
displayDetailsOnPhpunitDeprecations="true" | ||
failOnPhpunitDeprecation="true" | ||
failOnRisky="true" | ||
failOnWarning="true"> | ||
<testsuites> | ||
<testsuite name="default"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<coverage> | ||
<report> | ||
<html outputDirectory=".phpunit.cache/html"/> | ||
<text outputFile="php://stdout"/> | ||
</report> | ||
</coverage> | ||
|
||
<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true"> | ||
<include> | ||
<directory>src</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of DiceRoll. | ||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of holistic-agency/dice-roll. | ||
* | ||
* (c) JamesRezo <[email protected]> | ||
* | ||
|
@@ -58,9 +60,9 @@ private function parseFormula(string $formula): void | |
if (!\preg_match( | ||
',^(?<number>\d*)D(?<faces>\d*)(?<modifier>[+-]\d+)?$,i', | ||
$formula, | ||
$matches | ||
$matches, | ||
)) { | ||
throw new \LogicException('Bad formula "'.$formula.'".'); | ||
throw new \LogicException('Bad formula "' . $formula . '".'); | ||
} | ||
|
||
$this->number = !empty($matches['number']) ? (int) $matches['number'] : self::DEFAULT_NUMBER; | ||
|
@@ -71,13 +73,13 @@ private function parseFormula(string $formula): void | |
public function roll(): int | ||
{ | ||
$rolls = []; | ||
for ($times=0; $times < $this->number; $times++) { | ||
for ($times = 0; $times < $this->number; $times++) { | ||
$rolls[] = $this->randomizer->random(); | ||
} | ||
\sort($rolls, \SORT_NUMERIC); | ||
|
||
if (!\is_null($this->bestOfNumber)) { | ||
$rolls = \array_slice($rolls, -$this->bestOfNumber, $this->bestOfNumber); | ||
$rolls = \array_slice($rolls, -$this->bestOfNumber, $this->bestOfNumber); | ||
} | ||
if (!\is_null($this->leastOfNumber)) { | ||
$rolls = \array_slice($rolls, 0, $this->leastOfNumber); | ||
|
@@ -103,22 +105,22 @@ public function __toString(): string | |
public function bestOf(int $number): self | ||
{ | ||
if ($number > $this->number || $number < 1) { | ||
throw new \LogicException('Bad number "'.$number.'".'); | ||
throw new \LogicException('Bad number "' . $number . '".'); | ||
} | ||
|
||
$dice = clone $this; | ||
$dice->bestOfNumber = $number; | ||
$dice->leastOfNumber = \null; | ||
|
||
return $dice; | ||
} | ||
|
||
public function leastOf(int $number = 1): self | ||
{ | ||
if ($number > $this->number || $number < 1) { | ||
throw new \LogicException('Bad number "'.$number.'".'); | ||
throw new \LogicException('Bad number "' . $number . '".'); | ||
} | ||
|
||
$dice = clone $this; | ||
$dice->bestOfNumber = \null; | ||
$dice->leastOfNumber = $number; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of holistic-agency/dice-roll. | ||
* | ||
* (c) JamesRezo <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace HolisticAgency\Test\DiceRoll; | ||
|
||
use HolisticAgency\Decouple\Frozen\Randomizer; | ||
use HolisticAgency\DiceRoll\Dice; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(Dice::class)] | ||
class DiceTest extends TestCase | ||
{ | ||
private Randomizer $randomizer; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->randomizer = new Randomizer(2); | ||
} | ||
|
||
public function testRoll() | ||
{ | ||
// Given | ||
$dice = new Dice('2D6-2', $this->randomizer); | ||
|
||
// When | ||
$actual = $dice->roll(); // 2+2-2 | ||
|
||
// Then | ||
$this->assertSame(2, $actual); | ||
} | ||
|
||
public function testBadFormula() | ||
{ | ||
// Given | ||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage('Bad formula "test".'); | ||
|
||
// When | ||
new Dice('test'); | ||
|
||
// Then | ||
// An exception is thrown | ||
} | ||
|
||
public function testBestOfRoll() | ||
{ | ||
// Given | ||
$dice = (new Dice('4D6', $this->randomizer))->bestOf(3); | ||
|
||
// When | ||
$actual = $dice->roll(); | ||
|
||
// Then | ||
$this->assertSame(6, $actual); | ||
$this->assertSame('3 best of 4D6', (string) $dice); | ||
} | ||
|
||
public function testLeastOfRoll() | ||
{ | ||
// Given | ||
$dice = (new Dice('2D20', $this->randomizer))->leastOf(1); | ||
|
||
// When | ||
$actual = $dice->roll(); | ||
|
||
// Then | ||
$this->assertSame(2, $actual); | ||
$this->assertSame('1 least of 2D20', (string) $dice); | ||
} | ||
|
||
public function testBadBestNumber() | ||
{ | ||
// Given | ||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage('Bad number "0".'); | ||
|
||
// When | ||
(new Dice('D'))->bestOf(0); | ||
|
||
// Then | ||
// An exception is thrown | ||
} | ||
|
||
public function testBadLeastNumber() | ||
{ | ||
// Given | ||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage('Bad number "2".'); | ||
|
||
// When | ||
(new Dice('D'))->leastOf(2); | ||
|
||
// Then | ||
// An exception is thrown | ||
} | ||
} |