简单 PHP 单元测试的自动加载文件不起作用
Autoload file for a simple PHP unit test not working
我正在尝试熟悉 PHPUnit,我遵循了所有说明 here("Getting Started with PHPUnit 7")
。
我copy/pasted那些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
但这与我目前所写的内容没有区别。
编辑:
根据以下评论,我再次对构造函数 public 和 运行 进行了测试,我收到了此错误消息:
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
如果您使用 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
,它会运行我所有的测试。
Using classmap
it may be necessary to run composer dump
to regenerate autoload files after adding new classes/files.
我正在尝试熟悉 PHPUnit,我遵循了所有说明 here("Getting Started with PHPUnit 7")
。
我copy/pasted那些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
但这与我目前所写的内容没有区别。
编辑:
根据以下评论,我再次对构造函数 public 和 运行 进行了测试,我收到了此错误消息:
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
如果您使用 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
,它会运行我所有的测试。
Using
classmap
it may be necessary to runcomposer dump
to regenerate autoload files after adding new classes/files.