【发布时间】:2019-03-30 22:40:24
【问题描述】:
我目前正在使用 lumen 框架 (v5.6) 并为我的代码编写单元测试。
我有一个基类TestCase:
namespace Tests;
$_SERVER["http_proxy"] = "";
abstract class TestCase extends \Laravel\Lumen\Testing\TestCase
{
/**
* Creates the application.
*
* @return \Laravel\Lumen\Application
*/
public function createApplication()
{
return require __DIR__.'/../bootstrap/app.php';
}
}
我使用这个基类来编写我的测试,但是我有两个测试有很多重叠(它们都测试接口的实现),所以我把通用逻辑放在一个抽象类中:
namespace Tests\App\IO;
use App\io\PageDataParser;
use App\Models\AdvancedArray;
use App\Services\PageService;
use Mockery;
use Tests\TestCase;
abstract class ParsePageDataTestCase extends TestCase
{
// Test logic here, but not relevant for the question
}
最后我在实际测试中使用了这个抽象类:
namespace Tests\App\IO;
use App\io\JsonPageDataParser;
use App\Models\AdvancedArray;
class JsonParsePageDataTestCaseTest extends ParsePageDataTestCase
{
// Test are here, but not relevant for the question
}
但是,当我执行 JsonParsePageDataTestCaseTest 时,我收到以下错误:
PHP 致命错误:在第 15 行的 \tests\app\io\JsonParsePageDataTest.php 中找不到类“Tests\ParsePageDataTestCase”
我已验证文件夹的结构已更正,也尝试使用'composer dump-autoloadand verified that mycomposer.jsonhas an entry which specifies a classmap to 'tests/。
我使用加载bootstrap/app.php 的phpunit.xml 执行我的测试,但我仍然收到此错误。
phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/app.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Application test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<!-- ENV variables go here -->
</php>
</phpunit>
最后是我的composer.json:
{
"name": "laravel/lumen",
"description": "The Laravel Lumen Framework.",
"keywords": ["framework", "laravel", "lumen"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=7.1.3",
"laravel/lumen-framework": "5.6.*",
"vlucas/phpdotenv": "~2.2",
"willdurand/hateoas": "~2.1",
"guzzlehttp/guzzle": "~6.0",
"ext-json": "*",
"vinelab/neoeloquent": "^1.4.6",
"jenssegers/mongodb": "3.4.*",
"predis/predis": "~1.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"phpunit/phpunit": "~7.0",
"mockery/mockery": "~1.0",
"barryvdh/laravel-ide-helper": "~2.5"
},
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"classmap": [
"tests/"
]
},
"scripts": {
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
如果您需要更多信息,请告诉我。
提前谢谢你!
【问题讨论】:
-
在 JsonParsePageDataTestCaseTest.php 上的命名空间声明后缺少 ParsePageDataTestCase 使用语句。
-
@GabrielPereira,感谢您的回复。但它们位于同一个命名空间中,因此不需要使用语句
-
你能分享你的 composer.json 自动加载吗?
-
@GabrielPereira 是的,我更新了问题
-
您是否尝试过加载您的测试类,将它们放在 psr-4 上自动加载为 "Test\\": "tests/" ?
标签: php laravel unit-testing phpunit lumen