我试图熟悉PHPUnit,我遵循了所有说明here("Getting Started with PHPUnit 7")
。
我复制/粘贴了src/Email.php
和tests/EmailTest.php
这些代码源。我的问题是当我尝试运行./phpunit --bootstrap src/autoload.php tests/EmailTest
时。文档说我必须创建该src/autoload.php
,所以我遵循了提到的第一个示例here("Autoloading Classes")
:
<?php
// Maybe I do not need this require_once() stuff?
require_once('src/Email.php');
require_once('tests/EmailTest.php');
spl_autoload_register(function ($class_name) {
include $class_name . '.php';
});
$obj = new Email('bart.simpson@outlook.com');
$obj2 = new EmailTest();
?>
但是当我使用上一条命令进行测试时,我收到此错误消息:
PHP Error: Call to private Email::__construct() from context 'PHPUnit\Util\FileLoader' in /var/www/html/php_project/src/autoload.php on line 9
PHP Stack trace:
PHP 1. {main}() /var/www/html/php_project/vendor/phpunit/phpunit/phpunit:0
PHP 2. PHPUnit\TextUI\Command::main() /var/www/html/php_project/vendor/phpunit/phpunit/phpunit:53
PHP 3. PHPUnit\TextUI\Command->run() /var/www/html/php_project/vendor/phpunit/phpunit/src/TextUI/Command.php:159
PHP 4. PHPUnit\TextUI\Command->handleArguments() /var/www/html/php_project/vendor/phpunit/phpunit/src/TextUI/Command.php:170
PHP 5. PHPUnit\TextUI\Command->handleBootstrap() /var/www/html/php_project/vendor/phpunit/phpunit/src/TextUI/Command.php:896
PHP 6. PHPUnit\Util\FileLoader::checkAndLoad() /var/www/html/php_project/vendor/phpunit/phpunit/src/TextUI/Command.php:1035
PHP 7. PHPUnit\Util\FileLoader::load() /var/www/html/php_project/vendor/phpunit/phpunit/src/Util/FileLoader.php:45
PHP 8. include_once() /var/www/html/php_project/vendor/phpunit/phpunit/src/Util/FileLoader.php:57
PHP Fatal error: Uncaught Error: Call to private Email::__construct() from context 'PHPUnit\Util\FileLoader' in /var/www/html/php_project/src/autoload.php:9
Stack trace:
#0 /var/www/html/php_project/vendor/phpunit/phpunit/src/Util/FileLoader.php(57): include_once()
#1 /var/www/html/php_project/vendor/phpunit/phpunit/src/Util/FileLoader.php(45): PHPUnit\Util\FileLoader::load('/var/www/html/p...')
#2 /var/www/html/php_project/vendor/phpunit/phpunit/src/TextUI/Command.php(1035): PHPUnit\Util\FileLoader::checkAndLoad('src/autoload.ph...')
#3 /var/www/html/php_project/vendor/phpunit/phpunit/src/TextUI/Command.php(896): PHPUnit\TextUI\Command->handleBootstrap('src/autoload.ph...')
#4 /var/www/html/php_project/vendor/phpunit/phpunit/src/TextUI/Command.php(170): PHPUnit\TextUI\Command->handleArguments(Array)
#5 /var/www/html/php_project/vendor/phpunit/phpunit/src/TextUI/Command.php(159): PHPUnit\TextUI\Command->run(Array, true)
#6 /var/www/html/php_project/vendor/phpunit/phpunit/phpunit(53): PHPUnit\TextUI\Command::main()
#7 {main}
in /var/www/html/php_project/src/autoload.php on line 9
我很久以前就使用过这种东西,如果可以帮助我刷新...
P.S。根据我的设置,我宁愿运行./vendor/bin/phpunit --bootstrap src/autoload.php tests/EmailTest
,但这与我到目前为止所写的内容没有区别。
编辑:
在下面的注释之后,我公开了构造函数并再次运行测试,得到了以下错误消息:
PHP Fatal error: Uncaught PHPUnit\Runner\Exception: Class 'tests/EmailTest' could not be found in 'tests/EmailTest.php'. in /var/www/html/php_project/vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php:99
Stack trace:
#0 /var/www/html/php_project/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(130): PHPUnit\Runner\StandardTestSuiteLoader->load('tests/EmailTest', 'tests/EmailTest...')
#1 /var/www/html/php_project/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(78): PHPUnit\Runner\BaseTestRunner->loadSuiteClass('tests/EmailTest', '')
#2 /var/www/html/php_project/vendor/phpunit/phpunit/src/TextUI/Command.php(180): PHPUnit\Runner\BaseTestRunner->getTest('tests/EmailTest', '', Array)
#3 /var/www/html/php_project/vendor/phpunit/phpunit/src/TextUI/Command.php(159): PHPUnit\TextUI\Command->run(Array, true)
#4 /var/www/html/php_project/vendor/phpunit/phpunit/phpunit(53): PHPUnit\TextUI\Command::main()
#5 {main}
thrown in /var/www/html/php_project/vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php on line 99
答案 0 :(得分:3)
在我看来,如果您使用Composer安装PHPUnit,则最好也将其用于自动加载,而不必自己打扰。
允许我分享我对PHP项目的(建议的)设置:
./src/
中的类./tests/
中的测试用例类 composer.json (在较大的项目中检查PSR-4而不是classmap
)
"autoload": {
"classmap": [
"src/"
]
},
"autoload-dev": {
"classmap": [
"tests/"
]
},
phpunit.xml (用于保存设置,因此您不必一直在CLI中传递它):
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
然后,我可以不带选项地直接呼叫./vendor/bin/phpunit
,它会运行我的所有测试。
在添加新的类/文件之后,可能需要使用
classmap
运行composer dump
来重新生成自动加载文件。