Composer cannot load bootloader when using PhpUnit

I am trying to use PhpUnit with Composer. For this purpose I did:

1 Added phpunit to req composer section:

"require": { "php": ">=5.3.0" }, "require-dev": { "phpunit/phpunit": "3.7.*" }, "autoload": { "psr-0": {"PhpProject": "src/"} } 

2 Installed necessary:

php composer.phar install --dev

The operation completed successfully.

Install phpunit / phpunit (3.7.6) Download: 100%

Unfortunately, when I want to run tests, I get

./vendor/bin/phpunit PHP Fatal error: call add () member function for non-object in /home/serek/php/project/tests/bootstrap.php on line 12

The problem arises because return ComposerAutoloaderInit :: getLoader (); in vendor / autoload returns NULL in the test bootstrap.

Any idea how this can be solved without hacking a loader?

code: phpunnit.xml.dist

 > <?xml version="1.0" encoding="UTF-8"?> > > <phpunit bootstrap="tests/bootstrap.php" colors="true"> > <testsuites> > <testsuite name="PhpProject Test Suite"> > <directory>tests/PhpProject/</directory> > </testsuite> > </testsuites> > > <filter> > <whitelist> > <directory suffix=".php">src/PhpProject/</directory> > </whitelist> > </filter> </phpunit> 

tests / bootstrap.php (I only need an autoloader here)

 > $loader = require_once __DIR__ . "/../vendor/autoload.php"; > $loader->add('PhpProject\\', __DIR__); //<- this is problematic line 12 (comments has 9 lines) 

/../seller/autoload.php

 // autoload.php generated by Composer require_once __DIR__ . '/composer' . '/autoload_real.php'; return ComposerAutoloaderInit::getLoader(); 
+3
php phpunit composer-php
Oct 08 '12 at 23:01
source share
1 answer

The problem is that PHPUnit already requires an autoload file, so the require_once call is not executed, so the return value is not set (php does not save the return value of the required calls, so require_once breaks in this use case).

You can safely change it to require , because with recent versions of the composer, the autoloader is not created twice and requires many times, it simply returns you the same instance every time.

+11
Oct 09 '12 at 10:28
source share



All Articles