Last active
June 15, 2020 12:49
-
-
Save JeffreyWay/4060514308c0a845af2b99fc2618204e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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