Hello,
I'm trying to create a test, that will make sure the database does not accepts duplicate username and email entries. I know the database is working as intended, but I am not having any luck creating the test for this.
NOTE: Stupid code button doesn't work :'(
I'm curious to know how you would test for this.....
This is how my test is written:
/**
* @depends test_create
* @expectedException PHPUnit_Framework_Error
*/
public function test_no_duplicates()
{
$user = Model_User::factory(array(
'username' => 'unittest',
'password' => Auth::instance()->hash_password('123456'),
'email' => 'unit@test.local',
'profile_fields' => '',
'group' => '',
'last_login' => '',
'login_hash' => '',
));
$user->save();
}
And this is the output of my tests:
PHPUnit 3.5.14 by Sebastian Bergmann.
......E.
Time: 1 second, Memory: 12.00Mb
There was 1 error:
1) Test_Users::test_no_duplicates
Fuel\Core\Database_Exception: Duplicate entry 'unittest' for key 'username' [ INSERT INTO `users` (`username`, `password`, `email`, `profile_fields`, `group`, `last_login`, `login_hash`, `created_at`, `updated_at`) VALUES ('unittest', 'sOxROCpnRA81EFue/HpgPpfkxouQMiHi58gCI92yiTY=', 'unit@test.local', '', '', '', '', 1313354096, 1313354096) ]
/Users/bitmasked/Sites/eipfuel/fuel/core/classes/database/mysql/connection.php:203
/Users/bitmasked/Sites/eipfuel/fuel/core/classes/database/query.php:225
/Users/bitmasked/Sites/eipfuel/fuel/packages/orm/classes/query.php:1003
/Users/bitmasked/Sites/eipfuel/fuel/packages/orm/classes/model.php:838
/Users/bitmasked/Sites/eipfuel/fuel/packages/orm/classes/model.php:790
/Users/bitmasked/Sites/eipfuel/fuel/app/tests/users.php:109
FAILURES!
Tests: 8, Assertions: 10, Errors: 1.
Nevermind, the only way I managed to get this working was the following:
/**
* @depends test_create
*/
public function test_no_duplicates()
{
$error = Null;
$user = Model_User::factory(array(
'username' => 'unittest',
'password' => Auth::instance()->hash_password('123456'),
'email' => 'unit@test.local',
'profile_fields' => '',
'group' => '',
'last_login' => '',
'login_hash' => '',
));
// Attempt to save to the database, it should failed.
try {
$user->save();
} catch (Exception $e) {
$error = $e;
}
// If it did not failed, $error is Null.
$this->assertNotNull($error);
}