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.
Related
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.
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.
I need to run tests again an existing database in Drupal 8.
I already tried to replace default sqllite connection info with the right mysql database's info in phpunit.xml
Here's my phpunit.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
bootstrap="test/bootstrap.php"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
strictConfigSchema="false">
<testsuites>
<testsuite name="drupal-composer-project tests">
<directory>./test/</directory>
</testsuite>
</testsuites>
<php>
<!-- Set error reporting to E_ALL. -->
<ini name="error_reporting" value="32767"/>
<!-- Do not limit the amount of memory tests take to run. -->
<ini name="memory_limit" value="-1"/>
<env name="SIMPLETEST_BASE_URL" value="http://localhost"/>
<env name="SIMPLETEST_DB" value="mysql://user:password#mysql/databasename"/>
<env name="BROWSERTEST_OUTPUT_DIRECTORY" value=""/>
</php>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<listeners>
<listener class="\Drupal\Tests\Listeners\DrupalListener">
</listener>
<!-- The Symfony deprecation listener has to come after the Drupal listener -->
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
</listener>
</listeners>
<logging>
<log type="coverage-html" target="../../coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-clover" target="../../clover.xml"/>
<log type="coverage-crap4j" target="../../crap4j.xml"/>
</logging>
</phpunit>
When I log database information, I see that a test prefix is added, and this prefix will create tables inside my existing database. How can I make the tests run directly on my own database and tables ?
If you want to run tests on the existing database, you need to use Drupal Test Traits. Here's some introduction text for this testing framework.
But, as #DirkScholten said, you should avoid run tests on the existing database. An exception is running User Acceptance Tests, they need data from the production database in order to simulate an end-user experience. In this case, ensure that you run tests in isolate environment, so as not tests send some mail to the real user or do some changes via API on real servers.
If you just need some custom settings or field on you Drupal's test instance, then a better option is to create your own installation profile and use them in your tests. Here's instructions.
The third option is to use config_installer and doing tests on custom site configuration. From Drupal 8.6 you don't need this module, it's in the Drupal's core.
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>
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.