Last active
August 29, 2015 14:20
-
-
Save bassplayer7/28573f9e7c98d577fe7a to your computer and use it in GitHub Desktop.
PHP User Group Kansas City May 6 - FizzBuzz using a switch statement
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 | |
class FizzBuzz | |
{ | |
private $index; | |
private $numbers; | |
private $fizz; | |
private $buzz; | |
private $fizzbuzz; | |
public function __construct() | |
{ | |
$this->index = 0; | |
$this->numbers = range(0, 100); | |
$this->fizz = range(0, 100, 3); | |
$this->buzz = range(0, 100, 5); | |
$this->fizzbuzz = range(0, 100, 15); | |
} | |
public function runFizzBuzz() | |
{ | |
array_map(array($this, 'testNumberSwitch'), $this->numbers); | |
} | |
protected function testFizz($index) | |
{ | |
return in_array($index, $this->fizz); | |
} | |
protected function testBuzz($index) | |
{ | |
return in_array($index, $this->buzz); | |
} | |
protected function testFizzBuzz($index) | |
{ | |
return in_array($index, $this->fizzbuzz); | |
} | |
protected function testNumberSwitch() | |
{ | |
switch ($this->index) { | |
default: | |
echo $this->index . "\r\n"; | |
break; | |
case $this->testFizzBuzz($this->index) && ($this->index % 15) === 0: | |
echo 'FizzBuzz' . "\r\n"; | |
break; | |
case $this->testFizz($this->index) && ($this->index % 3) === 0: | |
echo 'Fizz' . "\r\n"; | |
break; | |
case $this->testBuzz($this->index) && ($this->index % 5) === 0: | |
echo 'Buzz'. "\r\n"; | |
break; | |
} | |
$this->index++; | |
} | |
} | |
$fizzBuzz = new FizzBuzz(); | |
$fizzBuzz->runFizzBuzz(); |
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
// This was a very rudimentry start but since I hadn't gotten to finishing, I'm posting it now. | |
require(__DIR__ . '/../FizzBuzz.php'); | |
class FizzBuzzTest extends PHPUnit_Framework_TestCase | |
{ | |
private $fizzBuzzOutput; | |
public function __construct() | |
{ | |
$fizzBuzz = new FizzBuzz(); | |
ob_start(); | |
$fizzBuzz->runFizzBuzz(); | |
$this->fizzBuzzOutput = ob_get_contents(); | |
} | |
public function testFirstCharacterIsOne() | |
{ | |
$this->assertEquals(0, $this->fizzBuzzOutput[0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment