- php >= 7.1
- composer
Create a new directory and cd into it:
mkdir roman-numerals
cd roman-numerals
mkdir src
mkdir tests
Create composer.json
composer --init
Accept default options. Don't add any packages just yet.
Update composer.json
to include autoload definition:
"autoload" : {
"psr-4:" {
"Kata\\RomanNumeral\\" : "src/"
}
}
Now add dev requirement for phpunit:
composer require --dev phpunit/phpunit
Generate the default PHPUnit configuration phpunit.xml
:
vendor/bin/phpunit --generate-configuration
Create class for software under test src/Converter.php
<?php
declare(strict_types=1);
namespace Kata\RomanNumeral;
class Converter
{
public function convert(int $number): string
{
return '';
}
}
And initial test:
<?php
declare(strict_types=1);
namespace Kata\RomanNumeral\Test;
use Kata\RomanNumeral\Converter;
use PHPUnit\Framework\TestCase;
class ConverterTest extends TestCase
{
public function test1(): void
{
$converter = new Converter();
$this->assertEquals('I', $converter->convert(1));
}
}
Run tests:
vendor/bin/phpunit
Should get an failure like this:
PHPUnit 7.5.2 by Sebastian Bergmann and contributors.
Runtime: PHP 7.2.10-0ubuntu0.18.04.1
Configuration: /vagrant/roman-numerals/phpunit.xml
F 1 / 1 (100%)
Time: 184 ms, Memory: 4.00MB
There was 1 failure:
1) Kata\RomanNumeral\Test\ConverterTest::test1
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'I'
+''
/vagrant/roman-numerals/tests/ConverterTest.php:19
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Now complete the exercise. Using TDD write code that converts any integer value from 1 to 4999 into its Roman numeral.
Extra challenge:
- Handle the case where an integer outside this range is supplied.