Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a base test-class for PHPUnit and extending it for common functionality results in class not found error

I'm running PHPUnit using a bootstrap file for autoloading classes (generated by composer).

All my tests load up classes just fine, but for two of my tests, I made a "base" test class which extends \PHPUnit\Framework\TestCase (similar to PHPUnit_Framework_TestCase before PHPUnit7), and then two test classes that extend the base class, similar structure to the following example code:

    abstract class BaseTest extends \PHPUnit\Framework\TestCase     {         abstract function setUp();          protected function getCommonTestVariables()         {             // ...         }          protected function runCommonTests()         {             // ...         }     } 
    class BlahBlahTest extends BaseTest     {         public function setUp()         {              $variables=$this->getCommonTestVariables();              //etc...         }          public function testThings()         {             $this->runCommonTests();         }     } 

Whenever I run this, PHPUnit gives an error:

Fatal error: Class 'BaseTest' not found in BlahBlahTest.php on line 13

I've checked filenames, locations, namespaces and everything seems to be in order. Any help would be appreciated to get to the bottom of this

like image 950
r.bilgil Avatar asked Sep 18 '13 18:09

r.bilgil


People also ask

Which method is used to create a mock with PHPUnit?

PHPUnit provides methods that are used to automatically create objects that will replace the original object in our test. createMock($type) and getMockBuilder($type) methods are used to create mock object. The createMock method immediately returns a mock object of the specified type.

How do I run a PHPUnit test?

How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.

What is a PHPUnit test?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.

What is the use of PHPUnit?

PHPUnit is a framework independent library for unit testing PHP. Unit testing is a method by which small units of code are tested against expected results. Traditional testing tests an app as a whole meaning that individual components rarely get tested alone.


2 Answers

I ran into the same problem and if you are not too familiar with the inner workings of both PHPUnit and Composer this can indeed seem perplexing.

PHPunit does not use use the Composer autoloader to find any of your test classes. It just scans any directory you give it and operates on one file at a time.

Hence it does not know about any other class than the one in the file it is currently operating on. That is where the bootstrap file comes into play.

If you want to use the Composer Autoloader to load other test classes, you need to tell it where it can find these test classes (and, optionally, in which namespace).

There are two ways to do this:

  1. Add an autoload-dev section to your composer.json or
  2. Add the test directory to the Composer Autoloader

Use autoload-dev

The autoload-dev sections allows you to define autoload rules for development purposes.

Quoting directly from the manual:

Classes needed to run the test suite should not be included in the main autoload rules to avoid polluting the autoloader in production and when other people use your package as a dependency.

Therefore, it is a good idea to rely on a dedicated path for your unit tests and to add it within the autoload-dev section.

Example:

{     "autoload": {         "psr-4": { "MyLibrary\\": "src/" }     },     "autoload-dev": {         "psr-4": { "MyLibrary\\Tests\\": "tests/" }     } } 

Add to the Composer Autoloader

An alternative would be to get the Composer Autoloader and add your testing namespace (if you have any) and the directory where your tests live. How to do this, as described in the manual (at the bottom of the autoloading section in "Basic Usage") is :

$loader = require('/path/to/vendor/autoload.php'); $loader->add('Test\\', __DIR__ . '/Tests'); 

If your tests use namespaces that mirror the test directory and you still run into trouble, you can try omitting the prefix by replacing the first parameter ('Test\\') with ''.


If you want further insight into how all of this works you should take a look at the Composer ClassLoader class, especially the add() and findFile() methods.

like image 165
Potherca Avatar answered Sep 23 '22 08:09

Potherca


For me the solution was much simpler.

  1. I changed the capital letter of Test to test at the end of the file and the class name

BaseSomethingtest.php

<?php  namespace Something\Tests\Sub1\Sub2;  class BaseSomethingtest { } 
  1. I just put a word at the end of my base class. As far as it didn't finish with Test, phpunit didn't call it

BaseSomethingTestCase.php

<?php  namespace Something\Tests\Sub1\Sub2;  class BaseSomethingTestCase { } 
like image 27
Zoltán Süle Avatar answered Sep 23 '22 08:09

Zoltán Süle