Back to home

Test FileFizzBuzzRunning Tests
PHP Fizzbuzz main image

PHP Fizzbuzz

This expects an installation on the system of phpunit.

Test File

Create fizz-buzz_test.php:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <?php require "fizz-buzz.php"; class FizzBuzzTest extends PHPUnit\Framework\TestCase { public function testFizz() { $this->assertEquals('Fizz', fizzBuzz(3)); } public function testBuzz() { $this->assertEquals('Buzz', fizzBuzz(5)); } public function testFizzBuzz() { $this->assertEquals('FizzBuzz', fizzBuzz(15)); } public function testReturnsInt() { $this->assertEquals(2, fizzBuzz(2)); } }

FizzBuzz

Create fizz-buzz.php:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <?php function fizzBuzz($i) { switch(true) { case ($i % 3 == 0 && $i % 5 == 0): return "FizzBuzz"; case ($i % 3 == 0): return "Fizz"; case ($i % 5 == 0): return "Buzz"; default: return $i; } }

Running Tests

Change into directory and run phpunit.phar file_test.php.

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.