Autoloading classes in PHPUnit using Composer and autoload.php
Asked Answered
B

5

70

I have just installed PHPUnit version 3.7.19 by Sebastian Bergmann via Composer and have written a class I would like to unit test.

I would like to have all my classes autoloaded into each unit test without having to use include or require at the top of my test but this is proving to be difficult!

This is what my directory structure looks like (a trailing / slash indicates a directory, not a file):

* composer.json
* composer.lock
* composer.phar
* lib/
    * returning.php
* tests/
    * returningTest.php
* vendor/
    * bin/
        * phpunit
    * composer/
    * phpunit/
    * symfony/
    * autoload.php

My composer.json file includes the following:

"require": {
    "phpunit/phpunit": "3.7.*",
    "phpunit/phpunit-selenium": ">=1.2"
}

My returning.php class file includes the following:

<?php
class Returning {
    public $var;
    function __construct(){
        $this->var = 1;
    }
}
?>

My returningTest.php test file includes the following:

<?php
class ReturningTest extends PHPUnit_Framework_TestCase
{
    protected $obj = null;
    
    protected function setUp()
    {
        $this->obj = new Returning;
    }
    
    public function testExample()
    {   
        $this->assertEquals(1, $this->obj->var);
    }

    protected function tearDown()
    {
        
    }
}
?>

However, when I run ./vendor/bin/phpunit tests from the command-line, I get the following error:

PHP Fatal error: Class 'Returning' not found in /files/code/php/db/tests/returningTest.php on line 8

I noticed that composer produced an autoload.php file in vendor/autoload.php but not sure if this is relevant for my problem.

Also, in some other answers on Stack Overflow people have mentioned something about using PSR-0 in composer and the namespace command in PHP, but I have not been successful in using either one.

Please help! I just want to autoload my classes in PHPUnit so I can just use them to create objects without worrying about include or require.


Update: 14th of August 2013

I have now created an Open Source project called PHPUnit Skeleton to help you get up and running with PHPUnit testing easily for your project.

Bostick answered 29/3, 2013 at 19:46 Comment(1)
thank you for organizing the question well.Gocart
L
80

Well, at first. You need to tell the autoloader where to find the php file for a class. That's done by following the PSR-0 standard.

The best way is to use namespaces. The autoloader searches for a Acme/Tests/ReturningTest.php file when you requested a Acme\Tests\ReturningTest class. There are some great namespace tutorials out there, just search and read. Please note that namespacing is not something that came into PHP for autoloading, it's something that can be used for autoloading.

Composer comes with a standard PSR-0 autoloader (the one in vendor/autoload.php). In your case you want to tell the autoloader to search for files in the lib directory. Then when you use ReturningTest it will look for /lib/ReturningTest.php.

Add this to your composer.json:

{
    ...
    "autoload": {
        "psr-0": { "": "lib/" }
    }
}

More information in the documentation.

Now the autoloader can find your classes you need to let PHPunit know there is a file to execute before running the tests: a bootstrap file. You can use the --bootstrap option to specify where the bootstrap file is located:

$ ./vendor/bin/phpunit tests --bootstrap vendor/autoload.php

However, it's nicer to use a PHPunit configuration file:

<!-- /phpunit.xml.dist -->
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="./vendor/autoload.php">

    <testsuites>
        <testsuite name="The project's test suite">
            <directory>./tests</directory>
        </testsuite>
    </testsuites>

</phpunit>

Now, you can run the command and it will automatically detect the configuration file:

$ ./vendor/bin/phpunit

If you put the configuration file into another directory, you need to put the path to that directory in the command with the -c option.

Lipchitz answered 29/3, 2013 at 20:58 Comment(8)
Wouter, thanks for the very detailed explanation. I tried these exact steps but I am still getting the error PHP Fatal error: Class 'Returning' not found in /files/code/php/db/tests/returningTest.php on line 8. How do I find out what I'm doing wrong?Bostick
@WouterJ <phpunit bootstrap="./vendor/autoload.php"> only sets up autoloader for classes under "lib/". You still need to autoload classes under "tests/".Vaporous
@ChuanMa no, PHPunit will scan through the tests/ directory and run the test classes, there are already autoloaded.Lipchitz
@jasdeepkhalsa Have you run "composer.phar install" after updating your composer.json file? It should work after that (it works for me). What that command does is to update this autoloader file (vendor/composer/autoload_namespaces.php) in order to autoload the Returning class. Changing composer.json alone isn't enough.Vaporous
Yes I ran composer.phar install but still nothing. It feels like ./vendor/autoload.php is not working, but I can't be sure. Ironically, I wonder how we can TEST where the problem is in my configuration!?Bostick
My problem was in not having the bootstrap="./vendor/autoload.php" in the phpunit.xml file. Now it autoloads fine! Thanks!Bostick
Remember to rerun composer install or composer update after including the psr entries in your config file, as suggested in composer documentation: getcomposer.org/doc/01-basic-usage.md#autoloadingArchduchess
Even after these instructions my autoloader just didn't work for tests, even though it worked fine for the production code. After several hours of struggling, I found that for my project the fix was to put each class into a separate file with the same name as the class. I prefer having closely-related classes in the same file, but I could not for the life of me find a way to get that working in my tests.Linnell
V
47

[Update2] Another simpler alternative approach is to use the autoload-dev directive in composer.json (reference). The benefit is that you don't need to maintain two bootstrap.php (one for prod, one for dev) just in order to autoload different classes.

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

[Update] Wouter J's answer is more complete. But mine can help people who want to set up PSR-0 autoloading in tests/ folder.
Phpunit scans all files with this pattern *Test.php. So we don't need to autoload them ourselves. But we still want to autoload other supporting classes under tests/ such as fixture/stub or some parent classes.

An easy way is to look at how Composer project itself is setting up the phpunit test. It's actually very simple. Note the line with "bootstrap".

reference: https://github.com/composer/composer/blob/master/phpunit.xml.dist

<?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="tests/bootstrap.php"
>
    <testsuites>
        <testsuite name="Composer Test Suite">
            <directory>./tests/Composer/</directory>
        </testsuite>
    </testsuites>

    <groups>
        <exclude>
            <group>slow</group>
        </exclude>
    </groups>

    <filter>
        <whitelist>
            <directory>./src/Composer/</directory>
            <exclude>
                <file>./src/Composer/Autoload/ClassLoader.php</file>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

reference: https://github.com/composer/composer/blob/master/tests/bootstrap.php

<?php

/*
* This file is part of Composer.
*
* (c) Nils Adermann <[email protected]>
* Jordi Boggiano <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

error_reporting(E_ALL);

$loader = require __DIR__.'/../src/bootstrap.php';
$loader->add('Composer\Test', __DIR__);

The last line above is autoloading phpunit test classes under the namespace Composer\Test.

Vaporous answered 30/3, 2013 at 7:28 Comment(2)
Also use "require-dev" block instead of "require" block to add phpunit packages in composer.json. You don't want phpunit code installed in production. See composer doc for more info.Vaporous
Worth mentioning that you should run composer dump-autoload --optimize after making this change. This regenerates the list of all classes that need to be included in the project.Darindaring
R
6

None of these answers were what I was looking for. Yes PHPUnit loads test files, but not stubs/fixtures. Chaun Ma's answer doesn't cut it because running vendor/bin/phpunit already includes the autoload, so there's no way to get an instance of the autoloader to push more paths to it's stack at that point.

I eventually found this in the docs:

If you need to search for a same prefix in multiple directories, you can specify them as an array as such:

{
    "autoload": {
        "psr-0": { "Monolog\\": ["src/", "lib/"] }
    }
}
Reno answered 29/9, 2013 at 15:42 Comment(2)
Yes. latest phpunit already includes composer autoloader. But my approach still works (we are using it for almost 2 years now). The benefit is that we don't want to autoload test classes in production. Here is another helpful SO link (answered by the composer maintainer): #12791181Vaporous
Note that even though vendor\bin\phpunit already require composer's autoloader class, it doesn't stop you require it again and get a $loader object to add new classes.Vaporous
I
5

There is a really simple way to set up phpunit with autoloading and bootstap. Use phpunit's --generate-configuration option to create your phpunit.xml configuration in a few seconds-:

vendor/bin/phpunit --generate-configuration

(Or just phpunit --generate-configuration if phpunit is set in your PATH). This option has been available from version phpunit5 and upwards.

This option will prompt you for your bootstrap file (vendor/autoload.php), tests and source directories. If your project is setup with composer defaults (see below directory structure) the default options will be all you need. Just hit RETURN three times!

project-dir
   -- src
   -- tests
   -- vendor

You get a default phpunit.xml which is good to go. You can of course edit to include any specialisms (e.g. colors="true") you require-:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.1/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         executionOrder="depends,defects"
         forceCoversAnnotation="true"
         beStrictAboutCoversAnnotation="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutTodoAnnotatedTests="true"
         verbose="true">
    <testsuites>
        <testsuite name="default">
            <directory suffix="Test.php">tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">src</directory>
        </whitelist>
    </filter>
</phpunit>
Impound answered 26/4, 2019 at 13:46 Comment(0)
S
4

If you are using PHPUnit 7 you can make your classes from src/ folder to autoload in tests like this:

  1. Ensure that your composer.json file looks similar to this:

    {
        "autoload": {
            "classmap": [
                "src/"
            ]
        },
        "require-dev": {
            "phpunit/phpunit": "^7"
        }
    }
    
  2. To apply changes in composer.json run command:

    composer install
    
  3. Finally you can run tests in tests/ folder:

    ./vendor/bin/phpunit tests/
    
Sherronsherry answered 4/12, 2018 at 8:2 Comment(1)
It is good to mention how to apply changes.Delanty

© 2022 - 2024 — McMap. All rights reserved.