我正在玩 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 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
,它会完美运行...
关于PHP fatal error : Class 'PHPUnit\Framework\TestCase' not found with PHPUnit 6 and PHP 7. 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42222836/