我目前正在使用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-autoload and verified that my
composer.json has an entry which specifies a classmap to 'tests/
。
我使用phpunit.xml加载了bootstrap/app.php
来执行测试,但是仍然出现此错误。
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
}
如果您需要更多信息,请告诉我。
提前谢谢!
答案 0 :(得分:1)
这是Lumen安装中的问题。 当您安装laravel时,tests文件夹在autoload-dev上配置为psr-4:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
...
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
...
"minimum-stability": "dev",
"prefer-stable": true
}
但是在流明上却没有安装,因为我们可以看到下面的声音:
{
"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.7.*",
"vlucas/phpdotenv": "~2.2"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"phpunit/phpunit": "~7.0",
"mockery/mockery": "~1.0"
},
"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
}
因此,要使其正常工作,您需要将自动加载更改为:
"autoload-dev": {
"classmap": [
"tests/"
],
"psr-4": {
"Tests\\": "tests/"
}
}