From 1fc41b55f0fcca42db9564a2f766e9ef6450d12f Mon Sep 17 00:00:00 2001 From: Harry Yu Date: Wed, 6 Dec 2023 00:10:43 -0500 Subject: [PATCH] Updated Backend Test (issue #3) --- backend/Controller/UserController.php | 5 ++- backend/Model/UserModel.php | 6 ++-- backend/tests/StackTest.php | 47 ++++++++++++++------------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/backend/Controller/UserController.php b/backend/Controller/UserController.php index 4133b9f..26d707a 100644 --- a/backend/Controller/UserController.php +++ b/backend/Controller/UserController.php @@ -27,7 +27,7 @@ public function login() $db->closeConnection(); } - + public function register() { $input = file_get_contents('php://input'); @@ -125,7 +125,6 @@ public function createSong() { $input = file_get_contents('php://input'); $data = json_decode($input, true); - $song_id = $data["id"]; $user_name = $data["username"]; $song_artist = $data['song_artist']; $song_name = $data['song_name']; @@ -135,7 +134,7 @@ public function createSong() $userModel = new UserModel($db); if ($userModel->checkDuplicate($song_name, $user_name, $song_artist)) { - $userModel->createSong($id, $song_name, $user_name, $song_artist, $song_rating); + $userModel->createSong($song_name, $user_name, $song_artist, $song_rating); $this->jsonResponse(['success' => true], 201); } else { $this->jsonResponse(['success' => false, 'message' => 'song already exist'], 401); diff --git a/backend/Model/UserModel.php b/backend/Model/UserModel.php index 0c64aa7..014933f 100644 --- a/backend/Model/UserModel.php +++ b/backend/Model/UserModel.php @@ -134,10 +134,10 @@ public function checkDuplicate($song_name, $user_name, $song_artist) $query->close(); } - public function createSong($song_id, $song_name, $user_name, $song_artist, $song_rating) + public function createSong($song_name, $user_name, $song_artist, $song_rating) { - $query = $this->db->getConnection()->prepare("INSERT INTO ratings (`id`, `username`, `artist`, `song`, `rating`) VALUES (?, ?, ?, ?, ?)"); - $query->bind_param('isssi', $song_id, $user_name, $song_artist, $song_name, $song_rating); + $query = $this->db->getConnection()->prepare("INSERT INTO ratings (`username`, `artist`, `song`, `rating`) VALUES (?, ?, ?, ?, ?)"); + $query->bind_param('sssi', $user_name, $song_artist, $song_name, $song_rating); $query->execute(); $query->close(); } diff --git a/backend/tests/StackTest.php b/backend/tests/StackTest.php index d475ec7..2cb243d 100644 --- a/backend/tests/StackTest.php +++ b/backend/tests/StackTest.php @@ -1,4 +1,5 @@ client = new GuzzleHttp\Client(["base_uri" => "http://localhost/YY_Music_JS/backend/index.php"]); + $this->client = new GuzzleHttp\Client(["base_uri" => "http://localhost/JS_BackUp/backend/index.php"]); } // register - public function testRegister(): void{ + public function testRegister(): void + { $data = [ 'json' => [ 'username' => 'test_admin', 'password' => '1234567890aaa', ] - ]; + ]; $response = $this->client->request('POST', '?action=register', $data); $this->assertEquals(201, $response->getStatusCode()); } // login: - public function testLogin(): void{ + public function testLogin(): void + { $data = [ 'json' => [ 'username' => 'test_admin', 'password' => '1234567890aaa', ] - ]; + ]; $response = $this->client->request('POST', '?action=login', $data); $this->assertEquals(200, $response->getStatusCode()); } // fail login - public function testFailLogin(): void{ + public function testFailLogin(): void + { $data = [ 'json' => [ 'username' => 'test_admin', @@ -50,10 +55,10 @@ public function testFailLogin(): void{ } // create song - public function testCreateSong(): void{ + public function testCreateSong(): void + { $data = [ 'json' => [ - 'id' => '1', 'username' => 'test_admin', 'song_artist' => 'test_admin', 'song_name' => 'admin_song', @@ -65,42 +70,38 @@ public function testCreateSong(): void{ } // get ratings - public function testGetRatings(): void{ + public function testGetRatings(): void + { $response = $this->client->request('GET', '?action=getRatings'); $this->assertEquals(200, $response->getStatusCode()); } // update song - public function testUpdateSong(): void{ + public function testUpdateSong(): void + { $data = [ 'json' => [ - 'id' => '23', + 'id' => '53', 'username' => 'test_admin', 'song_artist' => 'test_admin', 'song_name' => 'admin_song', 'song_rating' => '4', ] - ]; + ]; $response = $this->client->request('POST', '?action=updateSong', $data); $this->assertEquals(200, $response->getStatusCode()); } // delete song - public function testDeleteSong(): void{ + public function testDeleteSong(): void + { $data = [ 'json' => [ - 'id' => '23', + 'id' => '53', 'username' => 'test_admin', ] - ]; + ]; $response = $this->client->request('POST', '?action=deleteSong', $data); $this->assertEquals(200, $response->getStatusCode()); } - } - - -?> - - -