phpunit depends方法入参获取不到

ShawnLee
  • 32

问题概述

PHP单元测试同时使用了 @depends 和 @dataProvider 方法之后,depends所在方法获取不到上一个依赖方法的返回值。

代码

<?php

use algo\exception\IllegalIndexException;
use algo\LinkedList;
use PHPUnit\Framework\TestCase;

class LinkedListTest extends TestCase
{
    public function addProvider()
    {
        return [
            ['23 24 25', 23, 25, 1, 24],
            ['23 24 25', 23, 24, 2, 25]
        ];
    }

    /**
     * @dataProvider addProvider
     * @param $expected
     * @param $first
     * @param $last
     * @param $index
     * @param $value
     * @return LinkedList
     * @throws Exception
     */
    public function testAdd($expected, $first, $last, $index, $value)
    {
        $linkedList = new LinkedList();
        $linkedList->addFirst($first);
        $linkedList->addLast($last);
        $linkedList->add($index, $value);
        $this->assertEquals($expected, (string)$linkedList);

        return $linkedList;
    }

    /**
     * @depends testAdd
     * @param LinkedList $linkedList
     * @throws Exception
     */
    public function testAddException(LinkedList $linkedList) //line 43
    {
        $this->expectExceptionMessage(IllegalIndexException::MESSAGE_PREFIX);
        $linkedList->add(100, 23333);
    }
}

报错

Testing started at 下午9:24 ...
/usr/bin/php /Users/xxx/github/we_algo/vendor/phpunit/phpunit/phpunit --no-configuration --filter "/(LinkedListTest::testAdd|LinkedListTest::testAddException)( .*)?$/" --test-suffix LinkedListTest.php /Users/xxx/github/we_algo/tests --teamcity --cache-result-file=/Users/xxx/github/we_algo/.phpunit.result.cache
PHPUnit 9.5.4 by Sebastian Bergmann and contributors.


TypeError : Argument 1 passed to LinkedListTest::testAddException() must be an instance of algo\LinkedList, null given, called in /Users/xxx/github/we_algo/vendor/phpunit/phpunit/src/Framework/TestCase.php on line 1526
 /Users/xxx/github/we_algo/tests/LinkedListTest.php:43
回复
阅读 292
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏