PHPUnit错误:找不到类

时间:2018-01-25 10:08:53

标签: php unit-testing symfony phpunit symfony4

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

安装了phpunit-bridge

composer require --dev phpunit

现在,当我运行vendor\bin\simple-phpunit tests/Handler时显示此错误

C:\xampp\htdocs\symfonydemo>vendor\bin\simple-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) App\Tests\Handler\CalculatorResultHandlerTest::testWalkTime
Error: Class 'App\Service\CalculateWalk' not found

C:\xampp\htdocs\symfonydemo\tests\Handler\CalculatorResultHandlerTest.php:19

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

C:\xampp\htdocs\symfonydemo>

应用\测试\处理程序\ CalculatorResultHandlerTest:

namespace App\Tests\Handler;

use App\Service\CalculateWalk;
use PHPUnit\Framework\TestCase;

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:\xampp\htdocs\symfonydemo\src\Service\CalculateWalk.php

SRC \服务\ CalculateWalk.php

namespace App\Service;


use App\Interfaces\CalculateResultInterface;

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 个答案:

答案 0 :(得分:1)

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

$classLoader->unregister();

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

<?php

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

答案 1 :(得分:0)

我刚遇到这个问题。 不知道为什么,但是我的项目根目录中没有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"

>     

答案 2 :(得分:0)

在您的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="App\Kernel" />
        <env name="APP_ENV" value="test" />
        <env name="APP_DEBUG" value="1" />
        <env name="SHELL_VERBOSITY" value="-1" />
  </php>
</phpunit>

答案 3 :(得分:0)

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