-2

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
Bevelopper
  • 75
  • 1
  • 9
  • Can you post any example code? Does it throw any errors? You said you were able to instantiate other classes, but it sounds like you either need to `require` the base class, or there's something strange happening with your namespaces. – 404 Not Found Oct 29 '21 at 02:12
  • 1
    Does this answer your question? https://stackoverflow.com/q/18880067/367456 – hakre Oct 31 '21 at 12:42
  • Thank you @hakre, there was food for thought in that link. But the solution was rather in a link provided to another SO page. I will post the solution to issue now. – Bevelopper Nov 09 '21 at 16:48

1 Answers1

0

My class was named TestCase like its parent. I thought the Test class had to inherit from another Test class, and it would be enough. and for phpunit, it is not sufficient that one class extends another.

The parent class name needs to end with the "Test.php" suffix. OK,I didnt realize it also used its own method to call parent classes... But the other thing I found, really surprised me.

First I thought that some reserved class names were not allowed. Like Test or ParentTest. I also tried less obvious to weird class names that were not allowed either.

Some names worked, like CaseTest or CommonTest, others didn't, And after several attempts, I came to a conclusion, and I don't know what to make of it.

It seems that only class names starting with letter A, B or C and ending with "Test" work . ATest, BTest, CTest, ClassTest, CatTest, AllowedTest, BadTest will work. I was unable to make any other class name work...

Maybe there are more but I tested others like P, X, Y and Z and they dit not.

Bevelopper
  • 75
  • 1
  • 9