In the context of Phpunit, I have been adding a few functions in my Test class that I am planning to reuse in other tests.
My plan was to have another class residing in tests folder to inherit from. Class was to inherit classic TestCase and add my extra methods. I am not getting any error in IDE but Phpunit can't find custom class and crashes with an error message.
I tried putting it in a Trait. Not working either.
At the same I was testing that I could create a "User" class in same folder and I was able to instantiate it fine so I stopped tweaking the namespace as it didn't seem to be the issue. I have ended up creating a class with static methods and calling methods statically.
But I am very curious to understand why it is behaving that way. It's the first time I am facing such a situation. I can instantiate classes in a file but not extend or use them as a trait.
Here is the class that I am trying to have my test inherit from :
<?php
use PHPUnit\Framework\TestCase as BaseTestCase;
class TestCase extends BaseTestCase
{
public static function doSomething($params)
{
// ...
}
}
Here is the test class calling it :
<?php
use Model\Course;
//use PHPUnit\Framework\TestCase;
class CourseTest extends TestCase
public function setUp(): void
{
$this->student = new Student();
}
}
Following the link provided by @hakre, I have been toying with the composer autoload and namespaces like so :
"autoload-dev": {
"psr-4": { "Tests\\": "tests/" }
},
then
"autoload-dev": {
"psr-4": { "": "tests/" }
},
I have changed the namespace of the custom TestCase class accordingly (adding "Test/" to namespace). I have tried testing it without changing the namespace of the CourseTest, but to no avail. The only difference is that when I link the "empty" namespace to tests directory in autoload, the Student class is recognized and instantiated in CourseTest provided I have it extend standard Phpunit TestCase class, as soon as I have it extend my custom TestCase class I have the following error again :
Class "TestCase" not found in phar://G:/dev/phpbin/phpunit-9.5.10.phar/phpunit/TextUI/Command.php:95