Silex Web测试失败

时间:2018-01-12 11:22:25

标签: php symfony phpunit silex

我试图编写一个简单的网络测试来测试我的silex api使用phpunit但是 当我尝试运行它时,我不断收到此错误..

1) App\Tests\BlogControllerTest::testInitialPage
TypeError: Argument 1 passed to Symfony\Component\HttpKernel\Client::__construct() must implement interface Symfony\Component\HttpKernel\HttpKernelInterface, null given, called in C:\vendor\silex\silex\src\Silex\WebTestCase.php on line 63

这是我的网络测试

<?php
namespace App\Tests;

use Silex\WebTestCase;

class BlogControllerTest extends WebTestCase
{

    public function createApplication()
    {
        require dirname(dirname(__DIR__)) . '/src/Application.php';
    }
    public function testInitialPage()
    {
        $client  = $this->createClient();
        $crawler = $client->request('GET', '/Blog/1');

        $this->assertTrue($client->getResponse()->isOk());

    }
}

我的Composer文件的内容:

{
    "require": {
        "silex/silex": "~2.0",
        "symfony/validator": "^4.0",
        "crell/api-problem": "~1.7",
        "tobiassjosten/responsible-service-provider": "^1.0"
    },
    "require-dev": {
        "phpunit/phpunit": "6",
        "symfony/browser-kit": "^4.0",
        "symfony/css-selector": "^4.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/",
            "Tests\\": "tests/"
        }
    }
}

1 个答案:

答案 0 :(得分:1)

来自documentation

  

对于WebTestCase,您必须实现一个createApplication方法,该方法返回您的应用程序实例:

public function createApplication()
{
    // app.php must return an Application instance
    return require __DIR__.'/path/to/app.php';
}

所以,这就是你需要的:

public function createApplication()
{
    return new Application();
}

但在大多数情况下,从引导程序(bootstrap example)返回已配置的应用程序会更好:

public function createApplication()
{
    require __DIR__.'../web/index-test.php'; // path to your bootstrap file
    return $app;
}