PHPUnit Error: Class not found

孤者浪人 提交于 2020-01-01 19:25:11

问题


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 vendor\bin\simple-phpunit tests/Handler It shows this error

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>

App\Tests\Handler\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
        }
    }
}

Path of CalculateWalk Class:

C:\xampp\htdocs\symfonydemo\src\Service\CalculateWalk.php

src\Service\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;
    }
}

Can anyone please me to resolve this error. Thanks.


回答1:


Happening in 4.1 as well, possibly an issue in the bin/phpunit script:

$classLoader->unregister();

I worked around it by creating a tests/bootstrap.php file (and updating phpunit.xml.dist to use it):

<?php

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



回答2:


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



回答3:


I ended up with this problem because I forgot to run composer auto-dump -o after installing phpunit.




回答4:


I have just had this problem. Not sure why, but I didn't have a phpunit.xml.dist in the root of my project. None of the examples shows this, but if you add bootstrap= and then include the autoloaded. It should start to find your classes.

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

>



来源:https://stackoverflow.com/questions/48440291/phpunit-error-class-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!