Skip to content

Instantly share code, notes, and snippets.

@luelista
Created February 8, 2016 09:53
Show Gist options
  • Save luelista/b52db38824d29f9937b2 to your computer and use it in GitHub Desktop.
Save luelista/b52db38824d29f9937b2 to your computer and use it in GitHub Desktop.
<?php
$f3 = require('lib/base.php');
$f3->set('DEBUG',5);
$test=new Test();
$dbpath = __DIR__ . '/_test.sqlite';
echo "Using test db: $dbpath\n";
$db=new DB\SQL('sqlite:'.$dbpath);
$db->exec('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT,username,pw)');
$user_mapper = new DB\SQL\Mapper($db, 'users');
$user_mapper->username = 'theuser';
$user_mapper->pw = password_hash('thepassword', PASSWORD_DEFAULT);
$test->expect($user_mapper->save() > 0, 'Test user created successfully');
$user_mapper2 = new DB\SQL\Mapper($db, 'users');
$auth = new \Auth($user_mapper2, [ 'id' => 'username', 'pw' => 'pw', 'pwhash' => TRUE ]);
$test->expect($auth->login('theuser', 'thepassword'), 'Correct login was accepted');
$test->expect(!$auth->login('wronguser', 'thepassword'), 'Wrong username was rejected');
$test->expect(!$auth->login('theuser', 'thewrongpassword'), 'Wrong password was rejected');
// Display the results; not MVC but let's keep it simple
foreach ($test->results() as $result) {
echo str_pad($result['text'],55)." ";
if ($result['status'])
echo 'Pass';
else
echo 'Fail ('.$result['source'].')';
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment