CodeIgniter4 HTTP 測(cè)試

2020-08-17 17:46 更新

功能測(cè)試使您可以查看對(duì)應(yīng)用程序的一次調(diào)用的結(jié)果。這可能是返回單個(gè)Web表單的結(jié)果,命中API端點(diǎn)等等。這很方便,因?yàn)樗试S您測(cè)試單個(gè)請(qǐng)求的整個(gè)生命周期,確保路由有效,響應(yīng)是正確的格式,分析結(jié)果等等。

測(cè)試類

功能測(cè)試要求您所有的測(cè)試類都對(duì)該CodeIgniter\Test\FeatureTestCase類進(jìn)行擴(kuò)展。由于這擴(kuò)展了CIDatabaseTestCase,因此您必須始終確保 在執(zhí)行操作之前調(diào)用parent::setUp()parent::tearDown()。

  1. <?php namespace App;
  2. use CodeIgniter\Test\FeatureTestCase;
  3. class TestFoo extends FeatureTestCase
  4. {
  5. public function setUp()
  6. {
  7. parent::setUp();
  8. }
  9. public function tearDown()
  10. {
  11. parent::tearDown();
  12. }
  13. }

請(qǐng)求頁(yè)面

本質(zhì)上,F(xiàn)eatureTestCase僅允許您在應(yīng)用程序上調(diào)用終結(jié)點(diǎn)并返回結(jié)果。為此,您可以使用call()方法。第一個(gè)參數(shù)是要使用的HTTP方法(最常見(jiàn)的是GET或POST)。第二個(gè)參數(shù)是您網(wǎng)站上要測(cè)試的路徑。第三個(gè)參數(shù)接受一個(gè)數(shù)組,該數(shù)組用于填充您正在使用的HTTP動(dòng)詞的超全局變量。因此,GET方法將填充$ _GET變量,而發(fā)布請(qǐng)求將填充$ _POST數(shù)組。

  1. // Get a simple page
  2. $result = $this->call('get', site_url());
  3. // Submit a form
  4. $result = $this->call('post', site_url('contact'), [
  5. 'name' => 'Fred Flintstone',
  6. 'email' => 'flintyfred@example.com'
  7. ]);

存在用于每個(gè)HTTP動(dòng)詞的簡(jiǎn)寫方法,以簡(jiǎn)化鍵入并使內(nèi)容更清晰:

  1. $this->get($path, $params);
  2. $this->post($path, $params);
  3. $this->put($path, $params);
  4. $this->patch($path, $params);
  5. $this->delete($path, $params);
  6. $this->options($path, $params);

注解

$ params數(shù)組并不是每個(gè)HTTP動(dòng)詞都有意義,但為了保持一致性而包含了該數(shù)組。

設(shè)定不同的路線

您可以通過(guò)將“路線”數(shù)組傳遞給withRoutes()方法來(lái)使用路線的自定義集合。這將覆蓋系統(tǒng)中的所有現(xiàn)有路由:

  1. $routes = [
  2. [ 'get', 'users', 'UserController::list' ]
  3. ];
  4. $result = $this->withRoutes($routes)
  5. ->get('users');

每個(gè)“路由”都是一個(gè)3元素?cái)?shù)組,包含HTTP動(dòng)詞(或全部為“添加”),要匹配的URI和路由目的地。

設(shè)置會(huì)話值

您可以使用方法設(shè)置自定義會(huì)話值,以在單個(gè)測(cè)試中使用withSession()。發(fā)出此請(qǐng)求時(shí),這需要鍵/值對(duì)的數(shù)組,這些鍵/值對(duì)應(yīng)存在于$ _SESSION變量中。這對(duì)于測(cè)試身份驗(yàn)證非常方便。

  1. $values = [
  2. 'logged_in' => 123
  3. ];
  4. $result = $this->withSession($values)
  5. ->get('admin');

繞過(guò)事件

事件很容易在您的應(yīng)用程序中使用,但在測(cè)試過(guò)程中可能會(huì)出現(xiàn)問(wèn)題。尤其是用于發(fā)送電子郵件的事件。您可以使用以下skipEvents()方法告訴系統(tǒng)跳過(guò)任何事件處理:

  1. $result = $this->skipEvents()
  2. ->post('users', $userInfo);

測(cè)試響應(yīng)

執(zhí)行一個(gè) call()并獲得結(jié)果后,可以在測(cè)試中使用許多新的斷言。

注解

響應(yīng)對(duì)象可從公開(kāi)獲得$result-&response。如果需要,可以使用該實(shí)例對(duì)它執(zhí)行其他聲明。

檢查響應(yīng)狀態(tài)

isOK()

根據(jù)響應(yīng)是否被認(rèn)為是“ ok”,返回布爾值true / false。這主要由200或300的響應(yīng)狀態(tài)代碼確定。

  1. if ($result->isOK())
  2. {
  3. ...
  4. }

assertOK()

該論斷僅使用isOK()方法來(lái)測(cè)試響應(yīng)。

  1. $this->assertOK();

isRedirect()

根據(jù)響應(yīng)是否為重定向響應(yīng),返回布爾值true / false。

  1. if ($result->isRedirect())
  2. {
  3. ...
  4. }

assertRedirect()

論斷該響應(yīng)是RedirectResponse的一個(gè)實(shí)例。

  1. $this->assertRedirect();

assertStatus(int $code)

論斷返回的HTTP狀態(tài)代碼與$ code相匹配。

  1. $this->assertStatus(403);

會(huì)議斷言

assertSessionHas(string $key, $value = null)

斷言結(jié)果會(huì)話中存在值。如果傳遞了$ value,還將斷言該變量的值與指定的值匹配。

  1. $this->assertSessionHas('logged_in', 123);

assertSessionMissing(string $key)

斷言結(jié)果會(huì)話不包含指定的$ key。

  1. $this->assertSessionMissin('logged_in');

標(biāo)頭斷言

assertHeader(string $key, $value = null)

斷言響應(yīng)中存在名為$ key的標(biāo)頭。如果$ value不為空,還將斷言這些值匹配。

  1. $this->assertHeader('Content-Type', 'text/html');

assertHeaderMissing(string $key)

斷言響應(yīng)中不存在標(biāo)頭名稱$ key

  1. $this->assertHeader('Accepts');

Cookie斷言

assertCookie(string $key, $value = null, string $prefix = ‘’)

斷言響應(yīng)中存在一個(gè)名為$ key的cookie 。如果$ value不為空,還將斷言這些值匹配。您可以根據(jù)需要通過(guò)將cookie前綴作為第三個(gè)參數(shù)傳遞來(lái)設(shè)置它。

  1. $this->assertCookie('foo', 'bar');

assertCookieMissing(string $key)

斷言響應(yīng)中不存在名為$ key的cookie 。

  1. $this->assertCookieMissing('ci_session');

assertCookieExpired(string $key, string $prefix = ‘’)

斷言存在一個(gè)名為$ key的cookie ,但已過(guò)期。您可以根據(jù)需要通過(guò)將cookie前綴作為第二個(gè)參數(shù)傳遞來(lái)設(shè)置它。

  1. $this->assertCookieExpired('foo');

DOM論斷

您可以執(zhí)行測(cè)試,以查看帶有以下聲明的響應(yīng)的正文中是否存在特定的元素/文本/等。

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

斷言文本/ HTML是否在頁(yè)面上,無(wú)論是本身,還是(更具體而言)在標(biāo)簽內(nèi),由類型,類或id指定:

  1. // Check that "Hello World" is on the page
  2. $this->assertSee('Hello World');
  3. // Check that "Hello World" is within an h1 tag
  4. $this->assertSee('Hello World', 'h1');
  5. // Check that "Hello World" is within an element with the "notice" class
  6. $this->assertSee('Hello World', '.notice');
  7. // Check that "Hello World" is within an element with id of "title"
  8. $this->assertSee('Hellow World', '#title');

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

聲明與assertSee()方法完全相反的地方:

  1. // Checks that "Hello World" does NOT exist on the page
  2. $results->dontSee('Hello World');
  3. // Checks that "Hello World" does NOT exist within any h1 tag
  4. $results->dontSee('Hello World', 'h1');

assertSeeElement(string $search)

assertSee()類似,但是這僅檢查現(xiàn)有元素。它不檢查特定的文本:

  1. // Check that an element with class 'notice' exists
  2. $results->seeElement('.notice');
  3. // Check that an element with id 'title' exists
  4. $results->seeElement('#title')

assertDontSeeElement(string $search)

assertSee()類似,但是這僅檢查缺少的現(xiàn)有元素。它不檢查特定的文本:

  1. // Verify that an element with id 'title' does NOT exist
  2. $results->dontSeeElement('#title');

assertSeeLink(string $text, string $details=null)

斷言找到一個(gè)匹配標(biāo)簽為$ text的錨標(biāo)簽:

  1. // Check that a link exists with 'Upgrade Account' as the text::
  2. $results->seeLink('Upgrade Account');
  3. // Check that a link exists with 'Upgrade Account' as the text, AND a class of 'upsell'
  4. $results->seeLink('Upgrade Account', '.upsell');

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

斷言輸入標(biāo)簽具有名稱和值:

  1. // Check that an input exists named 'user' with the value 'John Snow'
  2. $results->seeInField('user', 'John Snow');
  3. // Check a multi-dimensional input
  4. $results->seeInField('user[name]', 'John Snow');

使用JSON

響應(yīng)通常包含JSON響應(yīng),尤其是在使用API方法時(shí)。以下方法可以幫助測(cè)試響應(yīng)。

getJSON()

此方法將以JSON字符串的形式返回響應(yīng)的主體:

  1. // Response body is this:
  2. ['foo' => 'bar']
  3. $json = $result->getJSON();
  4. // $json is this:
  5. {
  6. "foo": "bar"
  7. }

注解

請(qǐng)注意,JSON字符串將漂亮地打印在結(jié)果中。

assertJSONFragment(array $fragment)

斷言$fragment在JSON響應(yīng)中找到。它不需要匹配整個(gè)JSON值。

  1. // Response body is this:
  2. [
  3. 'config' => ['key-a', 'key-b']
  4. ]
  5. // Is true
  6. $this->assertJSONFragment(['config' => ['key-a']);

注解

這只是使用phpUnit自己的assertArraySubset() 方法進(jìn)行比較。

assertJSONExact($test)

assertJSONFragment()相似,但是檢查整個(gè)JSON響應(yīng)以確保完全匹配。

使用XML

getXML()

如果您的應(yīng)用程序返回XML,則可以通過(guò)此方法檢索它。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)