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>
Related
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.
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
I tried running PHP in my code and it shows
$ ./vendor/bin/phpunit
PHP Fatal error: Call to undefined method PHP_CodeCoverage_Filter::addDirectoryToBlacklist() in /home/petra/myproject/vendor/phpunit/phpunit/src/TextUI/TestRunner.php on line 979
Previously it's running normal.
Here's in the composer.json
"require-dev": {
...
"phpunit/phpunit": "4.6.*",
...
}
Here's the content of phpunit.xml.
<phpunit bootstrap="./test/bootstrap.php">
<testsuites>
<testsuite name="Test">
<directory>./test</directory>
</testsuite>
</testsuites>
</phpunit>
The content of ./test/bootstrap.php is just this
require dirname(__FILE__) . '/../vendor/autoload.php';
What did I do wrong?
Just tried manually set the phpunit/php-code-coverage dependency to 2.2.4 and this works.
I'm using PHPUnit with Mockery, the latter i installed via composer:
"require-dev": {
"mockery/mockery": "0.9.*"
},
Now, consider the following test case
<?php
use Mockery as m;
class FooTest extends PHPUnit_Framework_TestCase {
function testFoo() {
m::mock('DateTime');
}
}
phpunit run fine. Now consider the following:
use Mockery as m;
/**
* #runTestsInSeparateProcesses
* #preserveGlobalState disabled
*/
class FooTest extends PHPUnit_Framework_TestCase {
function testFoo() {
m::mock('DateTime');
}
}
In this case i get a Class 'Mockery' not found exception.
Mockery is not found - actually, nothing in the /vendor directory is found, as if the composer autoloading is totally messed up. I've ran both composer update and composer dump-autoload.
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
I'm using PHPUnit 3.7.10 and i run tests with command line phpunit without any args.
How can i fix this?
Upgrading to PHPUnit 4.x fixed this issue for me.
My phpunit xml:
<phpunit bootstrap="bootstrap.php">
<testsuites>
<testsuite name="Phase Unit Tests">
<directory>./Phase</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">Api/app/library</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="build/report" charset="UTF-8"
highlight="false" lowUpperBound="35" highLowerBound="70"/>
<log type="testdox-html" target="build/testdox.html"/>
</logging>
</phpunit>
My test run successfully and I am not getting errors. I have checked for any DIE() and EXIT calls and did not find any in my code. When I look at my report it is just the empty folders with no files being scanned.
I am using PHP_CodeCoverage 1.2.7 using PHP 5.4.3 and PHPUnit 3.7.13 on Windows 8 with Wamp server 2.2
Ok so I wrote a small thing to test PHPUnit coverage reporting... Below is my classes:
/**
* test
*/
class Application
{
public $testprop;
function __construct()
{
$this->testprop = "Stuff";
}
}
/**
* Tests for the application class
*/
class ApplicationTest extends PHPUnit_Framework_TestCase
{
public function testRunTheThing()
{
$app = new Application();
$this->assertEquals($app->testprop, 'Stuff');
}
}
I run the following in my command prompt:
phpunit --coverage-html report ApplicationTest.php
The test passes but the code coverage report generated is of the composer ClassLoader file.
It seems to have been an issue with the versions of phpunit and its dependencies. I ran an update on phpunit and now it is generating the coverage reports successfully. I am now using PHP_CodeCoverage 1.2.16, PHP 5.4.3 and PHPUnit 3.7.32.