Skip to content

Instantly share code, notes, and snippets.

@list
Created October 18, 2011 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save list/1296393 to your computer and use it in GitHub Desktop.
Save list/1296393 to your computer and use it in GitHub Desktop.
Functional test tanpa Selenium di Yii
<?php
Yii::import('ext.httpclient.*');
Yii::import('ext.httpclient.adapter.*');
class PagesControllerTest extends PHPUnit_Framework_TestCase {
public function testHome() {
$client = new EHttpClient(TEST_BASE_URL.'/pages/home');
$this->assertTrue($client->request()->isSuccessful());
}
public function testContact() {
$client = new EHttpClient(TEST_BASE_URL.'/pages/contact');
$this->assertTrue($client->request()->isSuccessful());
}
public function testError() {
$client = new EHttpClient(TEST_BASE_URL.'/pages/error-not-found');
$this->assertTrue($client->request()->isError());
}
public function testLoginLogout() {
$client = new EHttpClient(TEST_BASE_URL.'/site/login');
$client->setCookieJar();
$this->assertTrue($client->request()->isSuccessful());
$this->assertRegExp('/Login/', $client->request('POST')->getBody());
$client->resetParameters();
$client->setParameterPost(array(
'LoginForm[username]' => '',
'LoginForm[password]' => ''
));
$client->request('POST');
$this->assertRegExp('/Username cannot be blank./', $client->getLastResponse()->getBody());
$this->assertRegExp('/Password cannot be blank./', $client->getLastResponse()->getBody());
$client->resetParameters();
$client->setParameterPost(array(
'LoginForm[username]' => 'wronguser',
'LoginForm[password]' => 'wrongpass'
));
$this->assertRegExp('/Incorrect username or password./', $client->request('POST')->getBody());
$client->resetParameters();
$client->setParameterPost(array(
'LoginForm[username]' => 'demo',
'LoginForm[password]' => 'demo'
));
$client->request('POST');
$this->assertNotRegExp('/Username cannot be blank./', $client->getLastResponse()->getBody());
$this->assertNotRegExp('/Password cannot be blank./', $client->getLastResponse()->getBody());
$client->setUri(TEST_BASE_URL);
$this->assertTrue($client->request()->isSuccessful());
$this->assertRegExp('/Logout \(demo\)/', $client->request('POST')->getBody());
$client->setUri(TEST_BASE_URL.'/site/logout');
$this->assertTrue($client->request()->isSuccessful());
$client->setUri(TEST_BASE_URL);
$this->assertNotRegExp('/Logout \(demo\)/', $client->request('POST')->getBody());
}
}
@list
Copy link
Author

list commented Oct 18, 2011

Langkah-langkahnya:

  1. Install ekstensi Yii ehttpclient.
  2. Import dengan
Yii::import('ext.httpclient.adapter.*');```
3. Untuk test dasar tanpa database extend `PHPUnit_Framework_TestCase`.
4. Jalankan dengan perintah
`phpunit -c protected/tests/ protected/tests/functional/PagesControllerTest.php`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment