PHP 致命错误:找不到类 - PHPUnit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39454285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:54:17  来源:igfitidea点击:

PHP Fatal error: Class not found - PHPUnit

phpphpunit

提问by splunk

I'm trying to use PHPUnitin a PHP project. Here is my project structure (files are in italic font style)

我正在尝试在 PHP 项目中使用PHPUnit。这是我的项目结构(文件为斜体字体)

  • controllers

    • Pages.php
  • tests

    • pagesTest.php
  • vendor

    • bin
      • phpunit.bat
  • composer.json

  • 控制器

    • 页面.php
  • 测试

    • 页面测试.php
  • 小贩

    • 垃圾桶
      • phpunit.bat
  • 作曲家.json

My files:

我的文件:

composer.json

作曲家.json

{
    "require-dev": {
        "phpunit/phpunit":"5.5.4"
    }
}

Pages.php

页面.php

<?php
namespace controllers

class Pages
{
    public function render()
    {
        return 'Hello World';
    }

}

pagesTest.php

页面测试.php

<?php

class PagesTest extends PHPUnit_Framework_TestCase
{
    public function testRenderReturnsHelloWorld()
    {
        $pages = new \controllers\Pages();
        $expected = 'Hello Word';
        $this->assertEquals($expected, $pages->render());
    }
}

When I open the command line I write:

当我打开命令行时,我写道:

C:\xampp\htdocs\PHPUnitTestProject\vendor\bin>phpunit ../../tests/PagesTest.php

I receive this error message: PHP Fatal error: Class 'controllers\Pages' not found in C:\xampp\htdocs\PHPUnitTestProject\tests\pagesTest.php on line 7

我收到此错误消息: PHP Fatal error: Class 'controllers\Pages' not found in C:\xampp\htdocs\PHPUnitTestProject\tests\pagesTest.php on line 7

It's a path problem. I think it's because it searches for C:\xampp\htdocs\PHPUnitTestProject\vendor\bin\controllers\Pages()which doesn't exists.
It should be C:\xampp\htdocs\PHPUnitTestProject\controllers\Pages()

是路径问题。我认为这是因为它搜索的C:\xampp\htdocs\PHPUnitTestProject\vendor\bin\controllers\Pages()是不存在的。
它应该是C:\xampp\htdocs\PHPUnitTestProject\controllers\Pages()

回答by Katie

You need to point to the tested class, so in pagesTest.php add a require:

你需要指向被测试的类,所以在 pagesTest.php 添加一个 require:

require __DIR__ . "/../controllers/Pages.php";

Orif you are using autoloading, then you can bootstrap the autoloadin your command line

或者,如果您使用自动加载,则可以在命令行中引导自动加载

phpunit --bootstrap src/autoload.php

Oryou can set up a phpunit.xml configuration file like this example (from the PHPUnit page I linked to above):

或者你可以像这个例子一样设置一个 phpunit.xml 配置文件(来自我上面链接的 PHPUnit 页面):

<phpunit bootstrap="src/autoload.php">
  <testsuites>
    <testsuite name="money">
      <directory>tests</directory>
    </testsuite>
  </testsuites>
</phpunit>

Which you then use with the --configuration option.

然后将其与 --configuration 选项一起使用。

回答by JorgeObregon

Call phpunitfrom the root folder:

phpunit从根文件夹调用:

$ cd C:\xampp\htdocs\PHPUnitTestProject\
$ vendor\bin\phpunit tests/PagesTest.php

回答by Allen

Adding bootstrap="vendor/autoload.php"in phpunit.xml.distsolved the issue for me.

添加bootstrap="vendor/autoload.php"phpunit.xml.dist解决这个问题对我来说。

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"> <!-- in here -->
    <php>
        <!-- ... -->
    </php>
    <testsuites>
       <!-- ... -->
    </testsuites>
</phpunit>