Symfony 4 phpunit : KERNEL_CLASS env variable error - phpunit

I'm trying to learn how to make phpunit by testing the User class made by "make:user" but i'm facing this problem when extending "KernelTestCase" and run the test :
LogicException: You must set the KERNEL_CLASS environment variable to the fully-qualified class name of your Kernel in phpunit.xml / phpunit.xml.dist or override the "App\tests\Entity
UsersTest::createKernel()" or "App\tests\Entity\UsersTest::getKernelClass()" method.
Here is my test, i'm trying to test an Entity :
I'm trying to fix it since 2 days with my friend Google but I didn't found any solution. Can you help me ? Thank you guys !

I just got the same problem.
The solution is to update your ./phpunit.xml.dist file to set the KERNEL_CLASS environment variable :
[...]
<php>
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="7.5" />
<server name="KERNEL_CLASS" value="App\Kernel" /> <--- SET KERNEL_CLASS AT THIS LINE
</php>
[...]

In my case the tests/bootstrap.php file was not in sync with the one of the phpunit-bridge recipe. The recipe was not executed because I updated Symfony 3.4 (without flex) to Symfony 4.4.
After installing the recipe with composer recipes:install symfony/phpunit-bridge --force -v the following files were overridden:
.env.test
phpunit.xml.dist
tests/bootstrap.php
Now the .env.test gets loaded and the KERNEL_CLASS env variable, which is defined there, is respected correctly.

Related

How to get a simple phpunit codecoverage summary in text output?

I use PHPUnit 9.5.7 on php 8.0.3
I would like to have a checker for minimum code coverage of lines as a git hook.
I have seen in online examples a simple 3 line output as a summary after running the tests. How do I get this output? I searched several articles and the config documentation but didn't find anything.
My aim is to deny the commit if a minimal coverage of lines isn't achieved. So if you have other ideas to get this done I am open.
Here is my current configuration file:
<?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="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
cacheResult="false"
printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer"
>
<coverage>
<include>
<directory>src</directory>
</include>
</coverage>
<php>
<ini name="error_reporting" value="-1"/>
<server name="KERNEL_CLASS" value="\App\Kernel" />
<server name="APP_ENV" value="test" force="true"/>
<server name="SHELL_VERBOSITY" value="-1"/>
<server name="SYMFONY_PHPUNIT_REMOVE" value=""/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
<env name="CORS_ALLOW_ORIGIN" value="^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$"/>
</php>
<extensions>
<extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension"/>
</extensions>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
What you refer to is the phpunit text-output for coverage:
--coverage-text=<file> Generate code coverage report in text format [default: standard output]
An alternative to it is a small script that you can run after running the tests with coverage to check on the gathered data.
Here an excerpt from a composer.json {"script": {}} section:
"unit-test": [
"#phpunit --log-junit build/log/junit.xml --coverage-clover build/log/clover.xml test/unit",
"#php -f lib/build/coverage-checker.php -- build/log/clover.xml"
],
Taken from an existing project which has the script:
https://github.com/ktomk/pipelines/blob/master/lib/build/coverage-checker.php
It is a maintained version that originated in this blog-post:
http://ocramius.github.io/blog/automated-code-coverage-check-for-github-pull-requests-with-travis/
The percentage to check for by default is 100%. You can pass it as the second positional argument if you would like to lower it.

Symfony 4, PHPUnit Bridge and phpunit location

If you install phpunit/phpunit package in Symfony 4 application, you get the message:
Adding phpunit/phpunit as a dependency is discouraged in favor of Symfony's PHPUnit Bridge.
Instead:
Remove it now: composer remove --dev phpunit/phpunit
Use Symfony's bridge: composer require --dev phpunit
So, I installed symfony/phpunit-bridge package.
It created bin/phpunit file and vendor/bin/simple-phpunit.
If I run bin/phpunit, it downloads phpunit project and installs its dependencies in bin/.phpunit/phpunit-6.5.
If I run vendor/bin/simple-phpunit, it downloads phpunit project and installs its dependencies in vendor/bin/.phpunit/phpunit-5.7.
Note that the versions are not the same. Why?
And why to not use composer and its autoloader? Now we have troubles with it and other tools like PHPStorm (broken phpunit debugging, etc).
I know, that I can add phpunit path to main composer autoload, but this method seems very dirty.
How to use phpunit in Symfony 4 proper way, with all debugging integrations, etc?
I have managed to fix this issue by adding phpunit.xml.dist in the root of the Symfony.
The file existed previously but I suspect that when I did remove phpunit, it also removed this file.. (I could be wrong but I am pretty certain)
When I add back the standard phpunit.xml.dist file to the root, it prompts me to add 'KERNEL_CLASS' variable in it.
When I add it in, it started working fine.
Full contents of my phpunit.xml.dist in case someone needs it
<?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/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_CLASS" value="App\Kernel" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
</phpunit>

Symfony Codeception functional test: environment variable not found

I'm using Codeception v2.4.1 with PHPUnit 7.1.3 in a new Symfony 4 project.
I created a functional test, and attempt to run it, and receive an error:
./vendor/bin/codecept run functional
There was 1 error:
---------
1) CustomerControllerCest: Customers on index
Test tests/functional/Controller/Internal/CustomerControllerCest.php:customersOnIndex
[Symfony\Component\DependencyInjection\Exception\EnvNotFoundException]
Environment variable not found: "CORS_ALLOW_ORIGIN".
I have a phpunit.xml file that has this environment configured:
<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="1" />
<env name="APP_SECRET" value="191a5940854807ee081eaa57aff30562" />
<env name="SHELL_VERBOSITY" value="-1" />
<!-- define your env variables for the test env here -->
<env name="CORS_ALLOW_ORIGIN" value="^https?://localhost:?[0-9]*$" />
</php>
I also tried using .env.test, and included this same variable:
CORS_ALLOW_ORIGIN=^https?://localhost:?[0-9]*$
I checked in functional.suite.yml and my environment is set to test:
actor: FunctionalTester
modules:
enabled:
- Symfony:
app_path: 'src'
environment: 'test'
em_service: ‘doctrine.orm.entity_manager’
# - Doctrine2:
# depends: Symfony
# add a framework module here
- \Helper\Functional
Not sure where the issue is. Is there an additional config where I tell codeception where to look for these environment variables?
Use --bootstrap option, ex.
--bootstrap=config/bootstrap.php
For Symfony 4
I used CORS_ALLOW_ORIGIN="*" php codecept run and it solved my problem.

Symfony2 phpunit run tests on different environment

I'm testing email sending in my symfony2 application, I have custom environment that is properly set with email settings and it's able to actually sent emails. I've created environment test_emails and I want to run my phpunit tests with it.
phpunit doesn't seem to have this functionality, so my only option is probably create custom bootstrap file, but I can't find anywhere how to do it.
The doc about Environments says:
The test environment is used when writing functional tests and is not accessible in the browser directly via a front controller. In other words, unlike the other environments, there is no app_test.php front controller file.
So when testing emails you're supposed to use functional test which is well described in How to Test that an Email is Sent in a Functional Test.
If you want to write a custom bootstrap with some simple logic see: How to Customize the Bootstrap Process before Running Tests
Maybe you can actually customize what environment the test harness is using by creating a custom test case that boots the kernel just like here How can I test a service in symfony2? and then passing an array of options to bootKernel() method. See:
https://github.com/symfony/framework-bundle/blob/master/Test/KernelTestCase.php#L141
https://github.com/symfony/framework-bundle/blob/master/Test/KernelTestCase.php#L168
If your using Symfonys WebtestCase you can set the environment as option:
$client = static::createClient(array('environment' => 'test-emails'));
I've ended up creating command that sends all emails I wanted and I can easily switch between environments.
Example:
php app/console app:emails:test-emails --env=dev
Use different phpunit.xml configuration files for each environment.
.
├── phpunit_test_emails.xml
└── phpunit_other_env.xml
Run the tests with specific configuration:
$ phpunit -c phpunit_test_emails.xml
You can set different PHP configurations in the XML configuration file.
You create a custom bootstrap via the bootstrap attribute where additional initialisations can be done if needed.
<phpunit bootstrap="test/bootstrap.php">
<!-- ... -->
<php>
<includePath>.</includePath>
<ini name="foo" value="bar"/>
<const name="foo" value="bar"/>
<var name="foo" value="bar"/>
<env name="foo" value="bar"/>
<post name="foo" value="bar"/>
<get name="foo" value="bar"/>
<cookie name="foo" value="bar"/>
<server name="foo" value="bar"/>
<files name="foo" value="bar"/>
<request name="foo" value="bar"/>
</php>
</phpunit>
See the PHPUnit Manual for more details.

How to resolve ConfigurationError error 'Unknown directive' in Plone?

I created new plone site after that I created new product using the following command
../bin/zopeskel plone_basic dummy.work
I created browser folder under ../dummy.work/src/dummy/work .then I configured the browser folder in configure.zcml file as like below.
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:five="http://namespaces.zope.org/five"
xmlns:i18n="http://namespaces.zope.org/i18n"
xmlns:browser="http://namespaces.zope.org/browser"
xmlns:plone="http://namespaces.plone.org/plone"
xmlns:zcml="http://namespaces.zope.org/zcml"
xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
i18n_domain="dummy.work">
<five:registerPackage package="." initialize=".initialize" />
<plone:static
directory="browser"
type="work"
name="dummy.work"
/>
<genericsetup:registerProfile
name="default"
title="dummy.work"
directory="profiles/default"
description="Installs the dummy.work package"
provides="Products.GenericSetup.interfaces.EXTENSION"
/>
<include package=".browser" />
</configure>
when i try to create an instance i am getting the error like
ConfigurationError: ('Unknown directive', u'http://namespaces.plone.org/plone', u'static')
can any one help me to solve this problem
To use the plone:static directive you must include the meta.zcml.
It's documented in the package documentation at https://pypi.python.org/pypi/plone.resource/
You must do <include package="plone.resource" file="meta.zcml"/> before you can use the plone:static directive.
So: add the line <include package="plone.resource" file="meta.zcml"/> before the usage of plone:static.

Resources