예제 #1
0
 /**
  * Given an ID, finds a character with that ID
  * 
  * @throws StatusCodeException 404 if it does not find the row
  * 
  * @param DatabaseConnection database class instance
  * @param mixed $id ID number, could be a string or an int
  * @return array row of the database with that ID
  */
 public static function find(DatabaseConnection $db, $id)
 {
     $array = $db->selectRowByID('SELECT * FROM characters WHERE id = ?', $id);
     if ($array === NULL) {
         throw new StatusCodeException(404);
     }
     return $array;
 }
 public function testQuery()
 {
     require __DIR__ . '/config/database_test.php';
     $db = new DatabaseConnection($host, $database, $user, $password);
     $insert_result = $db->query("INSERT INTO characters (id, name, description, type, dead, stage, hp) \r\n\t\t\tVALUES ('9999', 'Carla', 'The swordmaster of Melee Island', 'pirate', 'false',  '4',  '87');");
     $expected = ['id' => '9999', 'name' => 'Carla', 'description' => 'The swordmaster of Melee Island', 'type' => 'pirate', 'dead' => '0', 'stage' => '4', 'hp' => '87'];
     $actual = $db->selectRowByID("SELECT * FROM characters WHERE id = '9999'");
     $delete_result = $db->query("DELETE FROM characters WHERE id = '9999'");
     $this->assertNotEmpty($actual, 'Character was not inserted properly, selectRowByID could not find it. Check TestDatabaseConnection::selectRowByID() and TestDatabaseConnection::query()');
     $this->assertEquals($expected, $actual, 'Function query not returning expected value. Check TestDatabaseConnection::selectRowByID() and TestDatabaseConnection::query()');
     $this->assertTrue($insert_result, 'Insert query failed. Check TestDatabaseConnection::query()');
     $this->assertTrue($delete_result, 'Delete query failed. Check TestDatabaseConnection::query()');
 }