Skip to content

Instantly share code, notes, and snippets.

@masayuki5160
Last active August 3, 2017 04:29
Show Gist options
  • Save masayuki5160/944c023066d94eedcfb6184a79ae02da to your computer and use it in GitHub Desktop.
Save masayuki5160/944c023066d94eedcfb6184a79ae02da to your computer and use it in GitHub Desktop.
TDD(テスト駆動開発)でFizzBuzz問題を解いてみる ref: http://qiita.com/masayuki5160/items/3e1c282eed91188699f1
<?php
require_once __DIR__. '/fizzBuzz.php';
class testFizzBuzz extends PHPUnit_Framework_TestCase
{
private $fizzBuzz;
public function setUp()
{
$this->fizzBuzz = new fizzBuzz();
}
public function testFizz()
{
$input = 3;
$expected = "Fizz";
$this->assertEquals($this->fizzBuzz->judge($input), $expected);
}
public function testBuzz()
{
$input = 5;
$expected = "Buzz";
$this->assertEquals($this->fizzBuzz->judge($input), $expected);
}
public function testFizzAndBuzz()
{
$input = 15;
$expected = "FizzBuzz";
$this->assertEquals($this->fizzBuzz->judge($input), $expected);
}
}
<?php
class fizzBuzz
{
public function judge($input)
{
$result = $input;
if($input % 3 == 0 && $input % 5 == 0){
$result = "FizzBuzz";
}elseif($input % 3 == 0){
$result = "Fizz";
}elseif($input % 5 == 0){
$result = "Buzz";
}
return $result;
}
}
<?php
require_once __DIR__. '/fizzBuzz.php';
class testFizzBuzz extends PHPUnit_Framework_TestCase
{
private $fizzBuzz;
public function setUp()
{
$this->fizzBuzz = new fizzBuzz();
}
public function testResult()
{
$this->assertEquals($this->fizzBuzz->judge(3), "Fizz");
$this->assertEquals($this->fizzBuzz->judge(9), "Fizz");
$this->assertEquals($this->fizzBuzz->judge(10), "Buzz");
$this->assertEquals($this->fizzBuzz->judge(50), "Buzz");
$this->assertEquals($this->fizzBuzz->judge(70), "Buzz");
$this->assertEquals($this->fizzBuzz->judge(90), "FizzBuzz");
$this->assertEquals($this->fizzBuzz->judge(97), 97);
}
}
<?php
require_once __DIR__. '/fizzBuzz.php';
class testFizzBuzz extends PHPUnit_Framework_TestCase
{
private $fizzBuzz;
public function setUp()
{
$this->fizzBuzz = new fizzBuzz();
}
/**
* @dataProvider fizzProvider
*/
public function testFizz($input)
{
$this->assertEquals('Fizz', $this->fizzBuzz->testFizzBuzz($input));
}
public function fizzProvider()
{
return [[3],[9],[33]];
}
/**
* @dataProvider buzzProvider
*/
public function testBuzz($input)
{
$this->assertEquals('Buzz', $this->fizzBuzz->testFizzBuzz($input));
}
public function buzzProvider()
{
return [[5],[10],[100]];
}
/**
* @dataProvider fizzAndBuzzProvider
*/
public function testFizzAndBuzz($input)
{
$this->assertEquals('FizzBuzz', $this->fizzBuzz->testFizzBuzz($input));
}
public function fizzAndBuzzProvider()
{
return [[15], [45], [60]];
}
/**
* @dataProvider notFizzBuzzProvider
*/
public function testNotFizzBuzz($input)
{
$this->assertEquals($input, $this->fizzBuzz->testFizzBuzz($input));
}
public function notFizzBuzzProvider()
{
return [[7], [11], [98]];
}
}
<?php
class fizzBuzz
{
public function testFizzBuzz($input)
{
$res = $input;
if($input % 3 == 0 && $input % 5 == 0)
{
$res = "FizzBuzz";
}elseif($input % 3 == 0)
{
$res = "Fizz";
}elseif($input % 5 == 0)
{
$res = "Buzz";
}
return $res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment