PHPUnit入門例

7754 ワード

PHPUnitを知る
この判例は三角形を作成する単位テストの入門例で、netbeans環境で完成しました。この環境の中でphpunnitを構築することについてはここでは説明しません。以下の資料を参考にして構築作業を完成します。
http://www.cnblogs.com/x3d/p/phpunit-in-netbeans8.html
https://phpunit.de/manual/current/zh_cn/installation.
https://github.com/sebastianbergmann/phpunit-skeleton-generator
 
元のコードクラス:
 
<?php

class Triangle
{

    /**
     *         
     * @var int 
     */
    protected $a;

    /**
     *         
     * @var int 
     */
    protected $b;

    /**
     *         
     * @var int 
     */
    protected $c;

    /**
     *   
     * @var string 
     */
    protected $type;

    /**
     *   
     */
    const TYPE_EQUILATERAL = 'Equilateral';

    /**
     *   
     */
    const TYPE_ISOSCELES = 'Isosceles';

    /**
     *   
     */
    const TYPE_ORDINARY = 'Ordinary';

    public function __construct($a = 0, $b = 0, $c = 0)
    {
        $this->initSide($a, $b, $c);
    }

    /**
     *      
     * @param int $a
     * @param int $b
     * @param int $c
     */
    protected function initSide(&$a = 0, &$b = 0, &$c = 0)
    {
        $this->a = intval($a);
        $this->b = intval($b);
        $this->c = intval($c);
        return $this;
    }

    /**
     *   
     */
    public function create($a, $b, $c)
    {
        return $this->initSide($a, $b, $c)->verifySideIsValid();
    }

    /**
     *     
     */
    public function getType()
    {
        return $this->verifyType()->type;
    }

    /**
     *         
     * @return boolean
     */
    protected function verifySideIsValid()
    {
        if (intval($this->a) <= 0 || intval($this->b) <= 0 || intval($this->c) <= 0) {
            return false;
        }

        if ($this->a + $this->b <= $this->c) {
            return false;
        }
        if ($this->a + $this->c <= $this->b) {
            return false;
        }
        if ($this->b + $this->c <= $this->a) {
            return false;
        }
        if ($this->a - $this->b >= $this->c) {
            return false;
        }
        if ($this->a - $this->c >= $this->b) {
            return false;
        }
        if ($this->b - $this->c >= $this->a) {
            return false;
        }
        return true;
    }

    /**
     *     
     */
    protected function verifyType()
    {
        if ($this->isEquilateral()) {
            $this->type = self::TYPE_EQUILATERAL;
            return $this;
        }
        if ($this->isIsosceles()) {
            $this->type = self::TYPE_ISOSCELES;
            return $this;
        }
        $this->type = self::TYPE_ORDINARY;
        return $this;
    }

    /**
     *         
     */
    protected function isEquilateral()
    {
        return (($this->a == $this->b ) && ($this->b == $this->c)) ? true : false;
    }

    /**
     *         
     */
    protected function isIsosceles()
    {
        return (($this->a == $this->b ) || ($this->b == $this->c) || ($this->a == $this->c)) ? true : false;
    }

}
 
 
生成したテストクラスファイル:
<?php

/**
 * Generated by PHPUnit_SkeletonGenerator on 2016-03-13 at 19:49:12.
 */
class TriangleTest extends PHPUnit_Framework_TestCase
{

    /**
     * @var Triangle
     */
    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 Triangle;
    }

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

    /**
     * @dataProvider addDataProvider
     * @covers Triangle::create
     * @todo   Implement testCreate().
     */
    public function testCreate($a, $b, $c)
    {
        // Remove the following lines when you implement this test.
        /**
          $this->markTestIncomplete(
          'This test has not been implemented yet.'
          );
         * 
         */
        /*      */
        $this->assertTrue($this->object->create($a, $b, $c));
    }

    /**
     * @covers Triangle::getType
     * @todo   Implement testGetType().
     */
    public function testGetType()
    {
        // Remove the following lines when you implement this test.
        $this->markTestIncomplete(
                'This test has not been implemented yet.'
        );
    }

    /**
     *     
     * @return array
     */
    public function addDataProvider()
    {
        return [
            [3, 4, 5], //yes
            [2, 2, 2], //yes
            [8, 10, 8], //yes
            [2, 3, 4], //yes
            [1, 2, 3], //no
            [5, 6, 7], //yes
            [8, 8, 15], //yes
            [0, 0, 0], //no
            [-10, 2, 5], //no
            [0, 2, 1], //no
        ];
    }

}
ここで注意したいのですが、「作成/更新テスト」を実行してから作成したテストファイル類は上記とは少し違っています。ここのテストケースは手動で追加したものです。ここで具体的に実現すればマニュアルの説明を確認できます。
 
添付実行結果:
"/usr/bin/php" "/usr/local/bin/phpunit" "--colors" "--log-junit" "/tmp/nb-phpunit-log.xml" "--bootstrap" "/var/www/html/phpunit/test/bootstrap.php" "/usr/local/netbeans-8.1/php/phpunit/NetBeansSuite.php" "--" "--run=/var/www/html/phpunit/test/core/triangleTest.php"
PHPUnit 5.2.10 by Sebastian Bergmann and contributors.

....F..FFFI                                                       11 / 11 (100%)

Time: 105 ms, Memory: 10.50Mb

There were 4 failures:

1) TriangleTest::testCreate with data set #4 (1, 2, 3)
Failed asserting that false is true.

/var/www/html/phpunit/test/core/triangleTest.php:47

2) TriangleTest::testCreate with data set #7 (0, 0, 0)
Failed asserting that false is true.

/var/www/html/phpunit/test/core/triangleTest.php:47

3) TriangleTest::testCreate with data set #8 (-10, 2, 5)
Failed asserting that false is true.

/var/www/html/phpunit/test/core/triangleTest.php:47

4) TriangleTest::testCreate with data set #9 (0, 2, 1)
Failed asserting that false is true.

/var/www/html/phpunit/test/core/triangleTest.php:47

FAILURES!
Tests: 11, Assertions: 10, Failures: 4, Incomplete: 1.
  。