Created
June 14, 2012 03:55
-
-
Save mikemill/2927915 to your computer and use it in GitHub Desktop.
Initial attempt at testing the user class. Still need to test user settings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace JawHare; | |
class UserTest extends \PHPUnit_Framework_TestCase | |
{ | |
protected $user; | |
protected $user_data = array( | |
'username' => 'PHPUnitTest', | |
'fullname' => 'PHP Unit Testing', | |
'email' => 'phpunit@jawhare.org', | |
'salt' => 'NaCl', | |
'admin' => true, | |
'password' => 'BillyJoeBob', | |
); | |
protected $user_settings = array( | |
'Foo' => 'bar', | |
'test' => 'nope', | |
'why?' => 'because' | |
); | |
static public function setUpBeforeClass() | |
{ | |
global $ini; | |
// User object needs the database | |
$db = Database($ini['database']); | |
// Clear out the user table | |
$db->query('DELETE FROM users'); | |
} | |
public function setUp() | |
{ | |
$this->user = new User(); | |
} | |
public function testSetterGetter() | |
{ | |
$user = $this->user; | |
$user->username($this->user_data['username']) | |
->fullname($this->user_data['fullname']) | |
->email($this->user_data['email']) | |
->salt($this->user_data['salt']) | |
->admin($this->user_data['username']) | |
->passwd($this->user_data['password']); | |
foreach ($this->user_settings AS $var => $val) | |
$user->settings($var, $val); | |
$this->assertEquals($user->username(), $this->user_data['username']); | |
$this->assertEquals($user->fullname(), $this->user_data['fullname']); | |
$this->assertEquals($user->email(), $this->user_data['email']); | |
$this->assertEquals($user->salt(), $this->user_data['salt']); | |
$this->assertEquals($user->admin(), $this->user_data['admin']); | |
$this->assertTrue($user->validatepw($this->user_data['password'])); | |
foreach ($this->user_settings AS $var => $val) | |
$this->assertEquals($user->settings($var), $val, $var); | |
} | |
/** | |
* @depends testSetterGetter | |
*/ | |
public function testSave() | |
{ | |
$user = $this->user; | |
$user->username($this->user_data['username']) | |
->fullname($this->user_data['fullname']) | |
->email($this->user_data['email']) | |
->salt($this->user_data['salt']) | |
->admin($this->user_data['username']) | |
->passwd($this->user_data['password']); | |
foreach ($this->user_settings AS $var => $val) | |
$user->settings($var, $val); | |
$user->save(); | |
$this->assertNotEquals($user->id(), 0); | |
$this->assertNotNull($user->id()); | |
} | |
/** | |
* @depends testSave | |
*/ | |
public function testLoad() | |
{ | |
$user = $this->user->load($this->user_data['username']); | |
$this->assertNotEquals($user->id(), 0); | |
$this->assertNotNull($user->id()); | |
foreach($this->user_data AS $variable => $value) | |
{ | |
if ($variable == 'password') | |
continue; | |
$this->assertEquals($user->$variable(), $value, $variable . ', User: ' . $user->$variable() . ', Value: ' . $value); | |
} | |
$this->assertTrue($user->validatepw($this->user_data['password'])); | |
} | |
/** | |
* @depends testLoad | |
*/ | |
public function testUpdate() | |
{ | |
$user = $this->user->load($this->user_data['username']); | |
$user->salt('Pringles')->settings('testing', 'food')->save(); | |
$user2 = new User($user->id()); | |
foreach($this->user_data AS $variable => $value) | |
{ | |
$this->assertEquals($user->$variable(), $user2->$variable(), $variable . ', User: ' . $user->$variable() . ', User2: ' . $user2->$variable()); | |
} | |
foreach($user->settings() AS $variable => $value) | |
{ | |
$this->assertEquals($value, $user2->settings($variable), $variable . ', User: ' . $value . ', User2: ' . $user2->settings($variable)); | |
} | |
} | |
} |
Better? I like the use of @depends
as there is no point in doing load if save fails or save if getting/setting fails.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TBH, tests shouldn't depend on one another, as annoying as it may seem - after all if whatever init works, it will just work the next time
(for reliability and maintainbility; and after all they're unit test - tests of a single unit of work, which can be tested and should be, in isolation as much as possible)