遗留环境中的 PHPUnit
PHPUnit in a legacy environment
我开始设置 PHPUnit (v4.8) 以便在我的 'legacy' 代码中使用(它不是那么遗留,但它有糟糕的编程习惯)。
我的文件夹结构如下
/home
-phpunit.xml
/folder1
/folder2
/folder3
/vendor
/tests
-Test1.php
/includes
-functions.php
/libs
-User.php
-TableClass.php
....
functions.php
<?php
//require_once $_SERVER['DOCUMENT_ROOT'] . "/home/vendor/autoload.php" ;
require_once $_SERVER['DOCUMENT_ROOT'] . "/home/includes/libs/table_classes/User.php" ;
?>
我已经评论了那行,因为我认为作曲家会自动加载它。 问题 1,我是否正确? (因为 phpunit 在我的测试中被自动识别 class...)
Test1.php
<?php
class Test1 extends PHPUnit_Framework_TestCase
{
public function testSomething()
{
// $something = getColNameByStatusId(1);
$this->assertEquals(1,2);
}
}
?>
phpunit.xml
<phpunit bootstrap="includes/functions.php" colors="true">
<testsuite name="Test1" >
<directory>./tests</directory>
</testsuite>
</phpunit>
然后我在命令行中执行phpunit
我的 functions.php
在我的代码中工作正常,当然没有作曲家集成,但是当它加载了 phpunit 时 'breaks',我收到以下错误:
Warning: require_once(/home/includes/libs/table_classes/User.php): failed to open stream: No such file or directory in C:\wamp\www\home\includes\functions.php on line 18
我想我缺少 phpunit 的 'loading' 东西。我的代码不使用命名空间,PSR-0 和 PSR-4 都不使用。
问题 2- 在这种情况下如何正确加载文件?
我的目标 是加载 functions.php
然后它会加载所有其他 'table' classes 来做我的测试
将$_SERVER['DOCUMENT_ROOT']
替换为__DIR__
并相应调整路径,一切正常。
PHPUnit 没有设置 $_SERVER['DOCUMENT_ROOT']
所以它找不到我的文件。 Apache 就是这样做的。所以PHPUnit的CLI找不到。
希望对其他人有所帮助。
我认为最好在 运行
开始使用 PHPUnit
phpunit --generate-configuration
然后回答一些简单的问题。
要自动加载 'functions.php'
和其他 table 'classes'
,您可以尝试通过 composer.json 自动加载。
"autoload": {
"psr-4": {
"Model\": "libs/"
}
}
这里是link自动加载的,供大家参考。
我开始设置 PHPUnit (v4.8) 以便在我的 'legacy' 代码中使用(它不是那么遗留,但它有糟糕的编程习惯)。
我的文件夹结构如下
/home
-phpunit.xml
/folder1
/folder2
/folder3
/vendor
/tests
-Test1.php
/includes
-functions.php
/libs
-User.php
-TableClass.php
....
functions.php
<?php
//require_once $_SERVER['DOCUMENT_ROOT'] . "/home/vendor/autoload.php" ;
require_once $_SERVER['DOCUMENT_ROOT'] . "/home/includes/libs/table_classes/User.php" ;
?>
我已经评论了那行,因为我认为作曲家会自动加载它。 问题 1,我是否正确? (因为 phpunit 在我的测试中被自动识别 class...)
Test1.php
<?php
class Test1 extends PHPUnit_Framework_TestCase
{
public function testSomething()
{
// $something = getColNameByStatusId(1);
$this->assertEquals(1,2);
}
}
?>
phpunit.xml
<phpunit bootstrap="includes/functions.php" colors="true">
<testsuite name="Test1" >
<directory>./tests</directory>
</testsuite>
</phpunit>
然后我在命令行中执行phpunit
我的 functions.php
在我的代码中工作正常,当然没有作曲家集成,但是当它加载了 phpunit 时 'breaks',我收到以下错误:
Warning: require_once(/home/includes/libs/table_classes/User.php): failed to open stream: No such file or directory in C:\wamp\www\home\includes\functions.php on line 18
我想我缺少 phpunit 的 'loading' 东西。我的代码不使用命名空间,PSR-0 和 PSR-4 都不使用。
问题 2- 在这种情况下如何正确加载文件?
我的目标 是加载 functions.php
然后它会加载所有其他 'table' classes 来做我的测试
将$_SERVER['DOCUMENT_ROOT']
替换为__DIR__
并相应调整路径,一切正常。
PHPUnit 没有设置 $_SERVER['DOCUMENT_ROOT']
所以它找不到我的文件。 Apache 就是这样做的。所以PHPUnit的CLI找不到。
希望对其他人有所帮助。
我认为最好在 运行
开始使用 PHPUnitphpunit --generate-configuration
然后回答一些简单的问题。
要自动加载 'functions.php'
和其他 table 'classes'
,您可以尝试通过 composer.json 自动加载。
"autoload": {
"psr-4": {
"Model\": "libs/"
}
}
这里是link自动加载的,供大家参考。