PHPUnit错误:找不到类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHPUnit错误:找不到类相关的知识,希望对你有一定的参考价值。

我正在使用symfony 4.0并在composer.json中使用“symfony / phpunit-bridge”:“^ 4.0”。我已经使用命令安装了phpunit-bridge

composer require --dev phpunit

现在,当我运行vendorinsimple-phpunit tests/Handler它显示此错误

C:xampphtdocssymfonydemo>vendorinsimple-phpunit tests/Handler
PHPUnit 6.3.1 by Sebastian Bergmann and contributors.

Testing tests/Handler
E                                                                   1 / 1 (100%)

Time: 173 ms, Memory: 4.00MB

There was 1 error:

1) AppTestsHandlerCalculatorResultHandlerTest::testWalkTime
Error: Class 'AppServiceCalculateWalk' not found

C:xampphtdocssymfonydemo	estsHandlerCalculatorResultHandlerTest.php:19

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

C:xampphtdocssymfonydemo>

应用程序测试处理器 CalculatorResultHandlerTest:

namespace AppTestsHandler;

use AppServiceCalculateWalk;
use PHPUnitFrameworkTestCase;

class CalculatorResultHandlerTest extends TestCase
{

    public function testWalkTime(){
        $walkObj = new CalculateWalk();
        $time = $walkObj->calculateTime('20000');
        $this->assertEquals('40',$time,'Walk minutes are wrong.');
    }

}

vendor / bin / .phpunit / phpunit-6.3 / phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="phpunit.xsd"
         bootstrap="tests/bootstrap.php"
         verbose="true">
    <testsuites>
        <testsuite name="small">
            <directory suffix="Test.php">tests/Framework</directory>
            <directory suffix="Test.php">tests/Runner</directory>
            <directory suffix="Test.php">tests/Util</directory>
            <directory>tests</directory>
        </testsuite>

        <testsuite name="large">
            <directory suffix=".phpt">tests/TextUI</directory>
            <directory suffix=".phpt">tests/Regression</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">src</directory>
            <exclude>
                <file>src/Framework/Assert/Functions.php</file>
                <file>src/Util/PHP/eval-stdin.php</file>
            </exclude>
        </whitelist>
    </filter>

    <php>
        <const name="PHPUNIT_TESTSUITE" value="true"/>
    </php>
</phpunit>

composer.json

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.1.3",
        "ext-iconv": "*",
        "sensio/framework-extra-bundle": "^5.1",
        "symfony/console": "^4.0",
        "symfony/flex": "^1.0",
        "symfony/form": "^4.0",
        "symfony/framework-bundle": "^4.0",
        "symfony/lts": "^4@dev",
        "symfony/security-csrf": "^4.0",
        "symfony/twig-bundle": "^4.0",
        "symfony/validator": "^4.0",
        "symfony/yaml": "^4.0"
    },
    "require-dev": {
        "symfony/debug-pack": "^1.0",
        "symfony/dotenv": "^4.0",
        "symfony/phpunit-bridge": "^4.0",
        "symfony/profiler-pack": "^1.0",
        "symfony/web-server-bundle": "^4.0"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\Tests\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php71": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "id": "01C41AEMAEYWEXNQZJ9Q0FBVGW",
            "allow-contrib": false
        }
    }
}

CalculateWalk类的路径:

C:xampphtdocssymfonydemosrcServiceCalculateWalk.php

SRC 服务 CalculateWalk.php

namespace AppService;


use AppInterfacesCalculateResultInterface;

class CalculateWalk implements CalculateResultInterface
{
    public const WALK = 500;
    public function calculateTime($meters)
    {
        $minutes = $meters / self::WALK;
        $seconds = $minutes * 60;
        $time['hours'] = floor($seconds / 3600);
        $time['mins'] = floor($seconds / 60 % 60);
        $time['secs'] = floor($seconds % 60);
        return $time;
    }
}

任何人都可以请我解决此错误。谢谢。

答案

也发生在4.1中,可能是bin / phpunit脚本中的一个问题:

$classLoader->unregister();

我通过创建tests / bootstrap.php文件(并更新phpunit.xml.dist来使用它)来解决这个问题:

<?php

$classLoader = require dirname(__DIR__).'/vendor/autoload.php';
$classLoader->register(true);
另一答案

我刚遇到这个问题。不知道为什么,但我的项目根目录中没有phpunit.xml.dist。没有一个例子显示这个,但是如果你添加bootstrap =然后包括自动加载。它应该开始找到你的课程。

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
     bootstrap="vendor/autoload.php"

>

另一答案

在phpunit.xml中,您可能需要指定内核和环境:

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="vendor/autoload.php"
>
    <php>
        <ini name="error_reporting" value="-1" />
        <env name="KERNEL_CLASS" value="AppKernel" />
        <env name="APP_ENV" value="test" />
        <env name="APP_DEBUG" value="1" />
        <env name="SHELL_VERBOSITY" value="-1" />
  </php>
</phpunit>
另一答案

我最终遇到了这个问题,因为我忘了在安装phpunit后运行composer auto-dump -o

以上是关于PHPUnit错误:找不到类的主要内容,如果未能解决你的问题,请参考以下文章

为啥,致命错误:在...中找不到类“PHPUnit_Framework_TestCase”?

PHPUnit错误:找不到类

PHPUnit 自动加载器错误:找不到类“Yii”

PHPUnit CodeIgniter 生成夹具 PHP 致命错误:找不到类“PHPUnit_Framework_TestCase”

使用 yii 在 phpunit 中找不到类“CDbTestCase”错误

如何解决此错误:“找不到类 PHPUnit_Extensions_SeleniumTestCase”