Skip to content

Instantly share code, notes, and snippets.

@jamband
Created June 16, 2012 05:02
Show Gist options
  • Save jamband/2939987 to your computer and use it in GitHub Desktop.
Save jamband/2939987 to your computer and use it in GitHub Desktop.
Yii Framework: Functional test case.
<?php
return array(
'Word_1' => array(
'userid' => '1',
'en' => 'aaa',
'ja' => 'えーえーえー',
),
'Word_2' => array(
'userid' => '1',
'en' => 'bbb',
'ja' => 'びーびーびー',
),
'Word_3' => array(
'userid' => '1',
'en' => 'ccc',
'ja' => 'しーしーしー',
),
'Word_4' => array(
'userid' => '1',
'en' => 'ddd',
'ja' => 'でぃーでぃーでぃー',
),
'Word_5' => array(
'userid' => '1',
'en' => 'eee',
'ja' => 'いーいーいー',
),
'Word_6' => array(
'userid' => '2',
'en' => 'abc',
'ja' => 'えーびーしー',
),
'Word_7' => array(
'userid' => '2',
'en' => 'def',
'ja' => 'でぃーいーえふ',
),
);
<?php
class WordTest extends WebTestCase
{
public $fixtures = array(
'words' => 'Word',
'users' => 'User',
);
public function test_index()
{
$location = 'word/index';
$this->open($location);
$this->login();
$this->assertLocation($location);
$this->assertTextPresent('5 results');
$this->checkSortLink($location);
}
public function test_edit()
{
$location = 'word/edit';
$this->open($location);
$this->login();
$this->assertLocation($location);
$this->assertTextPresent('5 results');
$this->checkSortLink($location);
// verify search form
$this->clickAndWait("//input[@value='submit']");
$this->assertLocation('word/edit');
$this->assertTextPresent('5 results');
$this->type('name=q', '  a ');
$this->clickAndWait("//input[@value='submit']");
$this->assertTextPresent('1 results');
$this->assertTextPresent('aaa');
$this->assertTextNotPresent('bbb');
// verify insert form
$this->clickAndWait("//input[@value='Create']");
$this->assertTextPresent('英単語が入力されていません');
$this->assertTextPresent('日本語訳が入力されていません');
$this->type('name=Word[en]', 'fff');
$this->type('name=Word[ja]', 'えふえふえふ');
$this->clickAndWait("//input[@value='Create']");
$this->assertLocation('word/8');
$this->assertTextPresent('英単語の追加が完了いたしました');
$this->assertTextPresent('fff');
$this->assertTextPresent('えふえふえふ');
}
public function test_view()
{
$location = 'word/1';
$this->open($location);
$this->login();
$this->assertLocation($location);
$this->assertTitle('aaa - English Wordbook yii - View Word');
$this->assertTextPresent('aaa');
$this->assertTextPresent('えーえーえー');
// test 404 exception error
$this->open('word/99999');
$this->assertTextPresent('データがありません');
}
public function test_update()
{
$location = 'word/update/1';
$this->open($location);
$this->login();
$this->assertLocation($location);
$this->assertTitle('aaa - English Wordbook yii - Update Word');
$this->assertValue('name=Word[en]', 'aaa');
$this->assertValue('name=Word[ja]', 'えーえーえー');
// test 404 exception error
$this->open('word/update/99999');
$this->assertTextPresent('データがありません');
// verify update form
$this->open($location);
$this->type('name=Word[en]', 'aaaa');
$this->type('name=Word[ja]', 'えーえーえーえー');
$this->clickAndWait("//input[@value='Update']");
$this->assertLocation('word/1');
$this->assertTextPresent('英単語の更新が完了いたしました');
$this->assertTextPresent('aaaa');
$this->assertTextPresent('えーえーえーえー');
}
public function test_delete()
{
$location = 'word/delete/5';
$this->open($location);
$this->login();
$this->assertLocation($location);
$this->assertTextPresent('無効なリクエストです');
$this->open('word/edit');
$this->assertTextPresent('eee');
$this->clickAndWait('yt1');
$this->assertConfirmation('この項目を削除してもよろしいですか?');
$this->assertLocation('word/edit');
$this->assertTextPresent('英単語の削除が完了いたしました');
$this->assertTextNotPresent('eee');
$this->assertTextPresent('4 results');
}
public function login()
{
$this->assertElementPresent('name=LoginForm[password]');
$this->type('name=LoginForm[username]', 'admin');
$this->type('name=LoginForm[password]', 'admin');
$this->clickAndWait("//input[@value='Login']");
}
public function checkSortLink($location)
{
$this->clickAndWait('link=a');
$this->assertLocation($location.'/sort/a');
$this->assertTextPresent('1 results');
$this->assertTextPresent('aaa');
$this->assertTextNotPresent('bbb');
$this->clickAndWait('link=b');
$this->assertLocation($location.'/sort/b');
$this->assertTextPresent('1 results');
$this->assertTextPresent('bbb');
$this->assertTextNotPresent('aaa');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment