简体   繁体   English

致命错误:找不到类“ CDbTestCase”

[英]Fatal error: Class 'CDbTestCase' not found

I'm new in php and I have to do the testing for an app. 我是php的新手,我必须为一个应用程序进行测试。 I'm trying to make unit testing but an error messages is displayed. 我正在尝试进行单元测试,但显示错误消息。 I have many weeks ago and I cannot fix it, please help me!! 我有很多星期了,我无法解决它,请帮助我!

The message says: Fatal error: Class 'CDbTestCase' not found. 该消息说:致命错误:找不到类'CDbTestCase'。

I read and follow many tutorials about this issue but it doesn't work. 我阅读并遵循了许多有关此问题的教程,但是它不起作用。

I'm using Yii, Eclipse IDE and Composer. 我正在使用Yii,Eclipse IDE和Composer。

I think the problem is in bootstrap.php but I don't use it because I'm working with composer, this is the composer.json 我认为问题出在bootstrap.php中,但是我不使用它,因为我正在使用composer,这就是composer.json

{
    "require-dev": {
        "yiisoft/yii": "1.1.*",
        "phpunit/phpunit": "4.6.*",
        "phpunit/phpunit-selenium": ">=1.2",
        "codeception/codeception":"*",
        "phpunit/dbunit": ">=1.2"
    },

    "autoload": {
        "psr-0": {"": "vendor/autoload.php"},
        "psr-4": {"": "/../framework/test/CDbTestCase.php"}
    }
}

The bootstrap file is needed to load the yii framework. 需要引导文件来加载yii框架。 CDbTestCase is part of the yii framework so failing to include yii will give you this error if your tests depend on yii's unit test related classes. CDbTestCase是yii框架的一部分,因此如果您的测试依赖yii与单元测试相关的类,则不包含yii将给您此错误。

Use the included bootstrap file and make sure you also include composer's autoload.php file. 使用随附的引导程序文件,并确保还包括作曲家的autoload.php文件。 I normally add this to my yii config file (I believe by default, yii uses the test.php config file for custom testing related settings. You can include autoload.php inside this file) 我通常将此添加到我的yii配置文件中(我相信默认情况下,yii使用test.php配置文件来进行与测试相关的自定义设置。您可以在该文件中包含autoload.php)

Somewhere at the top of your yii config file yii配置文件顶部的某个位置

// Include composer autoload
require_once 'path/to/composer/vendor/autoload.php';

Your Composer autoloading is completely wrong. 您的Composer自动加载完全错误。

  1. Don't include the composer autoloader "vendor/autoload.php" in the autoloading of your own composer.json . 不要在自己的composer.json的自动加载中包括composer自动加载器“ vendor / autoload.php”。 There is only one autoloader, and it is created using your autoload data as well as any libraries. 只有一个自动加载器,它是使用您的自动加载数据以及任何库创建的。
  2. The PSR-4 autoloading is also wrong. PSR-4自动加载也是错误的。 You have to state a directory where classes are put into files according to PSR-4. 您必须声明一个目录,根据PSR-4,将类放入文件中。 You point to one single file. 您指向一个文件。
  3. You don't use class prefixes for PSR-0 and PSR-4. 您不对PSR-0和PSR-4使用类前缀。 This is bad for performance, because you are stating that ANY class could be in the directory. 这对性能不利,因为您要声明ANY类可能在目录中。 So Composer has to search for that class in this directory as well, even for classes that are guaranteed to not be there. 因此,Composer也必须在此目录中搜索该类,即使对于保证不存在的类也是如此。 Always use a prefix, and make it as long as possible to allow unique matches: One prefix should only ever point to exactly one directory structure. 始终使用前缀,并使其尽可能长以允许唯一匹配:一个前缀应仅指向一个目录结构。

You should explain why you don't use a bootstrap file in your tests. 您应该解释为什么在测试中不使用引导文件。 The minimal required bootstrap code for PHPUnit is to include the Composer autoloader. PHPUnit所需的最少引导代码是包括Composer自动加载器。 Additionally, you should add things that are required in your tests and have to be done once, like initialization of framework components -YMMV. 另外,您应该添加测试中必需的并且必须完成的事情,例如框架组件-YMMV的初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM