Skip to content

Commit

Permalink
Updated Backend Test (issue #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
atlasharry committed Dec 6, 2023
1 parent 10bac36 commit 1fc41b5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
5 changes: 2 additions & 3 deletions backend/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function login()

$db->closeConnection();
}

public function register()
{
$input = file_get_contents('php://input');
Expand Down Expand Up @@ -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'];
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions backend/Model/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
47 changes: 24 additions & 23 deletions backend/tests/StackTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

use PHPUnit\Framework\TestCase;


Expand All @@ -7,37 +8,41 @@ class StackTest extends TestCase
// For testing the API
protected $client;

protected function setUp() : void{
protected function setUp(): void
{
parent::setUp();
$this->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',
Expand All @@ -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',
Expand All @@ -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());
}

}


?>



0 comments on commit 1fc41b5

Please sign in to comment.