dtoaillwk759656786 2017-08-24 16:06
浏览 63
已采纳

如何在phpunit中引用外部数据提供程序?

I am trying to run some tests using a common data provider in PHPUnit.

See below test:

    namespace AppBundle\Tests\Controller;

    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
    use AppBundle\Tests\DataProvider\XmlDataProvider;

    class DefaultControllerTest extends WebTestCase
    {
        /**
         * @dataProvider XmlDataProvider::xmlProvider
         * @covers ReceiveController::receiveAction()
         * @param string
         */
        public function testReceive($xml)
        {
            $client = static::createClient([], ['HTTP_HOST' => 'mt.host']);
            $client->request(
                'POST',
                '/receive',
                [],
                [],
                [],
                $xml
            );

            $response = $client->getResponse();
            $this->assertEquals(200, $response->getStatusCode());
        }
    }

Now I want an external data provider class:

namespace AppBundle\Tests\DataProvider;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class XmlDataProvider extends WebTestCase
{
    /**
     * @dataProvider
     */
    public static function xmlProvider()
    {
        return array([
            'xml1' => '<?xml version="1.0" encoding="UTF-8"?><myTestableXml></myTestableXml>'
        ]);
    }
}

But when I run phpunit I get:

1) Warning The data provider specified for AppBundle\Tests\Controller\DefaultControllerTest::testReceive is invalid. Class XmlDataProvider does not exist

2) Warning No tests found in class "AppBundle\Tests\DataProvider\XmlDataProvider".

How do I do this?

UPDATE

composer.json autoload snippet for reference:

"autoload": {
    "psr-4": {
        "AppBundle\\": "src/AppBundle",
        "Tests\\": "tests"
    },
    "classmap": [
        "app/AppKernel.php",
        "app/AppCache.php"
    ]
},
"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
    },
    "files": [
        "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
    ]
},
  • 写回答

2条回答 默认 最新

  • drf16571 2017-08-24 16:12
    关注

    You need to reference the data provider using the fully-qualified classname:

    namespace AppBundle\Tests\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
    
    class DefaultControllerTest extends WebTestCase
    {
        /**
         * @dataProvider \AppBundle\Tests\DataProvider\XmlDataProvider::xmlProvider
         * @covers ReceiveController::receiveAction()
         * @param string $xml
         */
        public function testReceive($xml)
        {
            // ...
        }
    }
    

    Autoloading

    Also, make sure to adjust your autoload configuration in composer.json, so the data provider can be autoloaded (might need adjustment depending on which directory the ’AppBundle\Test` namespace maps to):

    {
        "autoload-dev": {
            "psr-4": {
                "AppBundle\\Tests\\": "tests/"
            }
        }
    }
    

    Alternatively, since you suggest your autoloading configuration looks like this:

    {
        "autoload-dev": {
            "psr-4": {
                "Tests\\": "tests/"
            }
        }
    }
    

    you need to adjust your namespace for the presented tests from AppBundle\Tests to Tests\AppBundle.

    Note Unrelated to your question, but personally, I can't see a need for the data provider to extend WebTestCase.

    For examples, see:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀