Commit 19c79a1f authored by Carsten  Rose's avatar Carsten Rose
Browse files

Database*.php: added Tests for getEnumSetValueList and getLastInsertId

 *.sql: extend definition to test Set and Enum.
parent 02e87cb5
......@@ -98,7 +98,7 @@ class Database {
* @throws exceptions\UserException if the table or column does not exist, or is not of type ENUM or SET
* @return array
*/
public function getSetEnumValueList($table, $columnName) {
public function getEnumSetValueList($table, $columnName) {
$columnDefinition = $this->getFieldDefinitionFromTable($table, $columnName);
$setEnumDefinition = $columnDefinition["Type"];
......@@ -111,11 +111,11 @@ class Database {
$tokenLength = strpos($setEnumDefinition, "'") + 1;
// count("enum('") == 6, count("')") == 2
$setEnumString = mb_substr($setEnumDefinition, $tokenLength, $len - (2 + $tokenLength));
$enumSetString = mb_substr($setEnumDefinition, $tokenLength, $len - (2 + $tokenLength));
// String: ','red','blue','green
if (($setEnumValueList = explode("','", $setEnumString)) === false) {
if (($setEnumValueList = explode("','", $enumSetString)) === false) {
return array();
}
......
......@@ -3,13 +3,15 @@ CREATE TABLE person (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(128),
firstname VARCHAR(128),
gender VARCHAR(10)
gender ENUM('', 'male', 'female') NOT NULL DEFAULT '',
groups SET('', 'a', 'b', 'c') NOT NULL DEFAULT ''
);
# Fake data
INSERT INTO person (id, name, firstname, gender) VALUES
(NULL, 'Doe', 'John', 'male'),
(NULL, 'Smith', 'Jane', 'female');
#
# Create Demo Form
INSERT INTO person (id, name, firstname, gender, groups) VALUES
(NULL, 'Doe', 'John', 'male', 'c'),
(NULL, 'Smith', 'Jane', 'female', 'a,c');
DROP TABLE IF EXISTS address;
......
......@@ -121,81 +121,35 @@ class DatabaseTest extends AbstractDatabaseTest {
$this->database->querySimple('some garbage');
}
public function testParameters() {
$this->database->execute('SELECT * FROM person WHERE id = :valueOfA', [':valueOfA' => 2]);
$this->assertEquals(1, $this->database->rowCount());
}
public function testGetSetValueList() {
$valueList = $this->database->getEnumSetValueList('person', 'gender');
$expected = [0 => '', 1 => 'male', 2 => 'female'];
$this->assertEquals($expected, $valueList);
$expected = [0 => '', 1 => 'a', 2 => 'b', 3 => 'c'];
$valueList = $this->database->getEnumSetValueList('person', 'groups');
$this->assertEquals($expected, $valueList);
}
public function testGetLastInsertId() {
$sql = "INSERT INTO person (id, name, firstname, gender, groups) VALUES (NULL, 'Doe', 'Jonni', 'male','')";
$this->database->execute($sql);
$this->assertEquals(3, $this->database->getLastInsertId());
}
protected function setUp() {
parent::setUp();
$this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);
}
//
// public function testFetchEntireColumn() {
// $this->database->execute('SELECT * FROM DatabaseClass ORDER BY a', []);
//
// $column2 = $this->database->fetchEntireColumn(1);
//
// $this->assertCount(3, $column2);
//
// $this->assertEquals('abc', $column2[0]);
// $this->assertEquals('def', $column2[1]);
// $this->assertEquals('ghi', $column2[2]);
// }
//
// public function testFetchEntireColumnEmptyResult() {
// $this->database->execute('SELECT * FROM DatabaseClass WHERE a=0 ORDER BY a', []);
//
// $this->assertNull($this->database->fetchEntireColumn(1));
// }
//
// public function testFetchOneRowAssocOnUpdate() {
// $this->database->execute('UPDATE DatabaseClass SET a = :a, b = :b WHERE a=1', [':a' => '1', ':b' => 'abc']);
// $this->assertNull($this->database->fetchOneRowAssoc());
// }
//
// public function testFetchAllRowsAssocOnUpdate() {
// $this->database->execute('UPDATE DatabaseClass SET a = :a, b = :b WHERE a=1', [':a' => '1', ':b' => 'abc']);
// $this->assertNull($this->database->fetchAllRowsAssoc());
// }
//
// public function testFetchEntireColumnOnUpdate() {
// $this->database->execute('UPDATE DatabaseClass SET a = :a, b = :b WHERE a=1', [':a' => '1', ':b' => 'abc']);
// $this->assertNull($this->database->fetchEntireColumn(100));
// }
//
// public function testParameters() {
// $this->database->execute('SELECT * FROM DatabaseClass WHERE a = :valueOfA', [':valueOfA' => 1]);
//
// $this->assertEquals(1, $this->database->rowCount());
// }
//
// public function testIsTableAttribute() {
// $this->assertTrue($this->database->isTableAttribute('DatabaseClass', 'a'));
// $this->assertFalse($this->database->isTableAttribute('DatabaseClass', 'bla'));
// }
//
// public function testIsEnumColumn() {
// $this->assertTrue($this->database->isSetEnumColumn('SetEnumTable', 'color'));
// $this->assertFalse($this->database->isSetEnumColumn('SetEnumTable', 'id'));
// }
//
// /**
// * @expectedException PDOException
// */
// public function testIsEnumColumnInvalidTable() {
// $this->database->isSetEnumColumn('dontexist', 'color');
// }
//
// /**
// * @expectedException \qfq\exceptions\UserException
// */
// public function testIsEnumColumnInvalidColumnName() {
// $this->assertFalse($this->database->isSetEnumColumn('SetEnumTable', 'dontexist'));
// }
//
// public function testGetEnumValueList() {
// $expected = ['', 'red', 'blue', 'green'];
// $actual = $this->database->getSetEnumValueList('SetEnumTable', 'color_empty');
// $this->assertCount(count($expected), $actual);
// $this->assertEquals(implode($expected), implode($actual));
// }
}
......@@ -3,11 +3,12 @@ CREATE TABLE person (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(128),
firstname VARCHAR(128),
gender VARCHAR(10)
gender ENUM('', 'male', 'female') NOT NULL DEFAULT '',
groups SET('', 'a', 'b', 'c') NOT NULL DEFAULT ''
);
#
# Create Demo Form
INSERT INTO person (id, name, firstname, gender) VALUES
(NULL, 'Doe', 'John', 'male'),
(NULL, 'Smith', 'Jane', 'female');
INSERT INTO person (id, name, firstname, gender, groups) VALUES
(NULL, 'Doe', 'John', 'male', 'c'),
(NULL, 'Smith', 'Jane', 'female', 'a,c');
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment