Jas*_*lsa 63 php phpunit namespaces autoload composer-php
我刚刚通过Composer安装了Sebastian Bergmann的PHPUnit版本3.7.19,并编写了一个我想进行单元测试的课程.
我想让我的所有课程自动加载到每个单元测试中,而不必使用include
或require
在我的测试的顶部,但这证明是困难的!
这是我的目录结构的样子(尾部/斜杠表示目录,而不是文件):
我的composer.json文件包括以下内容:
"require": {
"phpunit/phpunit": "3.7.*",
"phpunit/phpunit-selenium": ">=1.2"
}
Run Code Online (Sandbox Code Playgroud)
我的returns.php类文件包括以下内容:
<?php
class Returning {
public $var;
function __construct(){
$this->var = 1;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我的returnsTest.php测试文件包括以下内容:
<?php
class ReturningTest extends PHPUnit_Framework_TestCase
{
protected $obj = null;
protected function setUp()
{
$this->obj = new Returning;
}
public function testExample()
{
$this->assertEquals(1, $this->obj->var);
}
protected function tearDown()
{
}
}
?>
Run Code Online (Sandbox Code Playgroud)
但是,当我从命令行运行时./vendor/bin/phpunit tests
,我收到以下错误:
PHP致命错误:第8行的/files/code/php/db/tests/returningTest.php中找不到类'返回'
我注意到composer
生成了一个autoload.php
文件,vendor/autoload.php
但不确定这是否与我的问题相关.
另外,在Stack Overflow的其他一些答案中,人们已经提到过在composer中使用PSR-0和namespace
PHP中的命令,但我没有成功使用任何一个.
请帮忙!我只是想在PHPUnit中自动加载我的类,所以我可以使用它们创建对象而不用担心include
或require
.
更新:2013年8月14日
我现在已经创建了一个名为PHPUnit Skeleton的开源项目,以帮助您轻松启动并运行PHPUnit测试,以便为您的项目进行测试.
Wou*_*r J 75
嗯,起初.你需要告诉自动加载器在哪里找到类的php文件.这是通过遵循PSR-0标准完成的.
最好的方法是使用命名空间.Acme/Tests/ReturningTest.php
您请求Acme\Tests\ReturningTest
课程时自动装带器会搜索文件.有一些很棒的命名空间教程,只是搜索和阅读.请注意,命名空间不是 PHP中用于自动加载的东西,它可以用于自动加载.
Composer附带一个标准的PSR-0自动加载器(一个vendor/autoload.php
).在您的情况下,您想告诉自动装带器搜索目录中的lib
文件.然后当你使用ReturningTest
它时会寻找/lib/ReturningTest.php
.
将此添加到您的composer.json
:
{
...
"autoload": {
"psr-0": { "": "lib/" }
}
}
Run Code Online (Sandbox Code Playgroud)
文档中的更多信息.
现在,自动加载器可以找到你需要的类,让phpunit知道在运行测试之前有一个文件要执行:一个bootstrap文件.您可以使用该--bootstrap
选项指定引导程序文件的位置:
$ ./vendor/bin/phpunit tests --bootstrap vendor/autoload.php
Run Code Online (Sandbox Code Playgroud)
但是,使用PHPunit配置文件更好:
<!-- /phpunit.xml.dist -->
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="The project's test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
现在,您可以运行该命令,它将自动检测配置文件:
$ ./vendor/bin/phpunit
Run Code Online (Sandbox Code Playgroud)
如果将配置文件放入另一个目录,则需要使用该-c
选项将该目录的路径放在命令中.
Chu*_* Ma 41
[ Update2 ]另一种更简单的替代方法是autoload-dev
在composer.json
(reference)中使用该指令.好处是你不需要维护两个bootstrap.php(一个用于prod,一个用于dev),只是为了自动加载不同的类.
{
"autoload": {
"psr-4": { "MyLibrary\\": "src/" }
},
"autoload-dev": {
"psr-4": { "MyLibrary\\Tests\\": "tests/" }
}
}
Run Code Online (Sandbox Code Playgroud)
[ 更新 ] Wouter J的答案更完整.但我的可以帮助想要在tests/
文件夹中设置PSR-0自动加载的人.
Phpunit使用此模式扫描所有文件*Test.php
.所以我们不需要自己加载它们.但我们仍然想在tests/
夹具/存根或某些父类下自动加载其他支持类.
一个简单的方法是查看Composer项目本身如何设置phpunit测试.它实际上非常简单.注意"bootstrap"行.
参考:https://github.com/composer/composer/blob/master/phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Composer Test Suite">
<directory>./tests/Composer/</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>slow</group>
</exclude>
</groups>
<filter>
<whitelist>
<directory>./src/Composer/</directory>
<exclude>
<file>./src/Composer/Autoload/ClassLoader.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
参考:https://github.com/composer/composer/blob/master/tests/bootstrap.php
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
error_reporting(E_ALL);
$loader = require __DIR__.'/../src/bootstrap.php';
$loader->add('Composer\Test', __DIR__);
Run Code Online (Sandbox Code Playgroud)
上面的最后一行是在命名空间Composer\Test下自动加载phpunit测试类.
这些答案都不是我想要的.是PHPUnit加载测试文件,但不加载存根/固定装置.Chaun Ma的回答没有削减它,因为运行vendor/bin/phpunit
已经包含自动加载,所以没有办法让自动加载器的实例在那时将更多路径推送到它的堆栈.
我最终在文档中找到了这个:
如果需要在多个目录中搜索相同的前缀,可以将它们指定为数组:
Run Code Online (Sandbox Code Playgroud){ "autoload": { "psr-0": { "Monolog\\": ["src/", "lib/"] } } }
有一种非常简单的方法可以使用自动加载和引导程序来设置 phpunit。使用 phpunit 的--generate-configuration
选项在几秒钟内创建您的phpunit.xml
配置-:
vendor/bin/phpunit --generate-configuration
Run Code Online (Sandbox Code Playgroud)
(或者只是phpunit --generate-configuration
在您的 PATH 中设置了 phpunit)。此选项从 phpunit5 及更高版本开始可用。
此选项将提示您输入引导程序文件 ( vendor/autoload.php )、测试和源目录。如果您的项目使用作曲家默认设置(请参阅下面的目录结构),则默认选项将是您所需要的。只需按三下 RETURN 即可!
project-dir
-- src
-- tests
-- vendor
Run Code Online (Sandbox Code Playgroud)
你会得到一个默认的 phpunit.xml,这是很好的。您当然可以编辑以包括您需要的任何专业(例如colors="true"
):
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.1/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
Run Code Online (Sandbox Code Playgroud)