Skip to content

Instantly share code, notes, and snippets.

@roongr2k7
Last active August 3, 2016 14:09
Show Gist options
  • Save roongr2k7/8070251 to your computer and use it in GitHub Desktop.
Save roongr2k7/8070251 to your computer and use it in GitHub Desktop.
<?php
class FizzBuzz {
function toString($number) {
$modThreeIsZero = $number % 3 == 0;
$modFiveIsZero = $number % 5 == 0;
$fizzWhen = ['', 'Fizz'];
$numberUnless = [$number, ''];
$buzzWhen = [$numberUnless[$modThreeIsZero], 'Buzz'];
return $fizzWhen[$modThreeIsZero] . $buzzWhen[$modFiveIsZero];
}
}
<?php
require_once 'FizzBuzz.php';
class FizzBuzzTest extends \PHPUnit_Framework_TestCase {
private $fizzbuzz;
function setUp() {
$this->fizzbuzz = new FizzBuzz();
}
function testFizz() {
$result = $this->fizzbuzz->toString(3);
$this->assertEquals('Fizz', $result);
}
function testBuzz() {
$result = $this->fizzbuzz->toString(5);
$this->assertEquals('Buzz', $result);
}
function testFizzBuzz() {
$result = $this->fizzbuzz->toString(15);
$this->assertEquals('FizzBuzz', $result);
}
function testAnother() {
$result = $this->fizzbuzz->toString(1);
$this->assertEquals('1', $result);
}
}
@['Fizz'][$n % 3] . [@[$n][$n % 3 == 0], 'Buzz'][$n % 5 == 0];
Copy link

ghost commented Dec 21, 2013

Clever!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment