为PHPUnit创建基础测试类并将其扩展为通用功能会导致未找到类错误 [英] Creating a base test-class for PHPUnit and extending it for common functionality results in class not found error

查看:81
本文介绍了为PHPUnit创建基础测试类并将其扩展为通用功能会导致未找到类错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用引导文件运行PHPUnit来自动加载类(由作曲家生成).

I'm running PHPUnit using a bootstrap file for autoloading classes (generated by composer).

我所有的测试都很好地加载了类,但是对于我的两个测试,我创建了一个扩展了\PHPUnit\Framework\TestCase的基础"测试类(类似于PHPUnit7之前的PHPUnit_Framework_TestCase),然后创建了两个扩展了测试类的测试类基类,其结构类似于以下示例代码:

All my tests load up classes just fine, but for two of my tests, I made a "base" test class which extends \PHPUnit\Framework\TestCase (similar to PHPUnit_Framework_TestCase before PHPUnit7), and then two test classes that extend the base class, similar structure to the following example code:

    abstract class BaseTest extends \PHPUnit\Framework\TestCase
    {
        abstract function setUp();

        protected function getCommonTestVariables()
        {
            // ...
        }

        protected function runCommonTests()
        {
            // ...
        }
    }

    class BlahBlahTest extends BaseTest
    {
        public function setUp()
        {
             $variables=$this->getCommonTestVariables();
             //etc...
        }

        public function testThings()
        {
            $this->runCommonTests();
        }
    }

每当我运行此命令时,PHPUnit都会出现错误:

Whenever I run this, PHPUnit gives an error:

致命错误:在第13行的BlahBlahTest.php中找不到类'BaseTest'

我已经检查了文件名,位置,名称空间,并且一切似乎都井井有条.感谢您的帮助,以便深入了解

I've checked filenames, locations, namespaces and everything seems to be in order. Any help would be appreciated to get to the bottom of this

推荐答案

我遇到了同样的问题,如果您不太熟悉PHPUnit和Composer的内部工作原理,那确实看起来很困惑.

I ran into the same problem and if you are not too familiar with the inner workings of both PHPUnit and Composer this can indeed seem perplexing.

PHPunit不使用Composer自动加载器来查找任何测试类.它只会扫描您提供的任何目录,并一次处理一个文件.

PHPunit does not use use the Composer autoloader to find any of your test classes. It just scans any directory you give it and operates on one file at a time.

因此,除了当前正在操作的文件中的类以外,它不知道其他任何类.这就是引导文件起作用的地方.

Hence it does not know about any other class than the one in the file it is currently operating on. That is where the bootstrap file comes into play.

如果要使用Composer Autoloader加载其他测试类,则需要告诉它可以在哪里找到这些测试类(以及(可选)在哪个名称空间中).

If you want to use the Composer Autoloader to load other test classes, you need to tell it where it can find these test classes (and, optionally, in which namespace).

有两种方法可以做到这一点:

There are two ways to do this:

  1. autoload-dev 部分添加到composer.json
  2. 将测试目录添加到Composer Autoloader

使用autoload-dev

autoload-dev部分允许您出于开发目的定义自动加载规则.

Use autoload-dev

The autoload-dev sections allows you to define autoload rules for development purposes.

直接从手册引用:

运行测试套件所需的类不应包含在 主要的自动加载规则,以避免污染生产中的自动加载器 以及其他人将您的软件包用作依赖项.

Classes needed to run the test suite should not be included in the main autoload rules to avoid polluting the autoloader in production and when other people use your package as a dependency.

因此,最好为您的单位使用专用路径 测试并将其添加到autoload-dev部分.

Therefore, it is a good idea to rely on a dedicated path for your unit tests and to add it within the autoload-dev section.

示例:

{
    "autoload": {
        "psr-4": { "MyLibrary\\": "src/" }
    },
    "autoload-dev": {
        "psr-4": { "MyLibrary\\Tests\\": "tests/" }
    }
}

添加到Composer自动加载器

一种替代方法是获取Composer Autoloader和add您的测试名称空间(如果有的话)以及测试所在的目录.如何执行此操作,如手册中所述 (位于底部基本用法"中的自动加载部分为:

Add to the Composer Autoloader

An alternative would be to get the Composer Autoloader and add your testing namespace (if you have any) and the directory where your tests live. How to do this, as described in the manual (at the bottom of the autoloading section in "Basic Usage") is :

$loader = require('/path/to/vendor/autoload.php');
$loader->add('Test\\', __DIR__ . '/Tests');

如果测试使用的名称空间反映了测试目录,而您仍然遇到麻烦,则可以尝试通过将第一个参数('Test\\')替换为''来省略前缀.

If your tests use namespaces that mirror the test directory and you still run into trouble, you can try omitting the prefix by replacing the first parameter ('Test\\') with ''.

如果您想进一步了解所有这些工作原理,请查看

If you want further insight into how all of this works you should take a look at the Composer ClassLoader class, especially the add() and findFile() methods.

这篇关于为PHPUnit创建基础测试类并将其扩展为通用功能会导致未找到类错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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