php 在 PHPUnit 中使用命名空间时“找不到类”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15719293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Class not found' when using namespaces in PHPUnit
提问by hohner
I'm new to PHPUnit and am having some trouble setting it up to access my PHP files. The directory structure I'm using for my app is this:
我是 PHPUnit 的新手,在设置它以访问我的 PHP 文件时遇到了一些麻烦。我用于我的应用程序的目录结构是这样的:
./phpunit.xml
./lib/Application/
-> Dir1/File1.php (namespace = Application\Dir1)
-> Dir1/File2.php
-> Dir2/File1.php (namespace = Application\Dir2)
./tests/Application/Tests
-> Test1.php (namespace = Application\Tests)
-> Test2.php
In my PhpUnit.xml, I have:
在我的 PhpUnit.xml 中,我有:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit verbose="false">
<testsuites>
<testsuite name="Application">
<directory>./tests/Application/Tests</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
<log type="json" target="/tmp/phpunit-logfile.json"/>
</logging>
<filter>
<whitelist>
<directory suffix=".php">./lib</directory>
</whitelist>
</filter>
</phpunit>
And in one of my test files, I open with:
在我的一个测试文件中,我打开:
namespace Application\Tests;
use Application\Dir1\File1;
class MyTest extends File1 {}
But it keeps on saying:
但它一直在说:
Class 'Application\Dir1\File1' not found
找不到类“Application\Dir1\File1”
Where am I going wrong?
我哪里错了?
采纳答案by MichaelRushton
Even if you use use
, you still have to include the file, either by using include
, require
, include_once
, or require_once
, or by using spl_autoload_register
to include the file, like so:
即使您使用use
,您仍然必须通过使用include
、require
、include_once
、 或require_once
或使用spl_autoload_register
来包含文件来包含文件,如下所示:
spl_autoload_register(function ($class)
{
include '\lib\' . $class . 'php';
});
When you then try to use Application\Dir1\File1
the script will automatically run include '\lib\Application\Dir1\File1.php'
当你再尝试使用Application\Dir1\File1
脚本时会自动运行include '\lib\Application\Dir1\File1.php'
回答by JohnnyFaldo
If you installed PHPUnit using Composer then you can use Composers autoloader. The easiest way to do so would be to add:
如果您使用 Composer 安装了 PHPUnit,那么您可以使用 Composers 自动加载器。最简单的方法是添加:
"autoload":{
"psr-0":{
"your-app-directory":""
}
}
to composer.json
到 composer.json
回答by hohner
I found this really useful class autoloaderby Jonathan Wage which allows PHPUnit tests to access namespaces from different directories. In my bootstrap.php, I just specified the location and associated module namespace:
我发现Jonathan Wage 的这个非常有用的类自动加载器,它允许 PHPUnit 测试访问来自不同目录的命名空间。在我的 bootstrap.php 中,我只是指定了位置和关联的模块命名空间:
require_once 'SplClassLoader.php';
$classLoader = new SplClassLoader('Application', dirname(__FILE__) . '/../lib');
$classLoader->register();
回答by silver
I had the same issue. I'm using composer as well and the only thing that solved it for me was the following:
我遇到过同样的问题。我也在使用作曲家,唯一为我解决的问题是:
add to your composer.json file in the autoload section a class map section with your root namespace
"autoload": { "classmap": ["namespaceRoot/"] }
- execute
composer dump-autoload
command in order to recreate your autoload files (with all the class mappings!)
将带有根命名空间的类映射部分添加到自动加载部分的 composer.json 文件中
"autoload": { "classmap": ["namespaceRoot/"] }
- 执行
composer dump-autoload
命令以重新创建您的自动加载文件(包含所有类映射!)