Skip to content

Commit

Permalink
test: phpstan,php-cs-fixer config, Unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesRezo committed Oct 28, 2024
1 parent e16bf09 commit f211dee
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 12 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
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
5 changes: 5 additions & 0 deletions .gitattributes
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
39 changes: 39 additions & 0 deletions .github/workflows/php.yml
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
5 changes: 5 additions & 0 deletions .gitignore
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
24 changes: 24 additions & 0 deletions .php-cs-fixer.dist.php
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)
;
11 changes: 11 additions & 0 deletions .vscode/settings.json
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
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
}
},
"require-dev": {
"symfony/var-dumper": "^7.1"
"symfony/var-dumper": "^7.1",
"phpunit/phpunit": "^11.4"
}
}
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
phpVersion: 080100
paths:
- src
level: max
33 changes: 33 additions & 0 deletions phpunit.xml.dist
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>
24 changes: 13 additions & 11 deletions src/Dice.php
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]>
*
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down
107 changes: 107 additions & 0 deletions tests/DiceTest.php
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
}
}

0 comments on commit f211dee

Please sign in to comment.