root/branches/ross-testing/samples/tests/app/modules/Default/functional/LoginTest.php

Revision 2002, 2.7 KB (checked in by ross, 17 months ago)

Added test generation phing commands to build.xml,
Added test code templates in buildtools.
Added some Sample Application Test files - mostly marked incomplete, however check out the Login functional test for an example of how to use AgaviBrowser?
run /agavi/tests/phing - to see what options there are.

More information here: http://trac.agavi.org/wiki/testing#Ross-TestingBranch

Line 
1<?php
2class LoginTest extends AgaviTestCase {
3
4  protected function setUp() 
5  {
6    $this->browser = new AgaviBrowser();
7    $this->browser->initialize('localhost', 'http://localhost');
8  }
9
10  protected function tearDown() 
11  {
12    $this->browser->shutdown();
13     $this->browser = null;
14  }
15
16  public function testCanLogin()
17  {
18    $browser = $this->browser;
19    // Test we can get to the login page
20    $session = $browser->get('/login');
21    $this->assertTrue($session->DomXPath()->evaluate('//html:div[@id="content"]//html:h2="Login"'));
22
23    // Test it logs me in
24    $session =  $browser->post('/login', array('username' => 'Chuck Norris', 'password' => 'kick'));
25    $this->assertTrue($session->getContext()->getUser()->isAuthenticated(), "Hmm wouldn't log me in!");
26    $this->assertTrue($session->DomXPath()->evaluate('//html:div[@id="content"]//html:h2="Login Successful"'));
27
28  }
29
30  public function testCannotLogin()
31  {
32    $browser = $this->browser;
33    // Test we can get to the login page
34    $session = $browser->get('/login');
35    $this->assertTrue($session->DomXPath()->evaluate('//html:div[@id="content"]//html:h2="Login"'));
36    $this->assertFalse($session->getContext()->getUser()->isAuthenticated());
37
38    // Test it logs me in
39    $session =  $browser->click('Login', array('email' => 'BogusUser', 'password' => 'password'));
40    $this->assertFalse($session->getContext()->getUser()->isAuthenticated());
41  }
42
43  public function testLogout()
44  {
45    $browser = $this->browser;
46    $session =  $browser->post('/login', array('username' => 'Chuck Norris', 'password' => 'kick'));
47    $this->assertTrue($session->getContext()->getUser()->isAuthenticated());
48
49    // Lets Logout
50    $session =  $browser->click('Log Out');
51    $this->assertFalse($session->getContext()->getUser()->isAuthenticated());
52    $this->assertTrue($session->DomXPath()->evaluate('//html:div[@id="content"]//html:h2="Logout Successful"'));
53  }
54
55  public function testValidation()
56  {
57    $browser = $this->browser;
58
59    // Invalid User Test
60    $session =  $browser->post('/login', array('username' => 'Sponge Bob', 'password' => 'squarepants'));
61    $this->assertTrue($session->DomXPath()->evaluate('//html:div[@id="content"]//html:p[@class="error"]="Wrong Username"'));
62
63    // Invalid Username Test
64    $session =  $browser->post('/login', array('username' => '', 'password' => 'squarepants'));
65    $this->assertTrue($session->DomXPath()->evaluate('//html:div[@id="content"]//html:p[@class="error"]="The username you supplied is fewer than 5 characters long."'));
66
67    // No Password
68    $session =  $browser->post('/login', array('username' => 'Sponge Bob', 'password' => ''));
69    $this->assertTrue($session->DomXPath()->evaluate('//html:div[@id="content"]//html:p[@class="error"]="Please supply a password."'));
70  }
71}
72?>
Note: See TracBrowser for help on using the browser.