使用PhpUnit时,Composer无法加载加载程序 [英] Composer cannot load loader when PhpUnit is used
问题描述
我正在尝试将PhpUnit与Composer结合使用.为此,我这样做了:
I am trying to use PhpUnit with Composer. In this purpose I did:
1将phpunit添加到req作曲者部分:
1 Added phpunit to req composer section:
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"psr-0": {"PhpProject": "src/"}
}
2安装了所需的内容:
2 Installed what needed:
php composer.phar安装--dev
php composer.phar install --dev
操作成功完成.
安装phpunit/phpunit(3.7.6) 下载:100%
Installing phpunit/phpunit (3.7.6) Downloading: 100%
不幸的是,当我想运行测试时,我得到了
Unfortunately when I want to run the tests, I get
./vendor/bin/phpunit PHP致命错误:调用成员函数 在/home/serek/php/project/tests/bootstrap.php中的非对象上执行add() 在第12行
./vendor/bin/phpunit PHP Fatal error: Call to a member function add() on a non-object in /home/serek/php/project/tests/bootstrap.php on line 12
出现问题是因为 返回ComposerAutoloaderInit :: getLoader();在vendor/autoload中,将NULL返回测试引导程序.
Problem occurs because return ComposerAutoloaderInit::getLoader(); in vendor/autoload returns NULL into test bootstrap.
任何想法都可以在不黑客加载程序的情况下解决吗?
Any idea how can it be solved without hacking Loader?
代码: phpunnit.xml.dist
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(这里我只需要自动加载器)
tests/bootstrap.php (here I need only autoloader)
> $loader = require_once __DIR__ . "/../vendor/autoload.php";
> $loader->add('PhpProject\\', __DIR__); //<- this is problematic line 12 (comments has 9 lines)
/../vendor/autoload.php
/../vendor/autoload.php
// autoload.php generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit::getLoader();
推荐答案
问题是PHPUnit已经需要自动加载文件,因此您的require_once调用未执行,因此未设置返回值(php不会保留需要调用的返回值,因此require_once在该用例上中断了.)
The issue is that PHPUnit already requires the autoload file, so your require_once call is not executed and therefore the return value is not set (php doesn't keep the return value of require calls so require_once breaks on that use case).
您可以放心地将其更改为require
,因为在最新的作曲家版本中,不会自动创建两次自动加载器,并且多次要求自动加载器每次都返回相同的实例.
You can safely change it to a require
because with recent composer versions the autoloader is not created twice anymore and requiring it many times just returns you the same instance every time.
这篇关于使用PhpUnit时,Composer无法加载加载程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!