测试响应

TestResponse 类提供了许多有用的函数来解析测试用例中的响应并对其进行测试。通常, TestResponse 将作为你的结果提供, 控制器测试HTTP 功能测试,但你始终可以直接使用任何 ResponseInterface 创建自己的:

$result = new \CodeIgniter\Test\TestResponse($response);
$result->assertOK();

测试响应

无论你是从测试中获得 TestResponse 还是自己创建,都可以在测试中使用许多新的断言。

访问请求/响应

request()

如果在测试期间设置了请求,你可以直接访问请求对象:

$request = $results->request();

response()

这允许你直接访问响应对象:

$response = $results->response();

检查响应状态

isOK()

根据响应是否被视为“正常”返回布尔值 true/false。这主要由 200 或 300 范围内的响应状态代码确定。如果重定向,空响应正文不被视为有效。

if ($result->isOK()) {
    // ...
}

assertOK()

此断言简单地使用 isOK() 方法来测试响应。assertNotOK() 是此断言的逆。

$result->assertOK();

isRedirect()

根据响应是否重定向返回布尔值 true/false。

if ($result->isRedirect()) {
    // ...
}

assertRedirect()

断言响应是 RedirectResponse 的一个实例。assertNotRedirect() 是此断言的逆。

$result->assertRedirect();

assertRedirectTo()

断言响应是一个 RedirectResponse 实例,且目标与给定的 uri 匹配。

$result->assertRedirectTo('foo/bar');

getRedirectUrl()

返回设置为 RedirectResponse 的 URL,如果失败则为 null。

$url = $result->getRedirectUrl();
$this->assertEquals(site_url('foo/bar'), $url);

assertStatus(int $code)

断言返回的 HTTP 状态码匹配 $code。

$result->assertStatus(403);

Session 断言

assertSessionHas(string $key, $value = null)

断言结果 Session 中存在一个值。如果传递了 $value,还将断言变量的值与指定的相匹配。

$result->assertSessionHas('logged_in', 123);

assertSessionMissing(string $key)

断言结果 Session 不包括指定的 $key。

$result->assertSessionMissing('logged_in');

DOM 辅助函数

你得到的响应包含许多帮助方法来检查响应中的 HTML 输出。这些在测试中的断言中很有用。

see()

根据页面上的文本是否存在,返回一个布尔值 true/false。可以通过 type、class 或 id 来指定文本所在的标签。

// Check that "Hello World" is on the page
if ($results->see('Hello World')) {
    // ...
}

// Check that "Hello World" is within an h1 tag
if ($results->see('Hello World', 'h1')) {
    // ...
}

// Check that "Hello World" is within an element with the "notice" class
if ($results->see('Hello World', '.notice')) {
    // ...
}

// Check that "Hello World" is within an element with id of "title"
if ($results->see('Hello World', '#title')) {
    // ...
}

dontSee() 方法正好相反:

// Checks that "Hello World" does NOT exist on the page
if ($results->dontSee('Hello World')) {
    // ...
}

// Checks that "Hellow World" does NOT exist within any h1 tag
if ($results->dontSee('Hello World', 'h1')) {
    // ...
}

seeElement()

seeElement()dontSeeElement() 与前面的方法非常相似,但不检查元素的值。相反,它们仅检查元素是否存在于页面上:

// Check that an element with class 'notice' exists
if ($results->seeElement('.notice')) {
    // ...
}

// Check that an element with id 'title' exists
if ($results->seeElement('#title')) {
    // ...
}

// Verify that an element with id 'title' does NOT exist
if ($results->dontSeeElement('#title')) {
    // ...
}

seeInField()

seeInField() 方法检查是否存在具有给定名称和值的输入标签:

// Check that an input exists named 'user' with the value 'John Snow'
if ($results->seeInField('user', 'John Snow')) {
    // ...
}

// Check a multi-dimensional input
if ($results->seeInField('user[name]', 'John Snow')) {
    // ...
}

seeCheckboxIsChecked()

最后,你可以使用 seeCheckboxIsChecked() 方法检查复选框是否存在并已被选中:

// Check if checkbox is checked with class of 'foo'
if ($results->seeCheckboxIsChecked('.foo')) {
    // ...
}

// Check if checkbox with id of 'bar' is checked
if ($results->seeCheckboxIsChecked('#bar')) {
    // ...
}

DOM 断言

你可以使用以下断言来测试响应正文中是否存在特定元素/文本等。

assertSee(string $search = null, string $element = null)

断言文本/HTML 存在于页面上,无论是自身存在还是更具体地说是存在于由类型、类或 id 指定的标记内:

// Verify that "Hello World" is on the page
$result->assertSee('Hello World');

// Verify that "Hello World" is within an h1 tag
$result->assertSee('Hello World', 'h1');

// Verify that "Hello World" is within an element with the "notice" class
$result->assertSee('Hello World', '.notice');

// Verify that "Hello World" is within an element with id of "title"
$result->assertSee('Hello World', '#title');

assertDontSee(string $search = null, string $element = null)

assertSee() 方法完全相反:

// Verify that "Hello World" does NOT exist on the page
$results->assertDontSee('Hello World');

// Verify that "Hello World" does NOT exist within any h1 tag
$results->assertDontSee('Hello World', 'h1');

assertSeeInField(string $field, string $value = null)

断言存在具有给定名称和值的输入标签:

// Verify that an input exists named 'user' with the value 'John Snow'
$results->assertSeeInField('user', 'John Snow');

// Verify a multi-dimensional input
$results->assertSeeInField('user[name]', 'John Snow');

使用 JSON

响应通常会包含 JSON 响应,特别是在使用 API 方法时。以下方法可以帮助测试响应。

getJSON()

此方法将以 JSON 字符串的形式返回响应正文:

/*
 * Response body is this:
 * ['foo' => 'bar']
 */

$json = $result->getJSON();
/*
 * $json is this:
 * {
 *     "foo": "bar"
 * }
`*/

你可以使用此方法来确定 $response 是否确实包含 JSON 内容:

// Verify the response is JSON
$this->assertTrue($result->getJSON() !== false);

备注

请注意结果中的 JSON 字符串将美化打印。

assertJSONFragment(array $fragment)

断言 $fragment 在 JSON 响应中找到。它不需要匹配整个 JSON 值。

/*
 * Response body is this:
 * [
 *     'config' => ['key-a', 'key-b'],
 * ]
 */

// Is true
$result->assertJSONFragment(['config' => ['key-a']]);

assertJSONExact($test)

assertJSONFragment() 类似,但检查整个 JSON 响应以确保完全匹配。

使用 XML

getXML()

如果应用程序返回 XML,则可以通过此方法检索它。