Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Autoload class in Tests subfolder #30546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
clavat opened this issue Mar 13, 2019 · 3 comments
Closed

Autoload class in Tests subfolder #30546

clavat opened this issue Mar 13, 2019 · 3 comments

Comments

@clavat
Copy link

clavat commented Mar 13, 2019

Symfony version(s) affected: 4.2.4

Description
I implemented a TestSuite based on this documentation with PHPUnit bridge on 4.2.4
https://symfony.com/doc/current/components/phpunit_bridge.html
But I tried to organize my tests class with some folders and abstract parent class

How to reproduce
Create a "tests" folder, with inside it a new "tools" folder.

Inside the "tests" folder create a class like this :

namespace App\Tests;

use App\Tests\Tools\PHPUnitFrameworkTestCaseTools;

class FormProductBusinessTestTools extends PHPUnitFrameworkTestCaseTools
{
........

Inside the "tools" folder create a class like this :

namespace App\Tests\Tools;

abstract class PHPUnitFrameworkTestCaseTools extends TypeTestCase
{
........

Possible Solution
Solution i found for the moment is to add extra bootstrap to include this tools class :

function testLoader($class_name) {
    if(strpos($class_name, 'Tests\Tools') !== FALSE) {
        require_once('tools/'.getName($class_name).'.php');
    }
}

Additional context

stan@SERVER:/var/www/html/project_test$ php bin/phpunit
#!/usr/bin/env php
PHP Fatal error:  Uncaught Error: Class 'App\Tests\Tools\PHPUnitFrameworkTestCaseTools' not found in /var/www/html/project_test/tests/FormProductTest.php:16
Stack trace:
#0 /var/www/html/clavat/project_test/bin/.phpunit/phpunit-6.5/src/Util/Fileloader.php(64): include_once()
#1 /var/www/html/project_test/bin/.phpunit/phpunit-6.5/src/Util/Fileloader.php(48): PHPUnit\Util\Fileloader::load('/var/www/html/c...')
#2 /var/www/html/project_test/bin/.phpunit/phpunit-6.5/src/Framework/TestSuite.php(325): PHPUnit\Util\Fileloader::checkAndLoad('/var/www/html/c...')
#3 /var/www/html/project_test/bin/.phpunit/phpunit-6.5/src/Framework/TestSuite.php(403): PHPUnit\Framework\TestSuite->addTestFile('/var/www/html/c...')
#4 /var/www/html/project_test/bin/.phpunit/phpunit-6.5/src/Util/Configuration.php(1014): PHPUnit\Framework\TestSuite->addTestFiles(Array)
#5 /var/www/html/project_test/bin/.phpunit/phpunit-6.5/src/Util/Configuration.php(910): PHPUnit\Util\Configuration->getTe in /var/www/html/project_test/tests/FormProductTest.php on line 16
@xabbuh
Copy link
Member

xabbuh commented Mar 13, 2019

If you want to rely on the default autoloading, the directory name must match the namespace name (i.e. your directory should be named Tools instead of tools). Otherwise you will need to adapt your autoloading configuration in the composer.json file.

@clavat
Copy link
Author

clavat commented Mar 13, 2019

Good point.

But why folder tests works with tests instead of Tests ?
Outside of src folder, all folders have lower case name, why for tests\Tools ? that doesn't make sens ?

@clavat
Copy link
Author

clavat commented Mar 13, 2019

So, I agree with the answer provided by @xabbuh.
This is not a bug, but an understanding miss from me :)

Thanks

@clavat clavat closed this as completed Mar 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants