Created
May 8, 2020 12:40
-
-
Save nawarian/fc54755ad875198b1f48ab1afa0f5299 to your computer and use it in GitHub Desktop.
A simple test class checking how a FizzBuzz function should behave.
This file contains hidden or 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 | |
declare(strict_types=1); | |
use PHPUnit\Framework\TestCase; | |
class FizzBuzzTest extends TestCase | |
{ | |
/** | |
* @dataProvider fizzBuzzHappyCaseDataProvider | |
*/ | |
public function testFizzBuzzHappyCases(int $start, int $end, string $expectedOutput): void | |
{ | |
self::assertEquals($expectedOutput, fizzBuzz($start, $end)); | |
} | |
public function fizzBuzzHappyCaseDataProvider(): array | |
{ | |
return [ | |
// Multiples of three become "Fizz" | |
[1, 4, '1,2,Fizz,4'], | |
// Multiples of five become "Buzz" | |
[1,5, '1,2,Fizz,4,Buzz'], | |
// Multiples of 15 become "Fizz Buzz" | |
[1, 20, '1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz,11,Fizz,13,14,Fizz Buzz,16,17,Fizz,19,Buzz'], | |
]; | |
} | |
/** | |
* @dataProvider fizzBuzzFaultyCaseDataProvider | |
*/ | |
public function testFizzBuzzExceptions( | |
int $start, | |
int $end, | |
string $expectedException, | |
string $expectedExceptionMessage | |
): void { | |
self::expectExceptionMessage($expectedExceptionMessage); | |
self::expectException($expectedException); | |
fizzBuzz($start, $end); | |
} | |
public function fizzBuzzFaultyCaseDataProvider(): array | |
{ | |
return [ | |
// Negative ranges are impossible | |
[1, -10, InvalidArgumentException::class, 'Invalid range: $end (-10) must be greater than $start (1).'], | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A possible implementation: