/** * @depends testExecute */ public function testDrop() { // table 'test' occures in the list of tables $this->assertEquals(true, in_array('test', $this->db->getTables())); // drop test $this->db->drop('test'); // table 'test' no longer exists in the list of tables $this->assertEquals(false, in_array('test', $this->db->getTables())); }
/** * @throws SpoonDatabaseException */ public function testExecute() { // clear all tables if (count($this->db->getTables()) != 0) { $this->db->drop($this->db->getTables()); } // create table users $this->db->execute("\n\t\t\tCREATE TABLE users (\n\t\t\tid INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t\t\tusername VARCHAR( 255 ) NOT NULL ,\n\t\t\temail VARCHAR( 255 ) NOT NULL ,\n\t\t\tdeveloper ENUM( 'Y', 'N' ) NOT NULL\n\t\t\t) ENGINE = MYISAM;"); }
/** * Validate if a database connection can be made * * @param InstallationData $data The form data * @param ExecutionContextInterface $context The forms validation context * * @todo Replace SpoonDatabase */ public function checkDatabaseConnection(InstallationData $data, ExecutionContextInterface $context) { try { // create instance $db = new \SpoonDatabase('mysql', $data->getDbHostname(), $data->getDbUsername(), $data->getDbPassword(), $data->getDbDatabase(), $data->getDbPort()); // test table $table = 'test' . time(); // attempt to create table $db->execute('DROP TABLE IF EXISTS ' . $table); $db->execute('CREATE TABLE ' . $table . ' (id int(11) NOT NULL) ENGINE=MyISAM'); // drop table $db->drop($table); } catch (\Exception $e) { $context->addViolation('Problem with database credentials'); } }
/** * Validate the form based on the variables in $_POST */ private function validateForm() { // form submitted if ($this->frm->isSubmitted()) { // database settings $this->frm->getField('hostname')->isFilled('This field is required.'); $this->frm->getField('database')->isFilled('This field is required.'); $this->frm->getField('username')->isFilled('This field is required.'); $this->frm->getField('password')->isFilled('This field is required.'); // all filled out if ($this->frm->getField('hostname')->isFilled() && $this->frm->getField('database')->isFilled() && $this->frm->getField('username')->isFilled() && $this->frm->getField('password')->isFilled()) { // test the database connection details try { // get port $port = $this->frm->getField('port')->isFilled() ? $this->frm->getField('port')->getValue() : 3306; // create instance $db = new SpoonDatabase('mysql', $this->frm->getField('hostname')->getValue(), $this->frm->getField('username')->getValue(), $this->frm->getField('password')->getValue(), $this->frm->getField('database')->getValue(), $port); // test table $table = 'test' . time(); // attempt to create table $db->execute('DROP TABLE IF EXISTS ' . $table); $db->execute('CREATE TABLE ' . $table . ' (id int(11) NOT NULL) ENGINE=MyISAM'); // drop table $db->drop($table); } catch (Exception $e) { // add errors $this->frm->addError('Problem with database credentials'); // show error $this->tpl->assign('formError', $e->getMessage()); } // all valid if ($this->frm->isCorrect()) { // update session SpoonSession::set('db_hostname', $this->frm->getField('hostname')->getValue()); SpoonSession::set('db_database', $this->frm->getField('database')->getValue()); SpoonSession::set('db_username', $this->frm->getField('username')->getValue()); SpoonSession::set('db_password', $this->frm->getField('password')->getValue()); SpoonSession::set('db_port', $this->frm->getField('port')->getValue()); // redirect SpoonHTTP::redirect('index.php?step=6'); } } } }
/** * Fully empties the test database * * @param \SpoonDatabase $database */ protected function emptyTestDatabase($database) { foreach ($database->getTables() as $table) { $database->drop($table); } }