Skip to content

Instantly share code, notes, and snippets.

@DaveLiddament
Last active January 16, 2019 14:00
Show Gist options
  • Save DaveLiddament/6fca517b4412317f7c4403b767cb451d to your computer and use it in GitHub Desktop.
Save DaveLiddament/6fca517b4412317f7c4403b767cb451d to your computer and use it in GitHub Desktop.
Roman Numeral Kata setup

Roman Numeral Kata

Requirements:

  • php >= 7.1
  • composer

Setup

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.

Challenge

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment