Move all files to php/phpunit/

This commit is contained in:
Oliver Davies 2025-09-29 23:09:31 +01:00
parent 533bcaf826
commit f6bf98bf1e
9 changed files with 0 additions and 0 deletions

31
php/phpunit/.github/workflows/main.yml vendored Normal file
View file

@ -0,0 +1,31 @@
---
name: Run tests
on:
push:
jobs:
phpunit:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['7.4']
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Configure PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-composer-${{ hashFiles('composer.json') }}
- name: Run tests
run: make test

8
php/phpunit/.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
*
!/*
!/.github/**
!/composer.*
!/phpunit.xml
!/README.md
!/src/**
!/tests/**

19
php/phpunit/Makefile Normal file
View file

@ -0,0 +1,19 @@
CLEAN_PATHS=vendor
PHPUNIT_PATH=vendor/bin/phpunit
all: test
clean:
@for dir in $(CLEAN_PATHS); do \
rm -fr $$dir; \
done
phpunit: vendor
@$(PHPUNIT_PATH) --colors=always --testdox
test: phpunit
vendor: composer.json composer.lock
@composer install
.PHONY: all clean phpunit test

1
php/phpunit/README.md Normal file
View file

@ -0,0 +1 @@
# PHP katas with PHPUnit

23
php/phpunit/composer.json Normal file
View file

@ -0,0 +1,23 @@
{
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"beberlei/assert": "^3.2"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"config": {
"sort-packages": true
}
}

1867
php/phpunit/composer.lock generated Normal file

File diff suppressed because it is too large Load diff

12
php/phpunit/phpunit.xml Normal file
View file

@ -0,0 +1,12 @@
<?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"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>

View file

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace App;
use Assert\Assert;
final class RomanNumeralsConverter
{
private static $map = [
1000 => 'M',
900 => 'CM',
500 => 'D',
400 => 'CD',
100 => 'C',
90 => 'XC',
50 => 'L',
40 => 'XL',
10 => 'X',
9 => 'IX',
5 => 'V',
4 => 'IV',
3 => 'III',
2 => 'II',
1 => 'I',
];
public static function convert(int $input): string
{
Assert::that($input)
->greaterOrEqualThan(0, 'Cannot convert negative numbers');
$letters = '';
while ($input > 0) {
foreach (static::$map as $number => $letter) {
if ($input >= $number) {
// Add the appropriate numeral and reduce the value of
// $input accordingly.
$letters .= $letter;
$input = ($input - $number);
break;
}
}
}
return $letters;
}
}

View file

@ -0,0 +1,142 @@
<?php
declare(strict_types=1);
namespace App\Tests;
use App\RomanNumeralsConverter;
use Assert\AssertionFailedException;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
final class RomanNumeralsConverterTest extends TestCase
{
/**
* @test
* @dataProvider numeralProvider
*/
public function it_converts_a_number(int $number, string $expected): void
{
Assert::assertSame(
$expected,
RomanNumeralsConverter::convert($number)
);
}
public function numeralProvider(): array
{
return [
1 => [
'number' => 1,
'expected' => 'I',
],
2 => [
'number' => 2,
'expected' => 'II',
],
3 => [
'number' => 3,
'expected' => 'III',
],
4 => [
'number' => 4,
'expected' => 'IV',
],
5 => [
'number' => 5,
'expected' => 'V',
],
9 => [
'number' => 9,
'expected' => 'IX',
],
10 => [
'number' => 10,
'expected' => 'X',
],
15 => [
'number' => 15,
'expected' => 'XV',
],
19 => [
'number' => 19,
'expected' => 'XIX',
],
20 => [
'number' => 20,
'expected' => 'XX',
],
21 => [
'number' => 21,
'expected' => 'XXI',
],
40 => [
'number' => 40,
'expected' => 'XL',
],
50 => [
'number' => 50,
'expected' => 'L',
],
80 => [
'number' => 80,
'expected' => 'LXXX',
],
90 => [
'number' => 90,
'expected' => 'XC',
],
100 => [
'number' => 100,
'expected' => 'C',
],
110 => [
'number' => 110,
'expected' => 'CX',
],
400 => [
'number' => 400,
'expected' => 'CD',
],
500 => [
'number' => 500,
'expected' => 'D',
],
700 => [
'number' => 700,
'expected' => 'DCC',
],
900 => [
'number' => 900,
'expected' => 'CM',
],
1000 => [
'number' => 1000,
'expected' => 'M',
],
1986 => [
'number' => 1986,
'expected' => 'MCMLXXXVI',
],
1990 => [
'number' => 1990,
'expected' => 'MCMXC',
],
2020 => [
'number' => 2020,
'expected' => 'MMXX',
],
];
}
/**
* @test
*/
public function it_cannot_convert_negative_numbers(): void
{
$this->expectException(AssertionFailedException::class);
$this->expectExceptionMessage('Cannot convert negative numbers');
RomanNumeralsConverter::convert(-1);
}
}