This is my PhpUnit test class:
<?php
namespace tests\AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class SendEmailControllerTest extends WebTestCase
{
public function testMailIsSentAndContentIsCorrect()
{
$client = static:: createClient();
...
}
}
But when I try to run it, I get the error whose trace is:
Unable to guess the Kernel directory.
C:\myProject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Test\KernelTestCase.php:62
C:\myProject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Test\KernelTestCase.php:138
C:\myProject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Test\KernelTestCase.php:184
C:\myProject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Test\KernelTestCase.php:165
C:\myProject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Test\WebTestCase.php:33
C:\myProject\tests\AppBundle\Controller\SendEmailControllerTest.php:12
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Remaining deprecation notices (3)
1x: Using the KERNEL_DIR environment variable or the automatic guessing based on the phpunit.xml / phpunit.xml.dist file location is deprecated since Symfony 3.4. Set the KERNEL_CLASS environment variable to the fully-qualified class name of your Kernel instead. Not setting the KERNEL_CLASS environment variable will throw an exception on 4.0 unless you override the :createKernel() or :getKernelClass() method.
1x in SendEmailControllerTest::testMailIsSentAndContentIsCorrect from tests\AppBundle\Controller
1x: The Symfony\Bundle\FrameworkBundle\Test\KernelTestCase::getPhpUnitXmlDir() method is deprecated since Symfony 3.4 and will be removed in 4.0.
1x in SendEmailControllerTest::testMailIsSentAndContentIsCorrect from tests\AppBundle\Controller
1x: The Symfony\Bundle\FrameworkBundle\Test\KernelTestCase::getPhpUnitCliConfigArgument() method is deprecated since Symfony 3.4 and will be removed in 4.0.
1x in SendEmailControllerTest::testMailIsSentAndContentIsCorrect from tests\AppBundle\Controller
C:\xampp\php\php.exe C:/myProject/vendor/phpunit/phpunit/phpunit --no-configuration tests\AppBundle\Controller\SendEmailControllerTest C:\myProject\tests\AppBundle\Controller\SendEmailControllerTest.php --teamcity
Testing started at 23:09 ...
PHPUnit 5.6.0 by Sebastian Bergmann and contributors.
Process finished with exit code 2
But the problem is that I don't understand how to fix the deprecation issues indicated in the trace...
Edit:
Following the official documentation, I've added the in the 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/4.8/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="App\Kernel" />
<server name="KERNEL_CLASS" value="AppKernel" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
<exclude>
<directory>src/*Bundle/Resources</directory>
<directory>src/*/*Bundle/Resources</directory>
<directory>src/*/Bundle/*Bundle/Resources</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
However:
1) I still get the same error
2) The code http://schema.phpunit.de/4.8/phpunit.xsd appears in red, with the message "URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs). So I went to that place and in "External Schemas and DTDs" I added that URI, and the location being my project root. However, this red warning persists and I suspect it is related to the location, but where is it?
As documentation states
To run your functional tests, the WebTestCase class needs to know
which is the application kernel to bootstrap it. The kernel class is
usually defined in the KERNEL_CLASS environment variable (included
in the default phpunit.xml.dist file provided by Symfony):
<?xml version="1.0" charset="utf-8" ?>
<phpunit>
<php>
<!-- the value is the FQCN of the application kernel -->
<env name="KERNEL_CLASS" value="App\Kernel" />
</php>
<!-- ... -->
</phpunit>
Setting this in phpunit.xml.dist make it work for me with symfony 3.4
<php>
<!-- the value is the FQCN of the application kernel -->
<env name="KERNEL_CLASS" value="AppKernel" />
</php>
Nikita U. is correct here, but I wanted to point out a couple small things that might help someone else in the future, especially location.
According to the many of the notes I have found, especially the comments from the repo, this should be <server name ..., but I have found that <env name ... works, too. This should be located in the <php> block of the phpunit.xml.dist or phpunit.xml files (only one of them should be used) in the root directory of the project. If neither file is there, create one (either seem to work)and use Nikita's example for the text.
Also, note that as of Symfony 4 with Flex, the value is "App\Kernel", not "AppKernel". More can be found here: https://github.com/symfony/symfony-standard/issues/1073
Related
I'm using PHPUnit, PHPStorm.
Now, my actual file being tested is located in root->foo->config
Next, if I select the file I want to test, and right click, new phptest, I get the screen below:
So everything looks ok so far. Everything is pre-entered automatically.
So my ip_require.php goes into root->tests->foo
My test file contains the following:
<?php
namespace config;
use foo\ip_request;
use PHPUnit\Framework\TestCase;
class ip_requestTest extends TestCase
{
public function testGetRealIpAddr()
{
$local = new ip_request();
$this->assertEquals('127.0.0.1', $local->getRealIpAddr());
}
}
Inside my phpunit.xml, I have:
<?xml version="1.0" encoding="UTF-8"?>
<target name="test">
<phpunit
bootstrap="tests/bootstrap.php"
colors="true"
> <!-- changed from vendor/autoload.php-->
<testsuites>
<testsuite name = "Website">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<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="SHELL_VERBOSITY" value="-1" />
</php>
</phpunit>
</target>
And all I have inside my bootstrap.php is
ini_set('display_errors', true);
error_reporting(E_ALL);
include $_SERVER['DOCUMENT_ROOT'].'\foo\ip_requestTest.php';
I Get the errors on the bootstrap.php preview:
Everything I hover over looks to be pointing in the right direction on the classes.
If I click and follow the class from the test file, it takes me to the correct location
My PHPUnit is installed to the latest version, verified in the command line, and inside composer.json
"require": {
"phpunit/phpunit": "9.3",
"ext-mysqli": "*"
},
Any ideas? I've run out of links to try...
I found a fix, to get past the bootstrap.php page at least.
include __DIR__ . '/../foo/config/ip_request.php';
I then still had a similar error message in my terminal, but the thing that finally got my tests working was I made sure that composer.json had the same version of PHPUnit.
Once they where the same it worked!
I am working on a task that is expose a xml file with given url. I want to test my code and I do this with PHPUnit. I used composer to install PHPUnit.
The example test that I want to run is this :
<?php
use PHPUnit\Framework\TestCase;
class IndexTest extends TestCase{
public function testGetXmlWithUrl(){
require 'index.php';
$XmlClass = new XmlReaderClass("localhost", "8000", "status.xml", "");
$url= $XmlClass->$url;
$myUrl = "http://localhost:8000/status.xml?password=";
$this->assertEquals($myUrl, $url);
}} ?>
TestCase class cannot be added. I mean even PhpUnit name is not colored in blue in the code. So I guess I could not include it in my project actually. Error is like:
Fatal error: Uncaught Error: Class 'PHPUnit\Framework\TestCase' not found in /Users/demetsen/Desktop/tests/IndexTest.php:4
My second problem is when I try to run testGetXmlWithUrl() method the result is :
Failed asserting that SimpleXMLElement Object (...) matches expected '\n ....
Why am I getting this result and how can I solve it?
A default phpunit.xml (or better, use phpunit.xml.dist, so you can copy/edit it locally if needed), can be created with phpunit --generate-configuration will include a line for bootstrap:
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
bootstrap="path/to/bootstrap.php"
<!-- more lines -->
</phpunit>
Here, the bootstrap="...." line can, at its simplest, point to the composer autoloader. PHPunit can also create a suitable file for you.
$ php ./phpunit-9.2.phar --generate-configuration # or vendor/bin/phpunit, via composer
PHPUnit 9.2.6 by Sebastian Bergmann and contributors.
Generating phpunit.xml in /home/username/code/test
Bootstrap script (relative to path shown above; default: vendor/autoload.php):
Tests directory (relative to path shown above; default: tests):
Source directory (relative to path shown above; default: src):
Generated phpunit.xml in /home/username/code/test
# optional, but useful:
# mv phpunit.xml phpunit.xml.dist # .dist will be read if .xml does not exist
The output file:
$ cat phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.2/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
If you had something you needed to add to the bootstrap process, create a file with your needs, and you'd probably also add something like require __DIR__.'/vendor/autoload.php'; to setup the autoloading.
I created fresh Symfony 4.1.x project, but when I'm trying to create and execute functional test I am getting error:
PHP Fatal error: Class
'Symfony\Bundle\FrameworkBundle\Test\WebTestCase' not found in
/home/tomasz/my_project/tests/ExampleControllerTest.php on line 7
Steps to reproduce:
Create project
composer create-project symfony/skeleton my_project
Install phpunit, functional tests components and maker for generating skeleton of functional test
composer req --dev symfony/phpunit-bridge symfony/css-selector symfony/browser-kit symfony/maker-bundle
Create example functional test
bin/console make:functional-test ExampleControllerTest
Content of created file:
<?php
namespace App\Tests;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ExampleControllerTest extends WebTestCase
{
public function testSomething()
{
$client = static::createClient();
$crawler = $client->request('GET', '/');
$this->assertSame(200, $client->getResponse()->getStatusCode());
$this->assertContains('Hello World', $crawler->filter('h1')->text());
}
}
Run tests
bin/phpunit
Output (after installing phpunit dependencies):
PHP Fatal error: Class
'Symfony\Bundle\FrameworkBundle\Test\WebTestCase' not found in
/home/tomasz/my_project/tests/ExampleControllerTest.php on line 7
I checked and class Symfony\Bundle\FrameworkBundle\Test\WebTestCase does exist in vendor/. Any clues?
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/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
</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>
In my phpunit I have this configuration:
<php>
<env name="ENVIRONMENT" value="test"/>
</php>
I worked under the false assumption that it always would set the environment variable to the value test. Yet when the system already has the variable set, then it prefers the already existing value.
$ export ENVIRONMENT=GNARF
$ phpunit -c test/phpunit.xml
So inside the tests the value of env('ENVIRONMENT') will be "GNARF" even though I expected "test".
Is there a way to make phpunit to treat the env setting not as a default but rather as the definite value it should be using?
I also would like to avoid calling phpunit in a certain way just to get the right env variables.
So while this works:
ENVIRONMENT="test";./vendor/bin/phpunit -c tests/phpunit.xml
I rather configure it inside the phpunit.xml file if possible.
This way worked for me (tested with PHPUnit 6.5):
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="MyProject">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="API_KEY" value="fakeApiKey" force="true" />
</php>
</phpunit>
Here's the pull request where they added this new feature (in case you're interested).
sebastianbergmann added this to the PHPUnit 6.3 milestone on 4 Jul 2017
The environment variables are set in PHPUnit_Util_Configuration::handlePHPConfiguration() and indeed, it looks like this:
foreach ($configuration['env'] as $name => $value) {
if (false === getenv($name)) {
putenv("{$name}={$value}");
}
if (!isset($_ENV[$name])) {
$_ENV[$name] = $value;
}
}
As it seems, there is no way to circumvent this check without unsetting the variable first. And while you're at it, you could as well set it to the desired value immediately.
You can set the env vars from the bootstrap file. The bootstrap file can be selected from the phpunit.xml file so the result would be what you're looking for.
I already made test for my application module and other module. They are working fine but i want to run all the test (application module and other module) toguether to generate clover report for jenkins. What should i do?? create another config file to invoke other config files??
---edit---
I have the same code for each module bootstrap.php and i want to use the same for each module to avoid code duplication. Rigth now i have two modules Application and Problem when i run phpunit it throws me this error:
**.PHP Fatal error: Class 'ProblemTest\Bootstrap' not found ....**
The test for the Application module works fine but the test for Problem module doesn't work for the namespace declaration in the phpunit_bootstrap.php file.
My Application test uses this declaration:
<?php
namespace ApplicationTest\Controller;
use ApplicationTest\Bootstrap; ....
My Problem tes uses this declaration:
<?php
namespace ProblemTest\Controller;
use ProblemTest\Bootstrap;
This is my phpunit.xml
<phpunit bootstrap="phpunit_bootstrap.php">
<testsuites>
<testsuite name="ALL">
<directory>module/Application/test</directory>
<directory>module/Problem/test</directory>
</testsuite>
</testsuites>
this is my phpunit_bootstrap.php
<?php
namespace ApplicationTest;
The problem that i have rigth now to run all the test toguether is how to include the same bootstrap for every test to avoid that exception.
You can make a testsuite to run a bunch of individual groups of tests all at once. In your phpunit.xml.dist file:
<phpunit bootstrap="phpunit_bootstrap.php">
<testsuites>
<testsuite name="all">
<directory>./</directory>
</testsuite>
<testsuite name="ALL">
<directory>/path/to/module1tests</directory>
<directory>/path/to/module2tests</directory>
<directory>/path/to/module3tests</directory>
<exclude>/path/to/module4tests</exclude>
</testsuite>
<testsuite name="module1only">
<directory>./module1tests</directory>
</testsuite>
</testsuites>
And then you can run it with: /path/to/phpunit --testsuite="ALL"
I can't comment yet so another solution to this issue can be found at zend framework 2 + phpunit + multiple modules + continuous integration If you don't want to go through naming each module independently.