PHP Fizzbuzz

Published: Jul 4, 2018

Last updated: Jul 4, 2018

This expects an installation on the system of phpunit.

Test File

Create fizz-buzz_test.php:

<?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:

<?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.

Personal image

Dennis O'Keeffe

Byron Bay, Australia

Share this post

Recommended articles

Dennis O'Keeffe

2020-present Dennis O'Keeffe.

All Rights Reserved.