symfonyphpunit

Phpunit error: You cannot create the client used in functional tests if the BrowserKit component is not available


Hello I'm trying to run a php unit test but it not working correct. I'm getting somehow this error:

LogicException : You cannot create the client used in functional tests if the BrowserKit component is not available. Try running "composer require symfony/browser-kit".

The test itself is really really basic so I'm pretty sure its the configuration itself which causes this problem.

This is my test case:

<?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();
    }
}

This is my 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>

And this is my services_test.yml:

services:
  _defaults:
    public: true
    autowire: true
    autoconfigure: true

  test.logger: '@logger'
  test.PaypalService: '@App\Payment\PaypalService'

And here is my 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.*"
        }
    }
}

This is the 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;
}

I used the symfony skeleton template for creating the project and followed the instructions symfony is giving for testing but always ending in this error.

So what is the problem here?


Solution

  • In your phpunit.xml file, check the value of the APP_ENV environment variable. If it's not there, you may want to force it like this:

    <phpunit bootstrap="vendor/autoload.php">
        <testsuites>
            <testsuite name="MyProject">
                <directory>tests</directory>
            </testsuite>
        </testsuites>
        <php>
            <env name="APP_ENV" value="test" force="true" />
        </php>
    </phpunit>
    

    In my case, the symfony/framework-bundle section contained the value "dev" instead of "test".