PHPUnit错误:找不到类 [英] PHPUnit Error: Class not found
问题描述
我正在使用symfony 4.0,并在composer.json中使用"symfony/phpunit-bridge":"^ 4.0". 我已经使用命令
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
.
现在,当我运行vendor\bin\simple-phpunit tests/Handler
时,它显示此错误
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
}
}
}
CalculateWalk类的路径:
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.
推荐答案
也发生在4.1中,可能是bin/phpunit脚本中的一个问题:
Happening in 4.1 as well, possibly an issue in the bin/phpunit script:
$classLoader->unregister();
我通过创建一个tests/bootstrap.php文件(并更新phpunit.xml.dist以使用它)来解决该问题:
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);
这篇关于PHPUnit错误:找不到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!