Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay JeffreyWay/FizzBuzz.php
Last active Jan 23, 2020

Embed
What would you like to do?
<?php
namespace App;
class FizzBuzz
{
public static function convert(int $number)
{
$result = '';
if ($number % 3 === 0) {
$result .= 'fizz';
}
if ($number % 5 === 0) {
$result .= 'buzz';
}
return $result ?: $number;
}
}
<?php
use App\FizzBuzz;
use PHPUnit\Framework\TestCase;
class FizzBuzzTest extends TestCase
{
/** @test */
function it_returns_fizz_for_multiples_of_three()
{
foreach ([3, 6, 9, 12] as $number) {
$this->assertEquals('fizz', FizzBuzz::convert($number));
}
}
/** @test */
function it_returns_buzz_for_multiples_of_five()
{
foreach ([5, 10, 20, 25] as $number) {
$this->assertEquals('buzz', FizzBuzz::convert($number));
}
}
/** @test */
function it_returns_fizzbuzz_for_multiples_of_three_and_five()
{
foreach ([15, 30, 45, 60] as $number) {
$this->assertEquals('fizzbuzz', FizzBuzz::convert($number));
}
}
/** @test */
function it_returns_the_original_number_if_not_divisible_by_three_or_five()
{
foreach ([1, 2, 4, 7, 8, 11] as $number) {
$this->assertEquals($number, FizzBuzz::convert($number));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.