Skip to content

Commit 711b7c6

Browse files
Merge pull request kennyledet#495 from marius-rizac/FizzBuzz-PHP
Fizz buzz php
2 parents ae066b6 + 23349cc commit 711b7c6

10 files changed

Lines changed: 316 additions & 0 deletions

FizzBuzz/PHP/composer.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"require": {
3+
"php": ">=5.5.0"
4+
},
5+
"autoload": {
6+
"psr-0": {
7+
"": "src/"
8+
}
9+
}
10+
}

FizzBuzz/PHP/phpunit.xml.dist

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
backupGlobals="true"
4+
backupStaticAttributes="false"
5+
bootstrap="tests/bootstrap.php"
6+
cacheTokens="false"
7+
colors="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
forceCoversAnnotation="false"
12+
mapTestClassNameToCoveredClassName="false"
13+
processIsolation="false"
14+
stopOnError="false"
15+
stopOnFailure="false"
16+
stopOnIncomplete="false"
17+
stopOnSkipped="false"
18+
timeoutForSmallTests="1"
19+
timeoutForMediumTests="10"
20+
timeoutForLargeTests="60"
21+
verbose="false">
22+
23+
<testsuites>
24+
<testsuite name="core">
25+
<directory>tests/*</directory>
26+
</testsuite>
27+
</testsuites>
28+
</phpunit>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
echo implode("\n", array_map(function($i){
4+
if ($i % 15 === 0) {
5+
return 'FizzBuzz';
6+
}
7+
if ($i % 3 === 0) {
8+
return 'Fizz';
9+
}
10+
if ($i % 5 === 0) {
11+
return 'Buzz';
12+
}
13+
14+
return $i;
15+
16+
}, range(1,100)));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* @param int $number
5+
*
6+
* @return string|int
7+
*/
8+
function fizzBuzz($number)
9+
{
10+
if ($number % 15 === 0) {
11+
return 'FizzBuzz';
12+
}
13+
14+
if ($number % 3 === 0) {
15+
return 'Fizz';
16+
}
17+
18+
if ($number % 5 === 0) {
19+
return 'Buzz';
20+
}
21+
22+
return $number;
23+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/**
4+
* All classes in the same file for a bird eye view
5+
* Usualy use one file per interface, class within a namespace
6+
*/
7+
8+
interface FizzBuzzRuleInterface
9+
{
10+
/**
11+
* @param int $number
12+
*
13+
* @return bool
14+
*/
15+
public function matchRule($number);
16+
17+
/**
18+
* @return string
19+
*/
20+
public function getResponse();
21+
}
22+
23+
class FizzBuzzRule implements FizzBuzzRuleInterface
24+
{
25+
/**
26+
* @var int
27+
*/
28+
protected $number;
29+
30+
/**
31+
* @var string
32+
*/
33+
protected $output;
34+
35+
/**
36+
* @param int $number
37+
* @param string $output
38+
*/
39+
public function __construct($number, $output)
40+
{
41+
$this->number = $number;
42+
$this->output = $output;
43+
}
44+
45+
/**
46+
* @param int $number
47+
*
48+
* @return bool
49+
*/
50+
public function matchRule($number)
51+
{
52+
return $number % $this->number === 0;
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function getResponse()
59+
{
60+
return $this->output;
61+
}
62+
}
63+
64+
class FizzBuzz
65+
{
66+
/**
67+
* @var array
68+
*/
69+
protected $rules = [];
70+
71+
/**
72+
* @param FizzBuzzRuleInterface $ruleInterface
73+
*
74+
* @return $this
75+
*/
76+
public function addRule(FizzBuzzRuleInterface $ruleInterface)
77+
{
78+
$this->rules[] = $ruleInterface;
79+
80+
return $this;
81+
}
82+
83+
/**
84+
* @param int $number
85+
*
86+
* @return string|int
87+
*/
88+
public function execute($number)
89+
{
90+
/** @var FizzBuzzRuleInterface $rule */
91+
foreach ($this->rules as $rule) {
92+
if ($rule->matchRule($number)) {
93+
return $rule->getResponse();
94+
}
95+
}
96+
97+
return $number;
98+
}
99+
}
100+
101+
/*
102+
$fizzBuzz = new FizzBuzz();
103+
$fizzBuzz
104+
->addRule(new FizzBuzzRule(15, 'FizzBuzz')) // add as many rules as you like without changing any class
105+
->addRule(new FizzBuzzRule(3, 'Fizz'))
106+
->addRule(new FizzBuzzRule(5, 'Buzz'))
107+
;
108+
109+
// variant 1
110+
for ($i = 1; $i <= 100; $i++) {
111+
echo $fizzBuzz->execute($i);
112+
echo "\n";
113+
}
114+
115+
// variant 2
116+
echo implode("\n", array_map([$fizzBuzz, 'execute'], range(1, 100)));
117+
*/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
for ($i=1; $i<=100; $i++) {
4+
if ($i % 15 === 0) {
5+
echo "FizzBuzz";
6+
7+
} elseif ($i % 3 === 0) {
8+
echo 'Fizz';
9+
10+
} elseif ($i % 5 === 0) {
11+
echo 'Buzz';
12+
13+
} else {
14+
echo $i;
15+
}
16+
17+
echo "\n";
18+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
require_once dirname(__DIR__) . '/../src/fizz-buzz-function.php';
4+
5+
class FizzBuzzFunctionTest extends \PHPUnit_Framework_TestCase
6+
{
7+
8+
public function testNumber3()
9+
{
10+
$this->assertSame('Fizz', fizzBuzz(3));
11+
}
12+
13+
public function testNumber5()
14+
{
15+
$this->assertSame('Buzz', fizzBuzz(5));
16+
}
17+
18+
public function testNumber15()
19+
{
20+
$this->assertSame('FizzBuzz', fizzBuzz(15));
21+
}
22+
23+
public function testNumber1()
24+
{
25+
$this->assertSame(1, fizzBuzz(1));
26+
}
27+
28+
public function testNumber10()
29+
{
30+
$this->assertSame('Buzz', fizzBuzz(10));
31+
}
32+
33+
public function testNumber13()
34+
{
35+
$this->assertSame(13, fizzBuzz(13));
36+
}
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
require dirname(__DIR__) . '/../src/fizz-buzz-plugin-classes.php';
4+
5+
class FizzBuzzRuleTest extends \PHPUnit_Framework_TestCase
6+
{
7+
8+
/**
9+
* @var FizzBuzzRuleInterface
10+
*/
11+
protected $rule;
12+
13+
public function setUp()
14+
{
15+
$this->rule = new FizzBuzzRule(10, 'MyRule');
16+
}
17+
18+
public function testRuleMatchNumber()
19+
{
20+
$this->assertTrue($this->rule->matchRule(10));
21+
}
22+
23+
public function testRuleDoNotMatchNumber()
24+
{
25+
$this->assertFalse($this->rule->matchRule(11));
26+
}
27+
28+
public function testOutput()
29+
{
30+
$this->assertSame('MyRule', $this->rule->getResponse());
31+
}
32+
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
class FizzBuzzTest extends \PHPUnit_Framework_TestCase
4+
{
5+
6+
/**
7+
* @var FizzBuzz
8+
*/
9+
protected $fizzBuzz;
10+
11+
public function setUp()
12+
{
13+
$this->fizzBuzz = new FizzBuzz();
14+
$this->fizzBuzz->addRule(new FizzBuzzRule(3, 'Fizz'));
15+
}
16+
17+
public function testFizzOutputOn3()
18+
{
19+
$this->assertSame('Fizz', $this->fizzBuzz->execute(3));
20+
}
21+
22+
public function testFizzOutputOnMultipleOf3()
23+
{
24+
$this->assertSame('Fizz', $this->fizzBuzz->execute(12));
25+
}
26+
27+
public function testNumberOutputOnNotMultipleOf3()
28+
{
29+
$this->assertSame(7, $this->fizzBuzz->execute(7));
30+
}
31+
}

FizzBuzz/PHP/tests/bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require_once dirname(__DIR__) . '/vendor/autoload.php';

0 commit comments

Comments
 (0)