Skip to content

Instantly share code, notes, and snippets.

@list
Created October 21, 2011 02:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save list/1302973 to your computer and use it in GitHub Desktop.
Save list/1302973 to your computer and use it in GitHub Desktop.
Functional Testing in Yii using Goutte
<?php
use Goutte\Client;
class SiteTest extends CTestCase {
protected $client;
public function setUp() {
parent::setUp();
$this->client = new Client();
}
public function open($route, $params=array()) {
$url = explode('phpunit', Yii::app()->createUrl($route, $params));
return TEST_BASE_URL.$url[1];
}
public function testIndex() {
$crawler = $this->client->request('GET', $this->open('site/index'));
$this->assertEquals('My Web Application', $crawler->filter('title')->text());
}
public function testContact() {
$crawler = $this->client->request('GET', $this->open('site/contact'));
$this->assertEquals('My Web Application - Contact Us', $crawler->filter('title')->text());
$form = $crawler->filter('input[type=submit]')->form();
$this->assertEquals('POST', strtoupper($form->getMethod()));
$this->assertTrue($form->has('ContactForm[name]'));
// set some values
$form['ContactForm[name]'] = 'tester';
$form['ContactForm[email]'] = 'tester@example.com';
$form['ContactForm[subject]'] = 'test subject';
// submit the form
$crawler = $this->client->submit($form);
$this->assertEquals('Body cannot be blank.', $crawler->filter('#ContactForm_body_em_')->text());
}
public function testLoginLogout() {
$crawler = $this->client->request('GET', $this->open('site/index'));
// ensure the user is logged out
$this->assertNotRegExp('/Logout/', $this->client->getResponse()->getContent());
// test login process, including validation
$crawler = $this->client->click($crawler->selectLink('Login')->link());
$form = $crawler->filter('input[type=submit]')->form();
$this->assertTrue($form->has('LoginForm[username]'));
$crawler = $this->client->submit($form);
$this->assertRegExp('/Username cannot be blank./', $this->client->getResponse()->getContent());
$this->assertRegExp('/Password cannot be blank./', $this->client->getResponse()->getContent());
$form['LoginForm[username]'] = 'demo';
$form['LoginForm[password]'] = 'demo';
$crawler = $this->client->submit($form);
$this->assertNotRegExp('/Password cannot be blank./', $this->client->getResponse()->getContent());
$this->assertRegExp('/Logout/', $this->client->getResponse()->getContent());
// test logout process
$this->assertNotRegExp('/Login/', $this->client->getResponse()->getContent());
$crawler = $this->client->click($crawler->selectLink('Logout (demo)')->link());
$this->assertRegExp('/Login/', $this->client->getResponse()->getContent());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment