symfony - Phpunit 错误:如果 BrowserKit 组件不可用,则无法创建用于功能测试的客户端
问题描述
您好,我正在尝试运行 php 单元测试,但它无法正常工作。我以某种方式遇到了这个错误:
LogicException :如果 BrowserKit 组件不可用,则无法创建用于功能测试的客户端。尝试运行“composer 需要 symfony/browser-kit”。
测试本身真的很基础,所以我很确定它的配置本身会导致这个问题。
这是我的测试用例:
<?php declare(strict_types=1);
namespace App\Tests\Service;
use App\Payment\PaypalService;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class PaypalServiceTest extends WebTestCase
{
public function testGetApiContext(): void
{
$client = static::createClient();
/** @var PaypalService $paypal */
$paypal = $client->getKernel()->getContainer()->get('test.PaypalService');
$test = $paypal->getApiContext();
}
}
这是我的 phpunit.xml.dist:
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.7/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
>
<php>
<ini name="error_reporting" value="-1"/>
<env name="KERNEL_CLASS" value="App\Kernel"/>
<env name="APP_ENV" value="test"/>
<env name="APP_DEBUG" value="0"/>
<env name="APP_SECRET" value="s$cretf0rt3st"/>
<env name="SHELL_VERBOSITY" value="-1"/>
<env name="USER_ID_CUSTOMER_CARE" value="user" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<!-- For Code Coverage -->
<logging>
<log type="junit" target="build/logs/junit.xml"/>
</logging>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<directory>src/Migrations</directory>
<file>src/Kernel.php</file>
</exclude>
</whitelist>
</filter>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
</phpunit>
这是我的 services_test.yml:
services:
_defaults:
public: true
autowire: true
autoconfigure: true
test.logger: '@logger'
test.PaypalService: '@App\Payment\PaypalService'
这是我的composer.json:
{
"type": "project",
"license": "proprietary",
"require": {
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"paypal/rest-api-sdk-php": "^1.14",
"symfony/browser-kit": "4.2.*",
"symfony/console": "4.2.*",
"symfony/dotenv": "4.2.*",
"symfony/flex": "^1.1",
"symfony/framework-bundle": "4.2.*",
"symfony/phpunit-bridge": "4.2.*",
"symfony/yaml": "4.2.*"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"paragonie/random_compat": "2.*",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "4.2.*"
}
}
}
这是 bootstrap.php:
<?php declare(strict_types=1);
require __DIR__.'/../vendor/autoload.php';
use Symfony\Component\Finder\Finder;
// Check if there are fixture files
if (file_exists(__DIR__.'/Resources/fixtures')
&& (new Finder())->files()->in(__DIR__.'/Resources/fixtures')->name(
'*.yml'
)->hasResults()) {
echo 'Loading fixtures...';
passthru('php "'.__DIR__.'/../bin/console" hautelook:fixtures:load --env=test');
echo ' Done'.PHP_EOL;
}
我使用 symfony 框架模板来创建项目,并按照 symfony 给出的说明进行测试,但总是以这个错误结束。
那么这里的问题是什么?
解决方案
在您的phpunit.xml文件中,检查 APP_ENV 环境变量的值。在我的例子中,symfony/framework-bundle 部分包含值“ dev ”而不是“ test ”。
推荐阅读
- python - 基于另一列的冗余对数据框列的数据进行平均
- symfony - 在 Symfony 5 上的 getData 中,Select2 选择的选项不可用
- flutter - Flutter admob 广告未在真实设备中显示
- javascript - 如何在android中获取事件“轮子”
- javascript - 对在 js 对象中通过 refrenece 传递感到困惑
- extjs - 通过 CSS 类选择多个 Dom 元素
- r - 与另一个因子列一起使用时在 geom_line() 时间图中获取多条线以在 r 中着色?
- bar-chart - 如何将图例与谷歌图表中的文本对齐
- google-cloud-platform - Google Cloud PubSub - 区域端点身份验证问题
- php - 如何在没有“\”的情况下解码“Unicode”,如 u091cu0940u0935u0928