ink / haphazard
该包已被 废弃 且不再维护。未建议替代包。
用于快速且粗糙的页面功能测试的库
v2.2.0
2014-06-26 19:57 UTC
Requires
- php: >=5.3.0
- symfony/symfony: >=2.3
README
Haphazard 是 Symfony's WebTestCase 的库扩展,它提供了一种快速且粗糙的方法来对基本页面进行功能测试。
安装
您可以通过 composer 安装 Haphazard
composer.phar require ink/haphazard
使用
简单的页面加载测试
当您只想测试页面是否至少正确加载时,可以使用 HaphazardTestCase::assertGet()
或 HaphazardTestCase::assertPost()
方法。这将创建一个网络客户端并断言页面返回 200 状态码。
use Ink\Haphazard\TestCase\HaphazardTestCase;
class ProductControllerTest extends HaphazardTestCase
{
/**
* Test Index Action
*/
public function testIndexAction()
{
$this->assertGet('index');
}
/**
* Test Post Action
*/
public function testCreatePostAction()
{
$this->assertPost('create');
}
}
带参数的页面
如果您正在测试的页面需要参数,您也可以传递这些参数。
/**
* Test View Action
*/
public function testViewAction()
{
$this->assertGet('product-view', ['productId' => 1]);
}
/**
* Test Edit Action
*/
public function testEditAction()
{
$this->assertPost('product-edit', ['productId' => 1]);
}
带有 POST 体的页面
如果您需要在 POST 请求中发送额外的参数,也可以发送。
/**
* Test Edit Action
*/
public function testEditAction()
{
$this->assertPost('product-edit', ['productId' => 1], ['product-name' => 'Acme Product']);
}
不同的状态码
有时您会想测试页面返回不同的状态码,例如,当匿名用户应该 无法 访问特定页面时,返回 403/禁止状态码,或者当页面重定向时返回 302 状态码。
/**
* Test Edit Action
*/
public function testEditAction()
{
$this->assertGet('product-edit', ['productId' => 1], 403);
}
/**
* Test Edit Action
*/
public function testEditAction()
{
$this->assertPost('product-edit', ['productId' => 1], ['product-name' => 'Acme Product'], 302);
}
伪造身份验证角色
为了有效地测试页面是否对正确的用户开放/关闭,该库提供了一个使用指定角色进行断言的简单方法。
/**
* Test Edit Action
*/
public function testEditAction()
{
// Allow our Admin role
$user = new User();
$this->login($user);
$this->assertGet('product-edit', ['productId' => 1], 200);
// Disallow Anonymous users
$this->refreshClient();
$this->assertGet('product-edit', ['productId' => 1], 403);
}