forked from 12meses12katas/Marzo-FizzBuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
412fa7f
commit 095109b
Showing
9 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
class FizzBuzz { | ||
|
||
public $translator; | ||
|
||
public function __construct(FizzBuzzTranslator $translator) { | ||
$this->translator = $translator; | ||
} | ||
|
||
public function countTo($limit) { | ||
$count = ""; | ||
$counter = 1; | ||
while ($counter <= $limit) { | ||
$count .= "{$this->translator->say($counter)} "; | ||
$counter++; | ||
} | ||
return trim($count); | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
require_once 'FizzBuzzNormalTranslator.class.php'; | ||
require_once 'FizzBuzzStrictTranslator.class.php'; | ||
require_once 'FizzBuzz.class.php'; | ||
|
||
class FizzBuzzFactory { | ||
const MODE_NORMAL = 'normal'; | ||
const MODE_STRICT = 'strict'; | ||
|
||
public static function create($mode = self::MODE_NORMAL) { | ||
switch ($mode) { | ||
case self::MODE_STRICT: | ||
$fizzBuzzTranslator = new FizzBuzzStrictTranslator(); | ||
break; | ||
case self::MODE_NORMAL: | ||
default: | ||
$fizzBuzzTranslator = new FizzBuzzNormalTranslator(); | ||
break; | ||
} | ||
return new FizzBuzz($fizzBuzzTranslator); | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
require_once 'FizzBuzzTranslator.abstract.php'; | ||
|
||
class FizzBuzzNormalTranslator extends AbstractFizzBuzzTranslator { | ||
|
||
public function isFizzy($number) { | ||
return $number % 3 == 0; | ||
} | ||
|
||
public function isBuzzy($number) { | ||
return $number % 5 == 0; | ||
} | ||
|
||
public function isFizzBuzzy($number) { | ||
return $number % 15 == 0; | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
require_once 'FizzBuzzTranslator.abstract.php'; | ||
|
||
class FizzBuzzStrictTranslator extends AbstractFizzBuzzTranslator { | ||
|
||
public function isFizzy($number) { | ||
return $number % 3 == 0 && strpos((string) $number, "3") !== false; | ||
} | ||
|
||
public function isBuzzy($number) { | ||
return $number % 5 == 0 && strpos((string) $number, "5") !== false; | ||
} | ||
|
||
public function isFizzBuzzy($number) { | ||
return $number % 15 == 0; | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
require_once 'FizzBuzzTranslator.interface.php'; | ||
|
||
abstract class AbstractFizzBuzzTranslator implements FizzBuzzTranslator { | ||
|
||
public function say($number) { | ||
if ($this->isFizzBuzzy($number)) { | ||
return "fizzbuzz"; | ||
} | ||
if ($this->isBuzzy($number)) { | ||
return "buzz"; | ||
} | ||
if ($this->isFizzy($number)) { | ||
return "fizz"; | ||
} | ||
return (string) $number; | ||
} | ||
|
||
public function isFizzy($number) { | ||
|
||
} | ||
|
||
public function isBuzzy($number) { | ||
|
||
} | ||
|
||
public function isFizzBuzzy($number) { | ||
|
||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
interface FizzBuzzTranslator { | ||
|
||
public function say($number); | ||
|
||
public function isFizzy($number); | ||
|
||
public function isBuzzy($number); | ||
|
||
public function isFizzBuzzy($number); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
require_once '../FizzBuzzNormalTranslator.class.php'; | ||
|
||
class FizzBuzzNormalTranslatorTest extends PHPUnit_Framework_TestCase { | ||
|
||
public $fizzBuzzTranslator; | ||
|
||
public function setUp() { | ||
$this->fizzBuzzTranslator = new FizzBuzzStrictTranslator(); | ||
} | ||
|
||
public function getSayTestCases() { | ||
return array( | ||
'1 says 1' => array(1, "1"), | ||
'2 says 2' => array(2, "2"), | ||
'3 says fizz' => array(3, "fizz"), | ||
'6 says fizz' => array(6, "fizz"), | ||
'33 says fizz' => array(33, "fizz"), | ||
'5 says buzz' => array(5, "buzz"), | ||
'10 says buzz' => array(10, "buzz"), | ||
'25 says buzz' => array(25, "buzz"), | ||
'15 says fizzbuzz' => array(15, "fizzbuzz"), | ||
'30 says fizzbuzz' => array(30, "fizzbuzz"), | ||
'45 says fizzbuzz' => array(45, "fizzbuzz"), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider getSayTestCases | ||
*/ | ||
public function testSayCases($number, $expected) { | ||
$this->assertEquals($expected, $this->fizzBuzzTranslator->say($number)); | ||
} | ||
|
||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
require_once '../FizzBuzzStrictTranslator.class.php'; | ||
|
||
class FizzBuzzStrictTranslatorTest extends PHPUnit_Framework_TestCase { | ||
|
||
public $fizzBuzzTranslator; | ||
|
||
public function setUp() { | ||
$this->fizzBuzzTranslator = new FizzBuzzStrictTranslator(); | ||
} | ||
|
||
public function getSayTestCases() { | ||
return array( | ||
'1 says 1' => array(1, "1"), | ||
'2 says 2' => array(2, "2"), | ||
'3 says fizz' => array(3, "fizz"), | ||
'6 says 6' => array(6, 6), | ||
'33 says fizz' => array(33, "fizz"), | ||
'5 says buzz' => array(5, "buzz"), | ||
'10 says 10' => array(10, 10), | ||
'25 says buzz' => array(25, "buzz"), | ||
'15 says fizzbuzz' => array(15, "fizzbuzz"), | ||
'30 says fizzbuzz' => array(30, "fizzbuzz"), | ||
'45 says fizzbuzz' => array(45, "fizzbuzz"), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider getSayTestCases | ||
*/ | ||
public function testSayCases($number, $expected) { | ||
$this->assertEquals($expected, $this->fizzBuzzTranslator->say($number)); | ||
} | ||
|
||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
require_once '../FizzBuzzFactory.class.php'; | ||
|
||
class FizzBuzzTest extends PHPUnit_Framework_TestCase { | ||
|
||
public function getNormalCountTestCases() { | ||
return array( | ||
'1 counts 1' => array(1, "1"), | ||
'2 counts 1 2' => array(2, "1 2"), | ||
'3 counts 1 2 fizz' => array(3, "1 2 fizz"), | ||
'100 counts it' => array(100, "1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz fizz 22 23 fizz buzz 26 fizz 28 29 fizzbuzz 31 32 fizz 34 buzz fizz 37 38 fizz buzz 41 fizz 43 44 fizzbuzz 46 47 fizz 49 buzz fizz 52 53 fizz buzz 56 fizz 58 59 fizzbuzz 61 62 fizz 64 buzz fizz 67 68 fizz buzz 71 fizz 73 74 fizzbuzz 76 77 fizz 79 buzz fizz 82 83 fizz buzz 86 fizz 88 89 fizzbuzz 91 92 fizz 94 buzz fizz 97 98 fizz buzz"), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider getNormalCountTestCases | ||
*/ | ||
public function testNormalCountCases($limit, $expected) { | ||
$fizzBuzz = FizzBuzzFactory::create(); | ||
$this->assertEquals($expected, $fizzBuzz->countTo($limit)); | ||
} | ||
|
||
public function getStrictCountTestCases() { | ||
return array( | ||
'1 counts 1' => array(1, "1"), | ||
'2 counts 1 2' => array(2, "1 2"), | ||
'3 counts 1 2 fizz' => array(3, "1 2 fizz"), | ||
'100 counts it' => array(100, "1 2 fizz 4 buzz 6 7 8 9 10 11 12 13 14 fizzbuzz 16 17 18 19 20 21 22 23 24 buzz 26 27 28 29 fizzbuzz 31 32 fizz 34 buzz fizz 37 38 fizz 40 41 42 43 44 fizzbuzz 46 47 48 49 buzz 51 52 53 54 buzz 56 57 58 59 fizzbuzz 61 62 fizz 64 buzz 66 67 68 69 70 71 72 73 74 fizzbuzz 76 77 78 79 80 81 82 83 84 buzz 86 87 88 89 fizzbuzz 91 92 fizz 94 buzz 96 97 98 99 100"), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider getStrictCountTestCases | ||
*/ | ||
public function testStrictCountCases($limit, $expected) { | ||
$fizzBuzz = FizzBuzzFactory::create(FizzBuzzFactory::MODE_STRICT); | ||
$this->assertEquals($expected, $fizzBuzz->countTo($limit)); | ||
} | ||
|
||
} | ||
|