Unable to extend controller test class

I have a Base class that needs to be extended using controller tests:

//Base.php namespace AppBundle\Tests\System; abstract class Base extends \PHPUnit_Framework_TestCase { ... } 

But when I try to expand it,

 //DefaultControllerTest.php namespace AppBundle\Tests\Controller; use AppBundle\Tests\System\Base; class DefaultControllerTest extends Base { ... } 

I get this error:

/usr/bin/php/tmp/ide-phpunit.php --configuration / server / project / phpunit.xml / server / project / src / AppBundle / Tests Testing started at 18:36 ... PHP Fatal error: Class' AppBundle \ Tests \ System \ Base 'not found in /server/project/src/AppBundle/Tests/Controller/DefaultControllerTest.php on line 7

Process terminated with exit code 255

PhpStorm detects the Base class in DefaultController.php , so it does not look like a typo.

This is my phpunit.xml :

 <?xml version="1.0" encoding="UTF-8"?> <phpunit backupGlobals = "false" backupStaticAttributes = "false" colors = "true" convertErrorsToExceptions = "true" convertNoticesToExceptions = "true" convertWarningsToExceptions = "true" processIsolation = "false" stopOnFailure = "false" syntaxCheck = "false" bootstrap = "app/bootstrap.php.cache" > <testsuites> <testsuite name="Tests"> <directory>src/AppBundle/Tests</directory> </testsuite> </testsuites> <php> <server name="KERNEL_DIR" value="app" /> </php> <groups> <exclude> <group>slow</group> </exclude> </groups> <!-- This is for code coverage --> <filter> <whitelist> <directory>app</directory> <directory>src</directory> <exclude> <directory>app/cache/*</directory> <file>app/check.php</file> </exclude> </whitelist> </filter> </phpunit> 

Any idea on what I am missing?

0
source share
2 answers

I believe you can really tell phpunit to use the composer autoloader directly:

 bootstrap="app/autoload.php" 

Instead of "web/app_test.php"

This is for Symfony> = 2.8, for previous versions I think it will be "vendor/autoload.php"

+1
source

As @malcolm and @Ilya Yarkovets point out, I need to enable the test autoloader. Therefore, I created the app_test.php file inside the web directory with this configuration:

 use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Debug\Debug; /** * @var Composer\Autoload\ClassLoader $loader */ $loader = require __DIR__.'/../app/autoload.php'; Debug::enable(); $kernel = new AppKernel('test', true); $kernel->loadClassCache(); $request = Request::createFromGlobals(); $response = $kernel->handle($request); $response->send(); $kernel->terminate($request, $response); 

and changed this line in phpunit.xml :

 bootstrap = "web/app_test.php" > 

I'm not sure if app_test.php should be changed, but now it seems to work as expected.

+2
source

Source: https://habr.com/ru/post/977550/


All Articles