FizzBuzz

以下摘自Github - FizzBuzzEnterpriseEdition

FizzBuzz is a game that has gained in popularity as a programming assignment to weed out non-programmers during job interviews. The object of the assignment is less about solving it correctly according to the below rules and more about showing the programmer understands basic, necessary tools such as if-/else-statements and loops. The rules of FizzBuzz are as follows:

For numbers 1 through 100,

  • if the number is divisible by 3 print Fizz;
  • if the number is divisible by 5 print Buzz;
  • if the number is divisible by 3 and 5 (15) print FizzBuzz;
  • else, print the number.

中文的描述如下:

  • 打印从1至100的整数
  • 该数能被3整除的时候,打印”Fizz”
  • 能被5整除的时候打印”Buzz”
  • 如果既能被3又能被5整除的时候,打印”FizzBuzz”
  • 其他情况打印数字

FizzBuzz类

以下是PHP实现的一个FizzBuzz类,只有一个方法check,输入数字可以返回FizzBuzz游戏的结果。

<?php

/*
 * Copyright (C) 2017 SINA Corporation
 *  
 *  
 * 
 * This script is firstly created at 2017-02-20.
 * 
 * To see more infomation,
 *    visit our official website http://app.finance.sina.com.cn/.
 */

namespace App\Foo;

use InvalidArgumentException;

/**
 * Description of FizzBuzz
 *
 * @encoding UTF-8 
 * @author jiaojie <jiaojie@staff.sina.com.cn> 
 * @since 2017-02-20 14:41 (CST) 
 * @version 0.1
 * @description 
 */
class FizzBuzz
{

    public function check($input)
    {
        if (is_numeric($input)) {
            if ($input % 3 === 0 && $input % 5 === 0) {
                return "FizzBuzz";
            } elseif ($input % 3 === 0 && $input % 5 !== 0) {
                return "Fizz";
            } elseif ($input % 3 !== 0 && $input % 5 === 0) {
                return "Buzz";
            } else {
                return $input;
            }
        } else {
            throw new InvalidArgumentException("Expect a number while put '{$input}' in.");
        }
    }

}

测试类

以下是一个FizzBuzz测试的一个简单实现,使用了数据提供器(DataProvider)。

<?php

namespace App\Foo;

/**
 * Generated by PHPUnit_SkeletonGenerator on 2017-02-20 at 14:52:19.
 */
class FizzBuzzTest extends \PHPUnit_Framework_TestCase
{

    /**
     * @var FizzBuzz
     */
    protected $object;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->object = new FizzBuzz;
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown()
    {
        unset($this->object);
    }

    public function fizzBuzzProvider()
    {
        return [
            [14, 14],
            [3, 'Fizz'],
            [5, 'Buzz'],
            [15, 'FizzBuzz'],
        ];
    }

    /**
     * @covers App\Foo\FizzBuzz::check
     * @dataProvider fizzBuzzProvider
     */
    public function testCheck($input, $expected)
    {
        $this->assertEquals($expected, $this->object->check($input));
    }

}

执行结果

jiaojie@finance:/data/projects/php/UnitTesting/test$ phpunit
PHPUnit 4.7.7 by Sebastian Bergmann and contributors.

 11  -_-_-_-_-_-_-_,------,  
 0   -_-_-_-_-_-_-_|  /\_/\  
 1   -_-_-_-_-_-_-~|_( ^ .^) 
     -_-_-_-_-_-_- ""  ""    


Time: 331 ms, Memory: 22.25Mb

OK, but incomplete, skipped, or risky tests!
Tests: 12, Assertions: 15, Incomplete: 1.