【发布时间】:2017-02-21 19:42:15
【问题描述】:
我使用 api-platform 框架创建了一个非常简单的基本 api。 现在我正在尝试使用 PHP 单元编写一个简单的单元测试,但是当我尝试运行测试时,我不断收到此错误:
Class 'PHPUnit_Framework_TestCase' not found
它在抱怨这条线:
class JobControllerTest extends \PHPUnit_Framework_TestCase
上面写着undefined class
我用 composer 安装了 phpunit 我不知道我做错了什么。
这是我的完整测试:
<?php
namespace tests\AppBundle;
class JobControllerTest extends \PHPUnit_Framework_TestCase
{
public function testPOST()
{
$client = new Client('http://localhost:8000', array(
'request.options' => array(
'exceptions' => false,
)
));
$data = array(
'bar' => "hello",
);
$request = $client->post('/foos', null, json_encode($data));
$response = $request->send();
$this->assertEquals(201, $response->getStatusCode());
$this->assertTrue($response->hasHeader('Location'));
$data = json_decode($response->getBody(true), true);
$this->assertArrayHasKey('bar', $data);
}
}
如果有人可以帮助我,那将是非常酷的:)
非常感谢!
【问题讨论】:
-
这是因为你安装了phpunit v6。安装 v5 或 v4 就可以了。对此其实有不少讨论。
-
@Cerad 我在 GitHub 上看到了 :) 谢谢你告诉我!
-
@Cerad 你知道为什么
$client = new Client(不起作用吗?我做了一个作曲家需要 guzzlehttp/guzzle,但似乎没有工作:p -
你看过测试一章吗? Client 是一个 HttpFoundation 类,而不是 guzzle 类。
-
您可以尝试使用stackoverflow.com/questions/42811164/… 答案中的方法为
PHPUnit 6以及早期版本做好准备。
标签: php unit-testing symfony phpunit