Skip to content

Commit

Permalink
Merge pull request #15 from tiny-blocks/release/2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavofreze authored Dec 11, 2024
2 parents 43c156a + 859875c commit 37abf68
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 92 deletions.
32 changes: 13 additions & 19 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,55 +1,49 @@
name: CI

on:
push:
pull_request:

permissions:
contents: read

env:
PHP_VERSION: '8.3'

jobs:
auto-review:
name: Auto review
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Use PHP 8.2
- name: Configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
php-version: ${{ env.PHP_VERSION }}

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

- name: Run phpcs
run: composer phpcs

- name: Run phpmd
run: composer phpmd
- name: Run review
run: composer review

tests:
name: Tests
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Use PHP 8.2
- name: Use PHP ${{ env.PHP_VERSION }}
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
php-version: ${{ env.PHP_VERSION }}

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

- name: Run unit tests
env:
XDEBUG_MODE: coverage
run: composer test

- name: Run mutation tests
run: composer test-mutation
- name: Run tests
run: composer tests
12 changes: 3 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
DOCKER_RUN = docker run --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/php:8.2
DOCKER_RUN = docker run --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/php:8.3

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

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

test:
@${DOCKER_RUN} composer tests

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

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

review:
@${DOCKER_RUN} composer review

fix-style:
@${DOCKER_RUN} composer fix-style

show-reports:
@sensible-browser report/coverage/coverage-html/index.html report/coverage/mutation-report.html

clean:
@sudo chown -R ${USER}:${USER} ${PWD}
@rm -rf report vendor
@rm -rf report vendor .phpunit.cache .lock
19 changes: 7 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,33 @@
}
},
"require": {
"php": "^8.2",
"php": "^8.3",
"tiny-blocks/value-object": "^3"
},
"require-dev": {
"phpmd/phpmd": "^2.15",
"phpstan/phpstan": "^1",
"phpunit/phpunit": "^11",
"infection/infection": "^0.29",
"squizlabs/php_codesniffer": "^3.10"
"infection/infection": "^0",
"squizlabs/php_codesniffer": "^3"
},
"scripts": {
"test": "phpunit --configuration phpunit.xml tests",
"phpcs": "phpcs --standard=PSR12 --extensions=php ./src",
"phpmd": "phpmd ./src text phpmd.xml --suffixes php --exclude /src/Alpha2Code.php,/src/Alpha3Code.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",
"mutation-test": "infection --only-covered --threads=max --logger-html=report/coverage/mutation-report.html --coverage=report/coverage",
"test-no-coverage": "phpunit --configuration phpunit.xml --no-coverage tests",
"review": [
"@phpcs",
"@phpmd",
"@phpstan"
],
"tests": [
"@test",
"@test-mutation"
"@mutation-test"
],
"tests-no-coverage": [
"@test-no-coverage",
"@test-mutation-no-coverage"
],
"tests-file-no-coverage": [
"@test-no-coverage"
]
}
Expand Down
23 changes: 12 additions & 11 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
{
"timeout": 10,
"testFramework": "phpunit",
"logs": {
"text": "report/infection/logs/infection-text.log",
"summary": "report/infection/logs/infection-summary.log"
},
"tmpDir": "report/infection/",
"minMsi": 100,
"timeout": 30,
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "report/infection/logs/infection-text.log",
"summary": "report/infection/logs/infection-summary.log"
},
"mutators": {
"@default": true,
"PublicVisibility": false
},
"phpUnit": {
"configDir": "",
"customPath": "./vendor/bin/phpunit"
}
},
"mutators": {
"@default": true
},
"minCoveredMsi": 100,
"testFramework": "phpunit"
}
6 changes: 4 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
failOnRisky="true"
failOnWarning="true"
cacheDirectory=".phpunit.cache"
executionOrder="random"
beStrictAboutOutputDuringTests="true">

<source>
Expand All @@ -22,14 +23,15 @@

<coverage>
<report>
<xml outputDirectory="report/coverage/coverage-xml"/>
<html outputDirectory="report/coverage/coverage-html"/>
<text outputFile="report/coverage.txt"/>
<html outputDirectory="report/html/"/>
<clover outputFile="report/coverage-clover.xml"/>
</report>
</coverage>

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

</phpunit>
8 changes: 7 additions & 1 deletion src/Alpha2Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace TinyBlocks\Country;

use TinyBlocks\Country\Internal\AlphaCode;
use TinyBlocks\Country\Internal\AlphaCodeMapper;

/**
Expand All @@ -17,6 +16,8 @@ enum Alpha2Code: string implements AlphaCode
{
use AlphaCodeMapper;

public const int CODE_LENGTH = 2;

case AFGHANISTAN = 'AF';
case ALAND_ISLANDS = 'AX';
case ALBANIA = 'AL';
Expand Down Expand Up @@ -259,6 +260,11 @@ enum Alpha2Code: string implements AlphaCode
case ZIMBABWE = 'ZW';
case PALESTINE = 'PS';

public function getName(): string
{
return $this->name;
}

public function toAlpha3(): Alpha3Code
{
$value = $this->getBy(name: $this->name, alphaCodes: Alpha3Code::cases())->value;
Expand Down
8 changes: 7 additions & 1 deletion src/Alpha3Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace TinyBlocks\Country;

use TinyBlocks\Country\Internal\AlphaCode;
use TinyBlocks\Country\Internal\AlphaCodeMapper;

/**
Expand All @@ -18,6 +17,8 @@ enum Alpha3Code: string implements AlphaCode
{
use AlphaCodeMapper;

public const int CODE_LENGTH = 3;

case AFGHANISTAN = 'AFG';
case ALAND_ISLANDS = 'ALA';
case ALBANIA = 'ALB';
Expand Down Expand Up @@ -260,6 +261,11 @@ enum Alpha3Code: string implements AlphaCode
case ZIMBABWE = 'ZWE';
case PALESTINE = 'PSE';

public function getName(): string
{
return $this->name;
}

public function toAlpha2(): Alpha2Code
{
$value = $this->getBy(name: $this->name, alphaCodes: Alpha2Code::cases())->value;
Expand Down
18 changes: 18 additions & 0 deletions src/AlphaCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Country;

/**
* Defines the contract for classes representing alpha codes of countries.
*/
interface AlphaCode
{
/**
* Gets the name associated with the alpha code.
*
* @return string The name of the country or region corresponding to the code.
*/
public function getName(): string;
}
11 changes: 4 additions & 7 deletions src/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@

namespace TinyBlocks\Country;

use TinyBlocks\Country\Internal\AlphaCode;
use TinyBlocks\Country\Internal\Exceptions\InvalidAlphaCode;
use TinyBlocks\Country\Internal\Exceptions\InvalidAlphaCodeImplementation;
use TinyBlocks\Country\Internal\Name;
use TinyBlocks\Vo\ValueObject;
use TinyBlocks\Vo\ValueObjectBehavior;

final class Country implements ValueObject
final readonly class Country implements ValueObject
{
use ValueObjectBehavior;

private const ALPHA2_CODE_LENGTH = 2;

private function __construct(public string $name, public Alpha2Code $alpha2, public Alpha3Code $alpha3)
{
}

public static function from(AlphaCode $alphaCode, string|null $name = null): Country
public static function from(AlphaCode $alphaCode, ?string $name = null): Country
{
$name = empty($name)
? Name::fromAlphaCode(alphaCode: $alphaCode)->value
Expand All @@ -38,9 +35,9 @@ public static function from(AlphaCode $alphaCode, string|null $name = null): Cou
throw new InvalidAlphaCodeImplementation(class: $alphaCode::class);
}

public static function fromString(string $alphaCode, string|null $name = null): Country
public static function fromString(string $alphaCode, ?string $name = null): Country
{
$alphaCodeFrom = strlen($alphaCode) === self::ALPHA2_CODE_LENGTH
$alphaCodeFrom = strlen($alphaCode) === Alpha2Code::CODE_LENGTH
? Alpha2Code::tryFrom(value: $alphaCode)
: Alpha3Code::tryFrom(value: $alphaCode);

Expand Down
21 changes: 0 additions & 21 deletions src/Internal/AlphaCode.php

This file was deleted.

1 change: 1 addition & 0 deletions src/Internal/Exceptions/AlphaCodeNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class AlphaCodeNotFound extends RuntimeException
public function __construct(string $name)
{
$template = 'Alpha code with name <%s> not found.';

parent::__construct(message: sprintf($template, $name));
}
}
1 change: 1 addition & 0 deletions src/Internal/Exceptions/InvalidAlphaCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class InvalidAlphaCode extends RuntimeException
public function __construct(string $alphaCode)
{
$template = 'Alpha code <%s> is invalid.';

parent::__construct(message: sprintf($template, $alphaCode));
}
}
1 change: 1 addition & 0 deletions src/Internal/Exceptions/InvalidAlphaCodeImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class InvalidAlphaCodeImplementation extends RuntimeException
public function __construct(string $class)
{
$template = 'The alpha code implementation <%s> is invalid.';

parent::__construct(message: sprintf($template, $class));
}
}
Loading

0 comments on commit 37abf68

Please sign in to comment.