php PHPUnit - 在测试中自动加载类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25219764/
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
PHPUnit - autoload classes within tests
提问by Martyn
I have the following structure within my project:
我的项目中有以下结构:
/
/app
/app/models/ --UserTable.php
/lib
/lib/framework
/lib/framework/Models
/lib/framework/Db
/tests -- phpunit.xml, bootstrap.php
/tests/app
/tests/app/models --UserTableTest.php
With the app and lib directories I have various classes that work together to run my app. To setup my tests I have create a /tests/phpunit.xml file and a /tests/bootstrap.php
使用 app 和 lib 目录,我有各种可以一起运行我的应用程序的类。为了设置我的测试,我创建了一个 /tests/phpunit.xml 文件和一个 /tests/bootstrap.php
phpunit.xml
phpunit.xml
<phpunit bootstrap="bootstrap.php">
</phpunit>
bootstrap.php
引导程序
<?php
function class_auto_loader($className)
{
$parts = explode('\', $className);
$path = '/var/www/phpdev/' . implode('/', $parts) . '.php';
require_once $path;
}
spl_autoload_register('class_auto_loader');
So I have the following test:
所以我有以下测试:
<?php
class UserTableTest extends PHPUnit_Framework_TestCase
{
protected $_userTable;
public function setup()
{
$this->_userTable = new app\models\UserTable;
}
public function testFindRowByPrimaryKey()
{
$user = $this->_userTable->find(1);
$this->assertEquals($user->id, 1);
}
}
But it can't find the class when I run the test - PHP Fatal error: Class 'app\models\UserTable' not found in /var/www/phpdev/tests/app/models/UserTableTest.php on line 13
但是当我运行测试时它找不到类 - PHP Fatal error: Class 'app\models\UserTable' not found in /var/www/phpdev/tests/app/models/UserTableTest.php on line 13
What am I doing wrong? I'm trying to understand PHPUnit configuration better so I opted to write the configuration and bootstrap file myself.
我究竟做错了什么?我试图更好地理解 PHPUnit 配置,所以我选择自己编写配置和引导文件。
回答by Raaghu
If you are using composer autoload
如果您使用作曲家自动加载
change
改变
<phpunit colors="true" strict="true" bootstrap="vendor/autoload.php">
<phpunit colors="true" strict="true" bootstrap="vendor/autoload.php">
to
到
<phpunit colors="true" strict="true" bootstrap="tests/autoload.php">
<phpunit colors="true" strict="true" bootstrap="tests/autoload.php">
and in tests
directory create new autoload.php
with following content
并在tests
目录中创建autoload.php
具有以下内容的新内容
include_once __DIR__.'/../vendor/autoload.php';
$classLoader = new \Composer\Autoload\ClassLoader();
$classLoader->addPsr4("Your\Test\Namespace\Here\", __DIR__, true);
$classLoader->register();
回答by ImLeo
You probably should use composer to organize your code, for example, the composer.json in your project root directory should contains something like:
您可能应该使用 composer 来组织您的代码,例如,您的项目根目录中的 composer.json 应该包含如下内容:
...
"autoload": {
"psr-0": {
"PRJ_NAME\APP\": "app/",
"PRJ_NAME\LIB\": "lib/"
}
},
...
Then after run composer update, the two namespaces defined above are put into the vendor/composer/autoload_namespaces.php. Next is simple, just run phpunit with the autoload option like this:
然后运行 composer update 后,将上面定义的两个命名空间放入 vendor/composer/autoload_namespaces.php。接下来很简单,只需像这样运行带有自动加载选项的 phpunit:
phpunit --bootstrap vendor/autoload.php tests/app/models/UserTableTest
Make sure change the usage of your namespace in both your source code and test code.
确保在源代码和测试代码中更改命名空间的使用。
回答by Alex
In my loader(very close to yours) I check if the first exploded part of the class name is my vendor, if it isn't the loaderjust return doing nothing (else had issues with phpunit's loader, since I'm a newbie of phpunitand don't know if this is the expected behavior nor if phpunitsuggest or provide a loaderready to use).
在我的加载器(非常接近你的)中,我检查类名的第一个爆炸部分是否是我的供应商,如果不是加载器就返回什么都不做(否则phpunit的加载器有问题,因为我是一个phpunit 的新手,不知道这是否是预期的行为,也不知道phpunit 是否建议或提供准备使用的加载程序)。
I keep phpunit.xml
in the same directory of tests/
(not within) and once configured <directory>tests</directory>
I just run phpunit
on command line without configuration or directory options.
我保留phpunit.xml
在tests/
(不在)的同一目录中,一旦配置好,<directory>tests</directory>
我就phpunit
在没有配置或目录选项的命令行上运行。
回答by Amorphous
If you load you classess with the same bootstrap in app you should be able to load them in tests. If you are running test by cd into your test directory just add to your phpunit.xml:
如果您在应用程序中使用相同的引导程序加载类,您应该能够在测试中加载它们。如果您通过 cd 运行测试到您的测试目录,只需添加到您的 phpunit.xml:
<testsuite name="My Application Tests">
<directory>./</directory>
</testsuite>
Inside <phpunit></phpunit>
里面 <phpunit></phpunit>