phpunit错误:如果BrowserKit组件不可用,则无法创建功能测试中使用的客户端

时间:2019-02-28 11:51:32

标签: symfony phpunit phpunit-testing

您好,我正在尝试运行php单元测试,但无法正常工作。我莫名其妙地出现了这个错误:

LogicException:如果BrowserKit组件不可用,则无法创建功能测试中使用的客户端。尝试运行“ composer require 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给出的测试说明进行操作,但始终以此错误结尾。

那这里是什么问题?

1 个答案:

答案 0 :(得分:2)

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