编程语言
首页 > 编程语言> > phpUnit中数据供给

phpUnit中数据供给

作者:互联网

数据供给

索引数组

<?php
require_once dirname(dirname(__FILE__)) . '\vendor\autoload.php';

use PHPUnit\Framework\TestCase;

class MyClassTest extends TestCase{

    public function one()
    {
        return [
            [1, 2, 3],
            [1, 1, 2],
            [1, 1, 1]
        ];
    }

    /**
     * @test
     * @dataProvider one
     */
    public function two($arr_value_1, $arr_value_2, $arr_value_3)
    {

        $expectd_value = $arr_value_3;
        $actual_value = $arr_value_1 + $arr_value_2;
        $this->assertEquals($expectd_value, $actual_value, '当不相等,报这条错误信息');
    }


}
PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

..F                                                                 3 / 3 (100%)

Time: 604 ms, Memory: 8.00MB

There was 1 failure:

1) MyClassTest::two with data set #2 (1, 1, 1)
当不相等,报这条错误信息
Failed asserting that 2 matches expected 1.

D:\yanjing\workspace\study_test\php\MyClass.php:26

FAILURES!
Tests: 3, Assertions: 3, Failures: 1.

关联数组

<?php
require_once dirname(dirname(__FILE__)) . '\vendor\autoload.php';

use PHPUnit\Framework\TestCase;

class MyClassTest extends TestCase
{

    public function one()
    {
        return [
            '第一项' => [1, 2, 3],
            '第一项' => [1, 1, 2],
            '第一项' => [1, 1, 1],
        ];
    }

    /**
     * @test
     * @dataProvider one
     */
    public function two($arr_value_1, $arr_value_2, $arr_value_3)
    {

        $expectd_value = $arr_value_3;
        $actual_value = $arr_value_1 + $arr_value_2;
        $this->assertEquals($expectd_value, $actual_value, '当不相等,报这条错误信息');
    }

}
PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 596 ms, Memory: 8.00MB

There was 1 failure:

1) MyClassTest::two with data set "第一项" (1, 1, 1)
当不相等,报这条错误信息
Failed asserting that 2 matches expected 1.

D:\yanjing\workspace\study_test\php\MyClass.php:27

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

迭代器对象

定义一个迭代器

<?php
namespace Test\Iterator;

class RmbIterAtor implements \IterAtor
{
    private $arr = [
        'first' => [1, 2, 3],
        'second' => [1, 3, 4],
        'third' => [1, 1, 0],
    ];

    public function current()
    {
        return current($this->arr);
    }

    public function key()
    {
        return key($this->arr);
    }

    public function next()
    {
        return next($this->arr);
    }

    public function rewind()
    {
        return reset($this->arr);
    }

    public function valid()
    {
        return current($this->arr);
    }
}

测试

<?php
require_once dirname(dirname(__FILE__)) . '\vendor\autoload.php';

use PHPUnit\Framework\TestCase;
use Test\iterator\RmbIterator;

class MyClassTest extends TestCase
{

    public function one()
    {
        return new RmbIterator;
    }

    /**
     * @test
     * @dataProvider one
     */
    public function two($arr_value_1, $arr_value_2, $arr_value_3)
    {

        $expectd_value = $arr_value_3;
        $actual_value = $arr_value_1 + $arr_value_2;
        $this->assertEquals($expectd_value, $actual_value, '当不相等,报这条错误信息');
    }

}

结果

PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

..F                                                                 3 / 3 (100%)

Time: 757 ms, Memory: 8.00MB

There was 1 failure:

1) MyClassTest::two with data set "third" (1, 1, 0)
当不相等,报这条错误信息
Failed asserting that 2 matches expected 0.

D:\yanjing\workspace\study_test\php\MyClass.php:24

FAILURES!
Tests: 3, Assertions: 3, Failures: 1.

数据供给优先级高于依赖

<?php
require_once dirname(dirname(__FILE__)) . '\vendor\autoload.php';

use PHPUnit\Framework\TestCase;

class MyClassTest extends TestCase
{


    public function one()
    {
        return [
            'key1' => [1, 2, 3],
            'key2' =>[1, 1, 2],
            'key3' =>[1, 1, 0]
        ];
    }


    /**
     * @test
     */
    public function two()
    {
        $this->assertTrue(true);
        return [
            [100, 100, 200],
            [10, 10, 20],
            [1, 1, 0]
        ];
    }    

    /**
     * @test
     * @depends two
     * @dataProvider one
     */
    public function three($arr_value_1, $arr_value_2, $arr_value_3)
    {

        $expectd_value = $arr_value_3;
        $actual_value = $arr_value_1 + $arr_value_2;
        $this->assertEquals($expectd_value, $actual_value, '当不相等,报这条错误信息');
    }

}
PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

...F                                                                4 / 4 (100%)

Time: 604 ms, Memory: 8.00MB

There was 1 failure:

1) MyClassTest::three with data set "key3" (1, 1, 0)
当不相等,报这条错误信息
Failed asserting that 2 matches expected 0.

D:\yanjing\workspace\study_test\php\MyClass.php:43

FAILURES!
Tests: 4, Assertions: 4, Failures: 1.

标签:function,arr,供给,错误信息,value,phpUnit,php,数据,public
来源: https://www.cnblogs.com/zxcv123/p/12559482.html