Download composer.phar from https://getcomposer.org/
curl -sS https://getcomposer.org/installer | php
php composer.phar install
vendor/bin/phpunit RomanNumeralsTest.php
curl -sS https://getcomposer.org/installer | php
php composer.phar install
vendor/bin/phpunit RomanNumeralsTest.php
| { | |
| "require-dev": { | |
| "phpunit/phpunit": "4.*" | |
| }, | |
| "autoload": { | |
| "files": ["RomanNumerals.php"] | |
| } | |
| } |
| <?php | |
| class RomanNumerals | |
| { | |
| public function arabicToRoman($arabicNumber) | |
| { | |
| $roman = ''; | |
| $chars = [ | |
| 'M' => 1000, | |
| 'CM' => 900, | |
| 'D' => 500, | |
| 'CD' => 400, | |
| 'C' => 100, | |
| 'XC' => 90, | |
| 'L' => 50, | |
| 'XL' => 40, | |
| 'X' => 10, | |
| 'IX' => 9, | |
| 'V' => 5, | |
| 'IV' => 4, | |
| 'I' => 1, | |
| ]; | |
| foreach ($chars as $c => $n) { | |
| while ($arabicNumber >= $n) { | |
| $roman .= $c; | |
| $arabicNumber -= $n; | |
| } | |
| } | |
| return $roman; | |
| } | |
| } |
| <?php | |
| // Composer genereated autoloader | |
| require __DIR__.'/vendor/autoload.php'; | |
| class RomanNumeralsTest extends PHPUnit_Framework_TestCase | |
| { | |
| public function testDataProvider() | |
| { | |
| return array( | |
| array(1, 'I'), | |
| array(2, 'II'), | |
| array(3, 'III'), | |
| array(4, 'IV'), | |
| array(5, 'V'), | |
| array(6, 'VI'), | |
| array(7, 'VII'), | |
| array(8, 'VIII'), | |
| array(9, 'IX'), | |
| array(10, 'X'), | |
| array(12, 'XII'), | |
| array(66, 'LXVI'), | |
| array(1970, 'MCMLXX'), | |
| array(2000, 'MM'), | |
| ); | |
| } | |
| /** | |
| * @dataProvider testDataProvider | |
| */ | |
| public function testArabicToRoman1($number, $result) | |
| { | |
| $Roman = new RomanNumerals(); | |
| $this->assertEquals($result, $Roman->arabicToRoman($number)); | |
| } | |
| } |