Home 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

 

原代码类:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

<?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;

    }

 

}

Copy after login

生成的测试类文件:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

<?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

        ];

    }

 

}

Copy after login

这里需要注意,在我们执行“创建/更新测试”后生成的测试文件类与上面会有些不同,这里的测试用例是手动加上去的,这里具体实现可以查看手册里的说明!

附执行结果:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

"/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.

完成。

Copy after login

  

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)