Back to home

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

    @dennisokeeffe92
    • Melbourne, Australia

    Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
    I am currently working on Visibuild.

    1,200+ PEOPLE ALREADY JOINED ❤️️

    Get fresh posts + news direct to your inbox.

    No spam. We only send you relevant content.

    PHP Fizzbuzz

    Introduction

    Share this post