gpt4 book ai didi

unit-testing - 为什么 PHPunit 使用 Silex 请求 KERNEL_DIR?

转载 作者:行者123 更新时间:2023-12-03 02:37:31 28 4
gpt4 key购买 nike

我正在尝试为我的 Silex 应用程序设置单元测试,但我不断收到此错误消息:

RuntimeException: Either set KERNEL_DIR in your phpunit.xml according to http://symfony.com/doc/current/book/testing.html#your-first-functional-test or override the WebTestCase::createKernel() method.

这是我的./app/phpunit.xml.dist:

<?xml version="1.0" encoding="UTF-8"?>

<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="phpunit_bootstrap.php"
>
<testsuites>
<testsuite name="Project Test Suite">
<directory>../src/Acme/*/Tests</directory>
</testsuite>
</testsuites>

<!--<php>-->
<!--<server name="KERNEL_DIR" value="/var/www/acme/api/app/" />-->
<!--</php>-->
</phpunit>

这是我的./app/phpunit_bootstrap.php(包括 Composer 的自动加载器):

<?php

if (!@include __DIR__ . '/../../vendor/autoload.php') {
die(<<<'EOT'
You must set up the project dependencies, run the following commands:
wget http://getcomposer.org/composer.phar
php composer.phar install
EOT
);
}

我的目录结构如下:

Silex application tree structure

看起来 phpunit 正在寻找 *Kernel.php 但我不知道为什么。

这是我的单元测试:

<?php

namespace Acme\User\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class UserControllerTest extends WebTestCase
{
protected $headers;

protected function setUp()
{
$this->headers = array(
'CONTENT_TYPE' => 'application/json',
);
}

public function testAuthUser()
{
$client = static::createClient();

$client->request('POST', ...);

// Check the response content type
$this->assertTrue(
$client->getResponse()->headers->contains(
'Content-Type',
'application/json'
)
);

// Assert that the response status code is 2xx
$this->assertTrue($client->getResponse()->isSuccessful());

$response = json_decode($client->getResponse()->getContent(), true);

var_dump($response);die;
}
}

最佳答案

好的,

我已经成功让它发挥作用了。我遇到了几个配置问题。

我在 app/phpunit_boostrap.php 中添加了第一个:

<?php

$_SERVER['env'] = 'test';
...

然后在我的 web/index.php 中添加:

// Return the kernel instead to run it if we are unit testing
if ('test' == $app['mode']) {
return $app;
}

$app->run();

然后在我的 app/application.php 中,我添加了:

...
// Set dev mode for unit testing
if (isset($_SERVER['env']) && 'test' === $_SERVER['env']) {
$app['mode'] = 'test';
}
...

我注意到我没有使用正确的 WebTestCaseSilex 有它自己的地方,您需要在其中创建应用程序(设置内核):

<?php

namespace Acme\User\Tests\Controller;

// Notice the Silex class for the WebTestCase
use Silex\WebTestCase;

class UserControllerTest extends WebTestCase
{
protected $headers;

public function createApplication()
{
// index.php should return the $app instead to run() it
return require __DIR__ . '/../../../../../web/index.php';
}

protected function setUp()
{
// Don't forget to call the parent setup that is setting the kernel
parent::setUp();
$this->headers = array(
'CONTENT_TYPE' => 'application/json',
);
}

public function testAuthUser()
{
// Create a client this way
$client = $this->createClient();

$client->request('POST', ...);

现在一切都运转良好。此外,我还创建了自己的 WebTestCase 类,扩展了 Silex 中的类,这样我就不必一直设置应用程序。

我希望这会对你们中的一些人有所帮助,因为我没有找到有关 Silex 单元测试的任何好的帮助。

干杯,马克西姆

关于unit-testing - 为什么 PHPunit 使用 Silex 请求 KERNEL_DIR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22161214/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com