Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The LogicException in WebTestCase.php is not correct. #30420

Closed
mehlichmeyer opened this issue Mar 1, 2019 · 11 comments
Closed

The LogicException in WebTestCase.php is not correct. #30420

mehlichmeyer opened this issue Mar 1, 2019 · 11 comments

Comments

@mehlichmeyer
Copy link

Symfony version(s) affected: 4.2

Description
In the WebTestCase class there is an error with the LogicException that is being thrown. The message is hardcoded to "You cannot create the client used in functional tests if the BrowserKit component is not available. Try running "composer require symfony/browser-kit".".
This error can however also occur if the browser-kit is installed.

How to reproduce
Go to vendor/symfony/framework-bundle/Test/WebTestCase.php and look at the try catch in the createClient() function.

Possible Solution
Change the message

Additional context
For me this error occured when I didn't have my phpunit configured in phpstorm.

@ro0NL
Copy link
Contributor

ro0NL commented Mar 1, 2019

Can you explain how the test.client service definition is related to phpunit configuration in phpstorm? What exactly did you had to do besides installing browser-kit?

@xabbuh
Copy link
Member

xabbuh commented Mar 1, 2019

I think the error can also occur when the test client isn't enabled in the framework bundle config.

@SerkanYildiz
Copy link
Contributor

Had this issue too, when I ran the command: composer dump-env test it was fixed.
When framework.test is not true, the test.client service gets removed from the container.

@xabbuh
Copy link
Member

xabbuh commented Mar 7, 2019

Does someone want to create a PR also including the framework.test config option in the error message?

@SerkanYildiz
Copy link
Contributor

SerkanYildiz commented Mar 7, 2019

Ok, couldn't reproduce this on a clean install anymore but went back to our original project.
I understand totally what happens..

Our application runs in a docker container where we have set up environment variables on machine base, and because it is on machine base the DotEnv component did not overwrite it with the value of phpunit.xml.dist

Because now our application is running under dev environment, the FrameworkExtension did not load the test.xml which contains the service definition of test.client.

Do we want to throw another exception that tests should be ran under APP_ENV=test or just add that framework.test should be set on true? Wdyt @xabbuh

@stof
Copy link
Member

stof commented Mar 7, 2019

the message should talk about the framework.test config setting. The fact that there is an environment named test where this setting is set is a convention of the Flex skeleton (and of symfony-standard before that), but it is not a requirement of Symfony at all (you could name it however you want)

@stof
Copy link
Member

stof commented Mar 7, 2019

btw, we can even check which message should be used, as we can check whether browser-kit is installed (using class_exists(Client::class) on the browserkit client)

@mehlichmeyer
Copy link
Author

@SerkanYildiz if I can I would gladly contribute in any way I can! If you'll allow me I'd gladly work ona fix for this and create a merge request.
You assumption regarding Docker could be correct, we are using Docker indeed.

@SerkanYildiz
Copy link
Contributor

Hi @mehlichmeyer,

I already created a PR (#30479) to fix this issue.

@fabpot fabpot closed this as completed Mar 20, 2019
fabpot added a commit that referenced this issue Mar 20, 2019
… provide clearer exception message (SerkanYildiz)

This PR was squashed before being merged into the 3.4 branch (closes #30479).

Discussion
----------

Check if Client exists when test.client does not exist, to provide clearer exception message

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30420
| License       | MIT

The `DotEnv` component does not overwrite by using environment variables declared in `.env` files.

In the `FrameworkExtension` will be checked if the framework.test config is set to a non-false value, if so it will load the `test.xml` file which contains the definition for the `test.client` service.

When running `php bin/phpunit` it will use `phpunit.xml.dist` but because we defined `APP_ENV` in our system it will not load `test.xml` so when creating a client to do functional tests, we'll get an exception which isn't correct: `You cannot create the client used in functional tests if the BrowserKit component is not available. Try running "composer require symfony/browser-kit"`

This PR aims to add a clearer exception message which indicates what really should be done to fix the error message.

Commits
-------

b429950 Check if Client exists when test.client does not exist, to provide clearer exception message
@k00ni
Copy link

k00ni commented Feb 15, 2021

I would like to add a small hint for others who might also come here after getting this error, because it was not clear what the real problem was at the beginning.

I get this message after I upgraded my project from Symfony 4.4 to 5.2. In case you use phpunit/phpunit (with symfony/browser-kit and symfony/css-selector) as well as rely on APP_ENV in .env and phpunit.xml, for instance:

<env name="APP_ENV" value="test"/>

Running PHPUnit with tests using static::createClient triggers the error message (You cannot create the client used in functional tests if the "framework.test" config is not set to true.), because it still thinks you are using APP_ENV=dev (or whatever your default is). Also framework.test was true in (config/packages/test/framework.yml).

Solution

You have to force APP_ENV to be set to test, e.g. by using the following in your phpunit.xml:

<env name="APP_ENV" value="testing" force="true"/>

Source: https://stackoverflow.com/a/53832084/5301527

@yapro
Copy link
Contributor

yapro commented Sep 28, 2021

If you want a prod env, you need to config/packages/framework.yaml add:

when@prod:
    framework:
        test: true
        session:
            storage_factory_id: session.storage.factory.mock_file

and into phpunit.xml.dist add APP_ENV = prod:

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

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/bin/.phpunit/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="tests/bootstrap.php"
         convertDeprecationsToExceptions="false"
>
    <php>
        <server name="APP_ENV" value="prod" force="true" />
        ...
    </php>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants