Skip to content

Commit

Permalink
refactor: Renamed fraction digit constants for clarity and updated me…
Browse files Browse the repository at this point in the history
…thod name.
  • Loading branch information
gustavofreze committed Oct 4, 2024
1 parent 82c0367 commit 7a37259
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 78 deletions.
14 changes: 14 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/tests export-ignore
/vendor export-ignore

/LICENSE export-ignore
/Makefile export-ignore
/README.md export-ignore
/phpmd.xml export-ignore
/phpunit.xml export-ignore
/phpstan.neon.dist export-ignore
/infection.json.dist export-ignore

/.github export-ignore
/.gitignore export-ignore
/.gitattributes export-ignore
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Use PHP 8.2
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'

- name: Install dependencies
run: composer update --no-progress --optimize-autoloader

Expand All @@ -33,6 +38,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Use PHP 8.2
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'

- name: Install dependencies
run: composer update --no-progress --optimize-autoloader

Expand Down
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
DOCKER_RUN = docker run --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/php:8.2

.PHONY: configure test test-no-coverage review show-reports clean
.PHONY: configure test test-file test-no-coverage review show-reports clean

configure:
@${DOCKER_RUN} composer update --optimize-autoloader

test: review
test:
@${DOCKER_RUN} composer tests

test-no-coverage: review
test-file:
@${DOCKER_RUN} composer tests-file-no-coverage ${FILE}

test-no-coverage:
@${DOCKER_RUN} composer tests-no-coverage

review:
Expand All @@ -19,4 +22,4 @@ show-reports:

clean:
@sudo chown -R ${USER}:${USER} ${PWD}
@rm -rf report vendor
@rm -rf report vendor .phpunit.cache
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ get the default amount fraction digits for the respective currency.
```php
$currency = Currency::USD;

$currency->name; # USD
$currency->value; # USD
$currency->getDefaultFractionDigits(); # 2
$currency->name; # USD
$currency->value; # USD
$currency->getFractionDigits(); # 2
```

<div id='license'></div>
Expand Down
21 changes: 13 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
"keywords": [
"vo",
"psr",
"psr-4",
"psr-12",
"currency",
"iso-4217",
"tiny-blocks",
Expand All @@ -22,6 +20,10 @@
"homepage": "https://github.com/gustavofreze"
}
],
"support": {
"issues": "https://github.com/tiny-blocks/currency/issues",
"source": "https://github.com/tiny-blocks/currency"
},
"config": {
"sort-packages": true,
"allow-plugins": {
Expand All @@ -39,24 +41,27 @@
}
},
"require": {
"php": "^8.1||^8.2"
"php": "^8.2"
},
"require-dev": {
"infection/infection": "^0.26",
"phpmd/phpmd": "^2.13",
"phpunit/phpunit": "^9.6",
"squizlabs/php_codesniffer": "^3.7"
"phpmd/phpmd": "^2.15",
"phpunit/phpunit": "^11",
"phpstan/phpstan": "^1",
"infection/infection": "^0.29",
"squizlabs/php_codesniffer": "^3.10"
},
"scripts": {
"phpcs": "phpcs --standard=PSR12 --extensions=php ./src",
"phpmd": "phpmd ./src text phpmd.xml --suffixes php --exclude /src/Currency.php --ignore-violations-on-exit",
"phpstan": "phpstan analyse -c phpstan.neon.dist --quiet --no-progress",
"test": "phpunit --log-junit=report/coverage/junit.xml --coverage-xml=report/coverage/coverage-xml --coverage-html=report/coverage/coverage-html tests",
"test-mutation": "infection --only-covered --logger-html=report/coverage/mutation-report.html --coverage=report/coverage --min-msi=100 --min-covered-msi=100 --threads=4",
"test-no-coverage": "phpunit --no-coverage",
"test-mutation-no-coverage": "infection --only-covered --min-msi=100 --threads=4",
"review": [
"@phpcs",
"@phpmd"
"@phpmd",
"@phpstan"
],
"tests": [
"@test",
Expand Down
11 changes: 5 additions & 6 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
{
"timeout": 10,
"testFramework": "phpunit",
"tmpDir": "report/",
"tmpDir": "report/infection/",
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "report/logs/infection-text.log",
"summary": "report/logs/infection-summary.log"
"text": "report/infection/logs/infection-text.log",
"summary": "report/infection/logs/infection-summary.log"
},
"mutators": {
"@default": true,
"MatchArmRemoval": false
"@default": true
},
"phpUnit": {
"configDir": "",
"customPath": "./vendor/bin/phpunit"
}
}
}
7 changes: 7 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
paths:
- src
level: 9
tmpDir: report/phpstan
ignoreErrors:
reportUnmatchedIgnoredErrors: false
38 changes: 24 additions & 14 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
cacheResultFile="report/.phpunit.result.cache"
backupGlobals="false"
backupStaticAttributes="false"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
cacheDirectory=".phpunit.cache"
beStrictAboutOutputDuringTests="true">

<source>
<include>
<directory>src</directory>
</include>
</source>

<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<text outputFile="report/coverage.txt"/>
<html outputDirectory="report/html/"/>
<clover outputFile="report/coverage-clover.xml"/>
</report>
</coverage>

<logging>
<junit outputFile="report/execution-result.xml"/>
</logging>

</phpunit>
17 changes: 12 additions & 5 deletions src/Currency.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Currency;

/**
Expand Down Expand Up @@ -180,17 +182,22 @@ enum Currency: string
case ZMW = 'ZMW';
case ZWL = 'ZWL';

public function getDefaultFractionDigits(): int
private const FRACTION_DIGITS_TWO = 2;
private const FRACTION_DIGITS_ZERO = 0;
private const FRACTION_DIGITS_FOUR = 4;
private const FRACTION_DIGITS_THREE = 3;

public function getFractionDigits(): int
{
return match ($this) {
Currency::BIF, Currency::CLP, Currency::DJF, Currency::GNF, Currency::ISK,
Currency::JPY, Currency::KMF, Currency::KRW, Currency::PYG, Currency::RWF,
Currency::UGX, Currency::UYI, Currency::VND, Currency::VUV, Currency::XAF,
Currency::XOF, Currency::XPF => 0,
Currency::XOF, Currency::XPF => self::FRACTION_DIGITS_ZERO,
Currency::BHD, Currency::IQD, Currency::JOD, Currency::KWD, Currency::LYD,
Currency::OMR, Currency::TND => 3,
Currency::CLF, Currency::UYW, => 4,
default => 2
Currency::OMR, Currency::TND => self::FRACTION_DIGITS_THREE,
Currency::CLF, Currency::UYW, => self::FRACTION_DIGITS_FOUR,
default => self::FRACTION_DIGITS_TWO
};
}
}
79 changes: 41 additions & 38 deletions tests/CurrencyTest.php
Original file line number Diff line number Diff line change
@@ -1,65 +1,68 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Currency;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class CurrencyTest extends TestCase
{
/**
* @dataProvider providerForTestValidNameAndValue
*/
#[DataProvider('currenciesDataProvider')]
public function testValidNameAndValue(string $name, string $value): void
{
self::assertEquals(3, strlen($name));
self::assertEquals(3, strlen($value));
self::assertSame(3, strlen($name));
self::assertSame(3, strlen($value));
}

/**
* @dataProvider providerForTestGetDefaultFractionDigits
*/
public function testGetDefaultFractionDigits(int $expected, Currency $currency): void
#[DataProvider('fractionDigitsDataProvider')]
public function testGetFractionDigits(int $expected, Currency $currency): void
{
$actual = $currency->getDefaultFractionDigits();
$actual = $currency->getFractionDigits();

self::assertEquals($expected, $actual);
self::assertSame($expected, $actual);
}

public function providerForTestValidNameAndValue(): array
public static function currenciesDataProvider(): array
{
return array_map(fn(Currency $currency) => [
return array_map(static fn(Currency $currency): array => [
'name' => $currency->name,
'value' => $currency->value
], Currency::cases());
}

public function providerForTestGetDefaultFractionDigits(): array
public static function fractionDigitsDataProvider(): array
{
return [
[
'expected' => 0,
'currency' => Currency::CLP,
],
[
'expected' => 2,
'currency' => Currency::BRL,
],
[
'expected' => 2,
'currency' => Currency::USD,
],
[
'expected' => 2,
'currency' => Currency::EUR,
],
[
'expected' => 3,
'currency' => Currency::KWD,
],
[
'expected' => 4,
'currency' => Currency::CLF
]
'Currency BIF with digits 0' => ['currency' => Currency::BIF, 'expected' => 0],
'Currency CLP with digits 0' => ['currency' => Currency::CLP, 'expected' => 0],
'Currency DJF with digits 0' => ['currency' => Currency::DJF, 'expected' => 0],
'Currency GNF with digits 0' => ['currency' => Currency::GNF, 'expected' => 0],
'Currency ISK with digits 0' => ['currency' => Currency::ISK, 'expected' => 0],
'Currency JPY with digits 0' => ['currency' => Currency::JPY, 'expected' => 0],
'Currency KMF with digits 0' => ['currency' => Currency::KMF, 'expected' => 0],
'Currency KRW with digits 0' => ['currency' => Currency::KRW, 'expected' => 0],
'Currency PYG with digits 0' => ['currency' => Currency::PYG, 'expected' => 0],
'Currency RWF with digits 0' => ['currency' => Currency::RWF, 'expected' => 0],
'Currency UGX with digits 0' => ['currency' => Currency::UGX, 'expected' => 0],
'Currency UYI with digits 0' => ['currency' => Currency::UYI, 'expected' => 0],
'Currency VND with digits 0' => ['currency' => Currency::VND, 'expected' => 0],
'Currency VUV with digits 0' => ['currency' => Currency::VUV, 'expected' => 0],
'Currency XAF with digits 0' => ['currency' => Currency::XAF, 'expected' => 0],
'Currency XOF with digits 0' => ['currency' => Currency::XOF, 'expected' => 0],
'Currency XPF with digits 0' => ['currency' => Currency::XPF, 'expected' => 0],
'Currency USD with digits 2' => ['currency' => Currency::USD, 'expected' => 2],
'Currency EUR with digits 2' => ['currency' => Currency::EUR, 'expected' => 2],
'Currency BHD with digits 3' => ['currency' => Currency::BHD, 'expected' => 3],
'Currency IQD with digits 3' => ['currency' => Currency::IQD, 'expected' => 3],
'Currency JOD with digits 3' => ['currency' => Currency::JOD, 'expected' => 3],
'Currency KWD with digits 3' => ['currency' => Currency::KWD, 'expected' => 3],
'Currency LYD with digits 3' => ['currency' => Currency::LYD, 'expected' => 3],
'Currency OMR with digits 3' => ['currency' => Currency::OMR, 'expected' => 3],
'Currency TND with digits 3' => ['currency' => Currency::TND, 'expected' => 3],
'Currency CLF with digits 4' => ['currency' => Currency::CLF, 'expected' => 4],
'Currency UYW with digits 4' => ['currency' => Currency::UYW, 'expected' => 4]
];
}
}

0 comments on commit 7a37259

Please sign in to comment.