Home » Php » I can't test with phpunit: Cannot open file "autoload.php"

I can't test with phpunit: Cannot open file "autoload.php"

Posted by: admin July 12, 2020 Leave a comment

Questions:

Config :

  • PHPUNIT: 4.5.0
  • PHP : 5.4.12
  • Server: Wamp
  • Composer: version 1.0-dev 2015-02-17 21:55:44

composer.json :

{
    "require-dev": {
        "phpunit/phpunit": "4.5.*"
    }
}

autoload.php :

<?php
date_default_timezone_set("Europe/Paris");

require __DIR__.'/vendor/Symfony/Component/ClassLoader/UniversalClassLoader.php';

use Symfony\Component\ClassLoader\UniversalClassLoader;

$loader = new UniversalClassLoader();

$loader->registerNamespaces(array(
    'Hangman' => __DIR__.'/src',
    'Symfony' => __DIR__.'/vendor',
));

$loader->register();

phpunit.xml :

<?xml version="1.0" encoding="UTF-8"?>

<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="autoload.php"
        >

    <testsuites>
        <testsuite name="hangman">
            <directory>tests/Hangman/Tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <blacklist>
            <directory>vendor</directory>
        </blacklist>
    </filter>
</phpunit>

Problem :

I executed : phpunit --bootstrap autoload.php tests

My Error : Cannot open file "autoload.php"

Can you help me ?

How to&Answers:

I am working with PHPUnit 7 and basis on that I have mentioned solution in the below which is tested and working 100%

If you are windows user then type following on cmd:
“vendor/bin/phpunit” –bootstrap ./vendor/autoload.php ./tests/EmailTest

Make sure I have updated ./ before vendor and tests.

Answer:

You could give bootstrap="vendor/autoload.php" a try in your phpunit.xml. Then PHPUnit and your Tests would use the Composer Autoloader.

Or you could require the Composer Autoloader (in addition to Symfony’s UCL) in your autoload.php by adding require 'vendor/autoload.php';. This results in two autoloaders being registered.

Then run:

  1. composer update – to fetch dependencies and rebuild the autoloading files
  2. phpunit – to execute a test run

You don’t need to run phpunit with --bootstrap, because the directive is already set in your phpunit.xml.


I think your folder layout is not right.
You start with this:

c:\wamp\www\yourproject
  \src
  \tests
    \- phpunit.xml.dist
  \vendor
  \composer.json
  1. add phpunit to the require-dev section of your composer.json
  2. add "bin-dir" : "bin", so that phpunit.bat lives in c:\wamp\www\yourproject\bin\phpunit.bat and not in c:\wamp\www\yourproject\vendor\bin\phpunit.bat
  3. composer install or update
  4. exec c:\wamp\www\yourproject\bin\phpunit.bat -c c:\wamp\www\yourproject\tests\phpunit.xml.dist