【问题标题】:PHP Fatal error: Class 'PHPUnit\Framework\TestCase' not found with PHPUnit 6 and PHP 7.0PHP 致命错误:在 PHPUnit 6 和 PHP 7.0 中找不到类 'PHPUnit\Framework\TestCase'
【发布时间】:2017-02-14 09:52:32
【问题描述】:

我正在玩 php 7 和 phpunit 6。这是我写的测试:

<?php declare(strict_types=1);

namespace Test;

use DesignPatterns\Observer\User;
use DesignPatterns\Observer\UserObserver;
use PHPUnit\Framework\TestCase;

class ObserverTest extends TestCase
{
    public function testChangeInUserLeadsToUserObserverBeingNotified()
    {
        $observer = new UserObserver();

        $user = new User();
        $user->attach($observer);

        $user->changeEmail('foo@bar.com');
        $this->assertCount(1, $observer->getChangedUsers());
    }
}

当我尝试运行此测试时,我收到以下错误消息:

PHP Fatal error:  Class 'PHPUnit\Framework\TestCase' not found in /home/.../.../Test/ObserverTest.php on line 9

我用 composer 安装了 PHPUnit,这是我的 composer.json 文件内容:

{
    "require": {
        "phpunit/phpunit": "^6.0"
    },
    "autoload": {
        "psr-4": {"DesignPatterns\\": "src/"}
    }
}

根据PHPUnit 6 documentation,您的测试现在应该扩展 PHPUnit\Framework\TestCase 而不是 PHPUnit_Framework_TestCase。

我知道这不是自动加载的问题。实际上,如果我用 PHPUnit_Framework_TestCase 替换 PHPUnit\Framework\TestCase,它工作得很好,但我想知道为什么这个语法不起作用。

我在 google、stackoverflow 和 PHPUnit 的 github 存储库上尝试了一些研究,但找不到任何东西。

期待你的回答,

编辑

这是我的文件的组织方式:

src/
├── DataMapper
│   ├── StorageAdapter.php
│   ├── UserMapper.php
│   └── User.php
├── Observer
│   ├── UserObserver.php
│   └── User.php
Test/
├── DataMapperTest.php
└── ObserverTest.php

【问题讨论】:

  • 进入供应商并查看phpunit是否存在
  • 测试文件在src文件夹中?如果是,它应该在命名空间中有 DesignPatterns 后缀?
  • 嗨,是的,phpunit 在那里。
  • 不,测试文件不在 src/ 中,它在 Test 文件夹中
  • 如何在自动加载器中加载测试文件夹?

标签: php unit-testing phpunit php-7


【解决方案1】:

我发布这个以防有人在升级到 Symfony 4.4 / 5.0 后出现此错误。

在较新版本的 Symfony 中,您有一个在 phpunit.xml.dist 中定义的版本。此版本将在您运行./bin/phpunit 时下载,其源代码将放在./bin/.phpunit/phpunit-VERSION 下。如果您正在执行php ./bin/console --env=test debug:container,您可能会得到 OP 异常提到的内容。修复很简单 - 您需要在 composer.json 中添加类的路径:

    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        },
        "classmap": [
            "tests/",
            "bin/.phpunit/phpunit-9.2.0-0/src" <--- Make sure the version is correct.
        ]
    },

然后运行composer dump-autoload,最后php ./bin/console --env=test debug:container 应该可以工作了。

【讨论】:

  • 我最终得到了这个:"autoload-dev": { "psr-4": { "App\\Tests\\": "tests/" }, "classmap": [ "bin/.phpunit/phpunit/src" ] },bin/.phpunit 是一个符号链接。感谢分享!
【解决方案2】:

我找到了答案:

我正在用这个命令行执行我的测试:

phpunit Test/ObserverTest.php

PHPUnit 全局安装在我的电脑上,但它是 5.1.3 版本:

phpunit -v

PHPUnit 5.1.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.13-0ubuntu0.16.04.1 with Xdebug 2.4.0
Configuration: /home/.../.../DesignPatterns/phpunit.xml

并且语法 PHPUnit\Framework\TestCase 仅适用于 PHPUnit 6

现在,如果我运行 php vendor/bin/phpunit Test/ObserverTest.php,它会完美运行...

【讨论】:

  • 确认一下,你只更新了phpunit的版本到v5.1.3?
【解决方案3】:

在我的例子中,我之前在 /Applications/MAMP/Library/bin 目录中的路径中有一个不同版本的 phpunit。在创建指向全局安装版本 (7.5.1) 而不是 5.1.3 的符号链接后,这为我修复了错误:

cd /Applications/MAMP/Library/bin
mv phpunit phpunit_bak
ln -s /usr/local/bin/phpunit phpunit

【讨论】:

    【解决方案4】:

    以防万一您面临在 PhpStorm 中找不到的 PHPUnit\Framework\TestCase

    在项目根目录运行php bin/phpunit,它将下载所有需要的类,PhpStorm 错误应该消失

    【讨论】:

    • php bin/phpunit 给我 phpunit 信息和手册
    猜你喜欢
    • 2022-01-02
    • 2013-09-27
    • 1970-01-01
    • 2017-02-24
    • 2013-01-04
    • 1970-01-01
    • 2014-06-11
    • 2014-10-02
    相关资源
    最近更新 更多