PHPUnit 错误:找不到类 [英] PHPUnit Error: Class not found

查看:38
本文介绍了PHPUnit 错误:找不到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm using symfony 4.0 and using "symfony/phpunit-bridge": "^4.0" in composer.json. I have installed phpunit-bridge using command

composer require --dev phpunit.

Now when I run vendorinsimple-phpunit tests/Handler It shows this error

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>

AppTestsHandlerCalculatorResultHandlerTest:

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
        }
    }
}

Path of CalculateWalk Class:

C:xampphtdocssymfonydemosrcServiceCalculateWalk.php

srcServiceCalculateWalk.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;
    }
}

Can anyone please me to resolve this error. Thanks.

解决方案

In your phpunit.xml, you maybe need to specify the kernel and environment :

<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 错误:找不到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆