レスポンスのテスト
TestResponse
クラスは、テストケースからのレスポンスを解析およびテストするための多くの便利な関数を提供します。コントローラーテストまたはHTTP機能テストの結果として、通常TestResponse
が提供されますが、任意のResponseInterface
を使用して独自に作成することもできます。
$result = new \CodeIgniter\Test\TestResponse($response);
$result->assertOK();
レスポンスのテスト
テストの結果としてTestResponse
を受け取った場合でも、自分で作成した場合でも、テストで使用できる新しいアサーションがいくつかあります。
リクエスト/レスポンスへのアクセス
request()
テスト中に設定されている場合、Requestオブジェクトに直接アクセスできます。
$request = $results->request();
response()
これにより、レスポンスオブジェクトに直接アクセスできます。
$response = $results->response();
レスポンスステータスの確認
isOK()
レスポンスが「OK」と見なされるかどうかを基に、真/偽のブール値を返します。これは主に、200または300台のレスポンスステータスコードによって決定されます。リダイレクトでない限り、空のボディは有効とは見なされません。
if ($result->isOK()) {
// ...
}
assertOK()
このアサーションは、isOK()
メソッドを使用してレスポンスをテストします。assertNotOK()
はこのアサーションの逆です。
$result->assertOK();
isRedirect()
レスポンスがリダイレクトレスポンスかどうかを基に、真/偽のブール値を返します。
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);
セッションアサーション
assertSessionHas(string $key, $value = null)
結果のセッションに値が存在することをアサートします。$valueが渡された場合は、変数の値が指定された値と一致することもアサートします。
$result->assertSessionHas('logged_in', 123);
assertSessionMissing(string $key)
結果のセッションに指定された$keyが含まれていないことをアサートします。
$result->assertSessionMissing('logged_in');
ヘッダーアサーション
assertHeader(string $key, $value = null)
$key
という名前のヘッダーがレスポンスに存在することをアサートします。$value
が空でない場合は、値が一致することもアサートします。
$result->assertHeader('Content-Type', 'text/html');
assertHeaderMissing(string $key)
$key
という名前のヘッダーがレスポンスに存在しないことをアサートします。
$result->assertHeader('Accepts');
DOMヘルパー
返されるレスポンスには、レスポンス内のHTML出力を検査するための多数のヘルパーメソッドが含まれています。これらは、テストのアサーションで使用すると便利です。
see()
ページ上のテキストが、それ自体で、またはタイプ、クラス、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')) {
// ...
}
seeLink()
seeLink()
を使用して、指定されたテキストを持つリンクがページ上に表示されることを確認できます。
// Check that a link exists with 'Upgrade Account' as the text::
if ($results->seeLink('Upgrade Account')) {
// ...
}
// Check that a link exists with 'Upgrade Account' as the text, AND a class of 'upsell'
if ($results->seeLink('Upgrade Account', '.upsell')) {
// ...
}
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');
assertSeeElement(string $search)
assertSee()
に似ていますが、これは既存の要素のみを確認します。特定のテキストは確認しません。
// Verify that an element with class 'notice' exists
$results->assertSeeElement('.notice');
// Verify that an element with id 'title' exists
$results->assertSeeElement('#title');
assertDontSeeElement(string $search)
assertSee()
に似ていますが、これは存在しない要素のみを確認します。特定のテキストは確認しません。
// Verify that an element with id 'title' does NOT exist
$results->assertDontSeeElement('#title');
assertSeeLink(string $text, string $details = null)
アンカータグの中に、本文として一致する$text
を持つものが存在することをアサートします。
// Verify that a link exists with 'Upgrade Account' as the text::
$results->assertSeeLink('Upgrade Account');
// Verify that a link exists with 'Upgrade Account' as the text, AND a class of 'upsell'
$results->assertSeeLink('Upgrade Account', '.upsell');
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の操作
レスポンスは、特にAPIメソッドを使用する場合、頻繁にJSONレスポンスを含みます。以下のメソッドは、レスポンスのテストに役立ちます。
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を返す場合、このメソッドで取得できます。