我正在尝试使用一些 phpunit 测试类对 composer autoload 进行测试,但我似乎无法让它工作。当我从命令行运行 phpunit 时,出现以下错误:“PHP fatal error :类 ... 未找到”。
我会给出所有的结构和文件信息。 我可以,所以希望有人能发现我哪里做错了。
结构(缩减为相关文件):
composer.json
composer.lock
phpunit.xml
vendor/
tests/
functional/
BaseTestCase.php
HomepageTest.php
Composer .json
{
"require": {
"php": ">=5.5.0",
"slim/slim": "^3.1",
"slim/php-view": "^2.0",
"monolog/monolog": "^1.17"
},
"require-dev": {
"phpunit/phpunit": ">=4.8 < 6.0"
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
}
phpunit.xml
<?xml version="1.0" encoding="utf-8" ?>
<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Initial tests">
<directory>tests/</directory>
</testsuite>
</testsuites>
</phpunit>
测试/功能/BaseTestCase.php
<?php
namespace Tests\Functional;
use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\Environment;
class BaseTestCase extends \PHPUnit_Framework_TestCase
{
...
测试/功能/HomepageTest.php
<?php
namespace Tests\Functional;
class HomepageTest extends BaseTestCase
{
...
然后我运行更新以刷新自动加载文件
$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files
然后我尝试运行 phpunit 并得到一个找不到类的错误:
$ vendor/bin/phpunit
PHP Fatal error: Class 'Tests\Functional\BaseTestCase' not found in <project-root>/tests/functional/HomepageTest.php on line 6
为了彻底,我尝试以另一种方式刷新自动加载文件,以防万一:
$ composer dump-autoload
Generating autoload files
sfbagency@sfb1:~/clients/ctest/dev$
我还检查了 vendor/composer/autoload_psr4.php 以确保设置了测试引用,并且确实如此。
...
Tests\\' => array($baseDir . '/tests'),
...
我疯狂地用 Google 搜索,但不知道哪里出错了。
最佳答案
命名空间目录区分大小写。您必须将文件夹重命名为 Functional
如 PSR-4 documentation 中所述:
The subdirectory name MUST match the case of the sub-namespace names.
关于php - 找不到 composer phpunit psr-4 自动加载类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39154600/