I have read all the related questions and answers here on StackOverflow and I have also read the relevant parts in the phpunit manual, and searched the internet etcetera. I am new to unit testing and would like to understand what goes wrong here. I have 2 classes, one for checking uploaded image files and one for saving sanitized filenames and hashed paths in a simple sqlite db table. I have managed to build working tests for most of the methods in the first class. But in the second class I have been stuck for hours now:
When I run a test on this method:
public function saveToDatabase($filename){
$this->initialize();
$this->upload->add([
'0' => $filename,
'1' => $this->imgpath,
]);
}
It calls the method "add" below, to store filename and path in the sqlite table declared in the constructor. When the test comes to the INSERT part in the SQL statement it fails and declares the error "General error 1: no such table". When I run the classes in its normal environment everything works, so there is nothing wrong with paths etc. It is only when trying to run that specific test case, that I get an error. I should also mention that I have read about setting up a sqlite table in memory using the getConnection and getDataSet methods, but I don't understand how that will help me test my class that saves filenames in my sqlite table. I also understand that my question makes it clear that I am a total noob, but I am totally stuck, so I would be appreciate any suggestions.
/**
* Save to database.
*
*/
private $pdo;
public function __construct(){
//for test purpose, replace with wanted db
$this->pdo = new \PDO("sqlite:test_upload.sqlite");
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING); // Display errors
}
public function add($upload) {
$stmt = $this->pdo->prepare("INSERT INTO test_upload (san_filename, path) VALUES(?, ?)");
$stmt->execute($upload);
}
public function findLast()
{
$id = $this->pdo->lastInsertId();
$stmt = $this->pdo->prepare('SELECT * FROM test_upload WHERE rowid = ?;');
$stmt->execute(array($id));
$lastimg = $stmt->fetchAll(\PDO::FETCH_ASSOC);
return $lastimg;
}
Aucun commentaire:
Enregistrer un commentaire