Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
adapted tests to run in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisChaumont committed Apr 2, 2019
1 parent 0984797 commit d15929d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ composer require francoischaumont/db "^3.0"
## Testing
Under the folder named *tests* you will find a SQL file and a test script ready to use.
The SQL file is a dump of a test database which the test script relies on.
Only run in web browser, not CLI.

## Built with
* Visual Studio Code
Expand All @@ -50,5 +49,5 @@ See also the list of [contributors](https://github.com/FrancoisChaumont/db/graph
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.

## Notes
Todo: Add support for more databases (currently only supports MySQL, MariaDB and PostgreSQL)
Todo: Test support to more databases

30 changes: 15 additions & 15 deletions tests/tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$password = 'udbclasspassw';

// instantiation of the class and attempted connection to the database
echo '### Connection to the database: ';
echo "### Connection to the database: ";
$db = new Db($dbms, $dbname, $host, $login, $password, 0, 'utf8mb4', true);

if ($db->isConnected()) {
Expand All @@ -26,29 +26,29 @@
if ($db->isConnected()) {

// select with syntax error
echo '### Select query with syntax error <br>';
echo "### Select query with syntax error\n";
$db->emptyParams();
$selectQuery = "select * from users_error";
$success = $db->select($selectQuery);
if (!$success) { echo 'ERROR: '.$db->getErrMessage().'<br><br>'; }
if (!$success) { echo 'ERROR: '.$db->getErrMessage()."\n\n"; }
else {
while ($row = $db->getNextRow()) {
print_r($row);
}
}

// select with no parameters and order by
echo '### Select query with no parameters and order by <br>';
echo "### Select query with no parameters and order by\n";
$db->emptyParams();
$selectQuery = "select * from users order by id desc";
$db->select($selectQuery);
while ($row = $db->getNextRow()) {
echo 'id: '.$row['id'].' / firstname: '.$row['firstname'].' / lastname: '.$row['lastname'].'<br>';
echo 'id: '.$row['id'].' / firstname: '.$row['firstname'].' / lastname: '.$row['lastname']."\n";
print_r($row);
}

// select with parameters
echo '### Select query with parameters <br>';
echo "### Select query with parameters\n";
$db->emptyParams();
$selectQuery = "select * from users where id = :id or id = :id5";
$db->addParamToBind('id', 1);
Expand All @@ -59,7 +59,7 @@
}

// select with group by
echo '### Select query with group by <br>';
echo "### Select query with group by\n";
$db->emptyParams();
$selectQuery = "select level, count(*) totalPerLevel from users group by level";
$db->select($selectQuery);
Expand All @@ -68,26 +68,26 @@
}

// insert attempt with duplicated primary key
echo '### Insert query with duplicated primary key <br>';
echo "### Insert query with duplicated primary key\n";
$db->emptyParams();
$insertQuery = "insert into users (id, firstname, lastname) values (:id, :firstname, :lastname)";
$db->addParamToBind('id', 1);
$db->addParamToBind('firstname', 'Michael');
$db->addParamToBind('lastname', 'Jackson');
$success = $db->insert($insertQuery);

if (!$success) { echo 'ERROR: '.$db->getErrMessage().'<br><br>'; }
if (!$success) { echo 'ERROR: '.$db->getErrMessage()."\n\n"; }

// insert user Michael Jackson
echo '### Insert query with parameters <br>';
echo "### Insert query with parameters\n";
$db->emptyParams();
$insertQuery = "insert into users (firstname, lastname) values (:firstname, :lastname)";
$db->addParamToBind('firstname', 'Michael');
$db->addParamToBind('lastname', 'Jackson');
$db->insert($insertQuery);

$lastId = $db->getLastId();
echo 'Last inserted id = '.$lastId;
echo "Last inserted id = $lastId\n";
$db->emptyParams();
$selectQuery = "select * from users where id = :id";
$db->addParamToBind('id', $lastId);
Expand All @@ -101,7 +101,7 @@
echo $db->getQueryDump();

// update last inserted record
echo "### Update the last inserted record (id=$lastId) modifying lastname 'Jackson' into 'Jordan' <br>";
echo "### Update the last inserted record (id=$lastId) modifying lastname 'Jackson' into 'Jordan'\n";
$db->emptyParams();
$updateQuery = "update users set lastname = :lastname where id = :id";
$db->addParamToBind('id', $lastId);
Expand All @@ -118,12 +118,12 @@
}

// delete last inserted record
echo "### Delete the last inserted record (id=$lastId) <br>";
echo "### Delete the last inserted record (id=$lastId)\n";
$db->emptyParams();
$deleteQuery = "delete from users where id = :id";
$db->addParamToBind('id', $lastId);
$deleted = $db->delete($deleteQuery);
if ($deleted) { echo "Record deleted!<br>"; }
if ($deleted) { echo "Record deleted!\n"; }

$db->emptyParams();
$selectQuery = "select * from users where id = :id";
Expand All @@ -132,7 +132,7 @@
if ($row = $db->getNextRow()) {
print_r($row);
}
else { echo "Record id $lastId NOT found! <br><br>"; }
else { echo "Record id $lastId NOT found!\n\n"; }
}
}

Expand Down

0 comments on commit d15929d

Please sign in to comment.