128 lines
3.0 KiB
PHP
128 lines
3.0 KiB
PHP
<?php
|
|
|
|
require_once 'Database.php';
|
|
|
|
// Remove the test.db file if it exists
|
|
if (file_exists('test.db')) {
|
|
unlink('test.db');
|
|
neutral('Database removed.');
|
|
}
|
|
|
|
// Open the database
|
|
$db = new Database('test.db');
|
|
|
|
// Make sure the database exists
|
|
if (!file_exists('test.db')) {
|
|
exit(bad('Failed to open database.'));
|
|
} else {
|
|
good('Database opened successfully.');
|
|
}
|
|
|
|
// Create a table
|
|
$db->table('test')->create([
|
|
'id INTEGER PRIMARY KEY',
|
|
'name TEXT NOT NULL',
|
|
'age INTEGER DEFAULT 18'
|
|
]);
|
|
|
|
// Make sure the table exists
|
|
if (!$db->tableExists('test')) {
|
|
exit(bad('Failed to create table.'));
|
|
} else {
|
|
good('Table created successfully.');
|
|
}
|
|
|
|
// Insert some data
|
|
$db->table('test')->insert([
|
|
'name' => 'John',
|
|
'age' => 32
|
|
]);
|
|
$db->table('test')->insert([
|
|
['name' => 'Smith', 'age' => 22],
|
|
['name' => 'Jane', 'age' => 18]
|
|
]);
|
|
|
|
// Make sure all three rows exist.
|
|
if ($db->table('test')->count() !== 3) {
|
|
exit(bad('Failed to insert data.'));
|
|
} else {
|
|
good('Data inserted successfully.');
|
|
}
|
|
|
|
// Make sure we only count two rows above age 18.
|
|
if ($db->table('test')->where('age', '>', 18)->count() !== 2) {
|
|
exit(bad('Failed to count data.'));
|
|
} else {
|
|
good('Data counted successfully.');
|
|
}
|
|
|
|
// Delete John from the table.
|
|
$db->table('test')->where('name', '=', 'John')->delete();
|
|
|
|
// Make sure John does not exist, and Jane and Smith still exist.
|
|
if (
|
|
$db->table('test')->where('name', '=', 'John')->select() == false
|
|
&& $db->table('test')->where('name', '=', 'Jane')->select() != false
|
|
&& $db->table('test')->where('name', '=', 'Smith')->select() != false
|
|
) {
|
|
exit(bad('Failed to delete data.'));
|
|
} else {
|
|
good('Data deleted successfully.');
|
|
}
|
|
|
|
// Make sure that Jane's age is 18.
|
|
if ($db->table('test')->where('name', '=', 'Jane')->select()['age'] !== 18) {
|
|
exit(bad('Jane is not 18.'));
|
|
} else {
|
|
good('Jane is 18.');
|
|
}
|
|
|
|
// Update Jane's age to 20.
|
|
$db->table('test')->where('name', '=', 'Jane')->update(['age' => 20]);
|
|
|
|
// Make sure that Jane's age is 20.
|
|
if ($db->table('test')->where('name', '=', 'Jane')->select()['age'] !== 20) {
|
|
exit(bad('Failed to update Jane\'s age.'));
|
|
} else {
|
|
good('Jane\'s age updated successfully.');
|
|
}
|
|
|
|
// Drop the table.
|
|
$db->table('test')->drop();
|
|
|
|
// Make sure the table does not exist.
|
|
if ($db->tableExists('test')) {
|
|
exit(bad('Failed to drop table.'));
|
|
} else {
|
|
good('Table dropped successfully.');
|
|
}
|
|
|
|
// Delete the test database.
|
|
if (file_exists('test.db')) {
|
|
unlink('test.db');
|
|
file_exists('test.db-shm') && unlink('test.db-shm');
|
|
file_exists('test.db-wal') && unlink('test.db-wal');
|
|
|
|
neutral('Database removed.');
|
|
} else {
|
|
neutral('Database not found.');
|
|
}
|
|
|
|
// ----------------------------------------------------------------
|
|
// ----------------------------------------------------------------
|
|
// Helpers
|
|
|
|
function neutral(string $message)
|
|
{
|
|
echo "⚪ $message" . PHP_EOL;
|
|
}
|
|
|
|
function good(string $message)
|
|
{
|
|
echo "🟢 $message" . PHP_EOL;
|
|
}
|
|
|
|
function bad(string $message)
|
|
{
|
|
echo "🔴 $message" . PHP_EOL;
|
|
} |