首页 > 解决方案 > 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 给出的说明进行测试,但总是以这个错误结束。

那么这里的问题是什么?

标签: symfonyphpunit

解决方案


在您的phpunit.xml文件中,检查 APP_ENV 环境变量的值。在我的例子中,symfony/framework-bundle 部分包含值“ dev ”而不是“ test ”。


推荐阅读