首页 php教程 php手册 PHPUnit 入门案例 - wangyulu

PHPUnit 入门案例 - wangyulu

May 20, 2016 pm 01:15 PM

了解PHPUnit

本案例是关于创建三角形的一个单元测试入门案例,在netbeans环境中完成,关于在此环境中搭建phpunit这里不再描述,可以参考以下资料完成搭建工作:

http://www.cnblogs.com/x3d/p/phpunit-in-netbeans8.html

https://phpunit.de/manual/current/zh_cn/installation.html

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.
完成。
登录后复制

  

 

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
2 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)