【问题标题】:PHPUnit 5.7 (autoloaded) Class Not Found未找到 PHPUnit 5.7(自动加载)类
【发布时间】: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


【解决方案1】:

非常抱歉浪费大家的时间!

php 文件被加载为文本,因为它需要

一旦我改变了一切,点击到位并开始工作!

【讨论】:

    【解决方案2】:

    在我看来,您的src/AdoptableFramework 目录是不必要的。您可以删除它并将所有文件移到更高一级或尝试以下使用语句:

    use AdoptableFramework\AdoptableFramework\Core\Database;
    

    "AdoptableFramework\\": "src/", 这一行告诉自动加载器 src 文件夹中的任何类都已经在 AdoptableFramework 命名空间中。例如,如果您以这种方式创建类Foosrc/Foo.php,您将以这种方式导入它:

    use AdoptableFramework\Foo;
    

    【讨论】:

    • 感谢您的帮助/建议,但仍然抛出同样的错误。我添加了我的 autoload_static.php 来查看类映射。如果这样看有帮助,我在 BitBucket 上有这个?
    • Bitbucket 肯定会有所帮助,目录结构和自动加载存在一些问题,代码本身看不到任何问题。
    猜你喜欢
    • 2015-09-05
    • 2016-02-17
    • 2017-01-02
    • 2020-11-03
    • 2017-02-28
    • 2020-02-24
    • 1970-01-01
    • 2015-09-30
    • 2018-06-01
    相关资源
    最近更新 更多