【发布时间】:2018-04-29 22:35:29
【问题描述】:
我无法让 PHPUnit(5.7 版)找到我的自定义框架类。任何帮助将不胜感激,因为我已经尝试过alsorts。当我运行测试时,它抱怨找不到自动加载的类。
{
"scripts": {
"test": [
"@clean",
"@load",
"@phpunit"
],
"clean": "composer clear-cache",
"load": "composer dump-autoload -o",
"phpunit": "\"vendor/bin/phpunit\" --configuration phpunit.xml tests"
},
"autoload": {
"psr-4": {
"AdoptableFramework\\": "src/",
"AdoptableFramework\\Tests\\": "tests/",
"AdoptableFramework\\Extend\\": "src/extend/"
}
},
"require": {
"matthiasmullie/minify": "^1.3"
},
"require-dev": {
"phpunit/phpunit": "5.7"
}
}
这就是我的 phpunit.xml 的样子:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.7/phpunit.xsd"
bootstrap="vendor\autoload.php"
backupGlobals="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
backupStaticAttributes="false"
cacheTokens="false"
colors="always"
convertErrorsToExceptions="true"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false"
forceCoversAnnotation="false"
mapTestClassNameToCoveredClassName="false"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
stopOnRisky="false"
verbose="true">
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/AdoptableFramework</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="tests/code_coverage"
showUncoveredFiles="true"/>
</logging>
</phpunit>
我的文件夹结构如下:
src
--AdoptableFramework
----Core
------Database.php tests
--databaseTest.php
--unitTestSuite.php
这就是我的 databaseTest.php 的样子:
<?php
namespace AdoptableFramework\Tests;
use AdoptableFramework\Core\Database;
use AdoptableFramework\Tests\unitTestSuite;
class DatabaseTest extends UnitTestSuite
{
public function testDatabase()
{
$database = new Database();
$this->assertEquals(null, $database);
}
}
?>
这是我在 AdoptableFramework/Core 中的 Database.php。
<?
/**
* @link https://www.pswebsolutions.co.uk/
* @author Paul Sonny Cook <paul.cook@pswebsolutions.co.uk>
* @copyright 2017 P S Web Solutions Ltd
* @version 1.0.0
* @package psAdoptables
**/
namespace AdoptableFramework\Core;
class Database
{
public function __construct($host = null, $username = null, $password = null, $database = null) {
$this->type = __CLASS__;
if ($host || $username || $password || $database) {
$this->connect($host, $username, $password, $database);
} else {
$this->connect();
}
}
public function connect($host = DB_HOST, $username = DB_USER, $password = DB_PASS, $database = DB_DATABASE)
{
return true;
}
}
?>
autoload_static.php 类映射:
我查看了我的 autoload_static.php 文件并且可以看到:
'AdoptableFramework\\Core\\Database' => __DIR__ . '/../..' . '/src/AdoptableFramework/Core/Database.php',
'AdoptableFramework\\Extend\\ExtendDatabase' => __DIR__ . '/../..' . '/src/AdoptableFramework/Extend/ExtendDatabase.php',
'AdoptableFramework\\Game' => __DIR__ . '/../..' . '/src/AdoptableFramework/Game.php',
'AdoptableFramework\\Tests\\DatabaseTest' => __DIR__ . '/../..' . '/tests/databaseTest.php',
'AdoptableFramework\\Tests\\UnitTestSuite' => __DIR__ . '/../..' . '/tests/unitTestSuite.php',
任何帮助将不胜感激。
【问题讨论】:
-
也请告诉我们
Database.php或至少前几行 -
我已经添加了 Database.php 类 - 谢谢!
标签: php phpunit composer-php psr-4