I'm trying to write a unittest for a PHP project using PHPUnit. The project is mainly working with an SQL database.
Any function I have uses SQL, and I don't know how to start with writing tests. I have a problem finding what tests to do, since they all work with SQL, and most of them doesn't return anything, but just manipulate the database and session variables.
This is one of the attempts for creating a test:
<?php
require_once '../../test/admin/admin.php';
class checkIsAdminTest extends PHPUnit_Framework_TestCase {
private $ID = '123456789';
private $Class = 0;
private $Pass = 'Aa12345678';
private $First = "Bla";
private $Last = "Bla";
private $Email = "[email protected]";
protected function setUp() {
parent::setUp();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = 'trivia';
$conn = mysqli_connect($servername, $username, $password, $dbname);
$sql = "INSERT INTO `users` (`ID`, `Class`, `Password`, `First`, `Last`, `Email`) VALUES ('" . $this->ID . "', '" . $this->Class . "', '" . sha1($this->Pass) . "', '" . $this->First . "', '" . $this->Last . "', '" . $this->Email . "');";
mysqli_query($conn, $sql);
session_start();
$_SESSION["ID"] = $this->ID;
}
protected function tearDown() {
parent::tearDown();
}
public function __construct() {
}
public function testCheckIsAdmin() {
$this->assertEquals($this->Class == 0 ? False : True, checkIsAdmin());
}
}
?>
The function checks if the vale of the field "Class" of a record in a Table named "Users" is 0 or 1, returns "true" if it's 1 and false otherwise.