From 9fb03d8768ed3901792dd65ce5681fb0c10b5e87 Mon Sep 17 00:00:00 2001 From: Hendra Susanto Date: Tue, 14 Jan 2025 14:04:52 +0700 Subject: [PATCH] Add unit tests to this project Add unit tests for various components of the project. * **Spotify Service**: Add `tests/Unit/SpotifyTest.php` to test `generateChart` and `createPlaylist` methods. * **GenerateCharts Command**: Add `tests/Unit/GenerateChartsTest.php` to test the `handle` method. * **HomeController**: Add `tests/Unit/HomeControllerTest.php` to test `getIndex`, `getUserChart`, `getDashboard`, `getRewind2018`, `postRewind2018`, `getRewind`, and `postRewind` methods. * **Chart Model**: Add `tests/Unit/ChartTest.php` to test the `user` method. * **User Model**: Add `tests/Unit/UserTest.php` to test the `charts` method. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/hendrasan/weekly-top20?shareId=XXXX-XXXX-XXXX-XXXX). --- tests/Unit/ChartTest.php | 16 +++++ tests/Unit/GenerateChartsTest.php | 32 +++++++++ tests/Unit/HomeControllerTest.php | 109 ++++++++++++++++++++++++++++++ tests/Unit/SpotifyTest.php | 91 +++++++++++++++++++++++++ tests/Unit/UserTest.php | 27 ++++++++ 5 files changed, 275 insertions(+) create mode 100644 tests/Unit/ChartTest.php create mode 100644 tests/Unit/GenerateChartsTest.php create mode 100644 tests/Unit/HomeControllerTest.php create mode 100644 tests/Unit/SpotifyTest.php create mode 100644 tests/Unit/UserTest.php diff --git a/tests/Unit/ChartTest.php b/tests/Unit/ChartTest.php new file mode 100644 index 0000000..347f923 --- /dev/null +++ b/tests/Unit/ChartTest.php @@ -0,0 +1,16 @@ +create(); + $this->assertInstanceOf(User::class, $chart->user); + } +} diff --git a/tests/Unit/GenerateChartsTest.php b/tests/Unit/GenerateChartsTest.php new file mode 100644 index 0000000..c43a726 --- /dev/null +++ b/tests/Unit/GenerateChartsTest.php @@ -0,0 +1,32 @@ +shouldReceive('generateChart')->andReturn(true); + + $this->app->instance(Spotify::class, $spotifyMock); + + $user = factory(User::class)->create(); + + $command = new GenerateCharts($spotifyMock); + $this->artisan('chart:generate') + ->expectsOutput('Starting chart generation...') + ->expectsOutput('Chart for ' . $user->name . ' generated successfully!') + ->expectsOutput('All charts generated successfully!') + ->assertExitCode(0); + } +} diff --git a/tests/Unit/HomeControllerTest.php b/tests/Unit/HomeControllerTest.php new file mode 100644 index 0000000..57473a8 --- /dev/null +++ b/tests/Unit/HomeControllerTest.php @@ -0,0 +1,109 @@ +createMock(Spotify::class); + $controller = new HomeController(); + $response = $controller->getIndex($spotifyMock); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertViewHas('users'); + } + + public function testGetUserChart() + { + $user = factory(User::class)->create(['spotify_id' => 'testuser']); + $chart = factory(Chart::class)->create(['user_id' => $user->id, 'period' => 1]); + + $controller = new HomeController(); + $response = $controller->getUserChart('testuser'); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertViewHas('user'); + $this->assertViewHas('chart'); + } + + public function testGetDashboard() + { + $user = factory(User::class)->create(); + Auth::login($user); + + $controller = new HomeController(); + $response = $controller->getDashboard(); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertViewHas('user'); + $this->assertViewHas('chart'); + } + + public function testGetRewind2018() + { + $user = factory(User::class)->create(); + Auth::login($user); + + $controller = new HomeController(); + $response = $controller->getRewind2018(); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertViewHas('user'); + $this->assertViewHas('chart'); + } + + public function testPostRewind2018() + { + $user = factory(User::class)->create(); + Auth::login($user); + + $spotifyMock = $this->createMock(Spotify::class); + $spotifyMock->method('createPlaylist')->willReturn(['id' => 'new_playlist_id']); + + $controller = new HomeController(); + $response = $controller->postRewind2018($spotifyMock); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertJson($response->getContent()); + } + + public function testGetRewind() + { + $user = factory(User::class)->create(); + Auth::login($user); + + $controller = new HomeController(); + $response = $controller->getRewind(2018); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertViewHas('user'); + $this->assertViewHas('chart'); + } + + public function testPostRewind() + { + $user = factory(User::class)->create(); + Auth::login($user); + + $spotifyMock = $this->createMock(Spotify::class); + $spotifyMock->method('createPlaylist')->willReturn(['id' => 'new_playlist_id']); + + $controller = new HomeController(); + $response = $controller->postRewind($spotifyMock, 2018); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertJson($response->getContent()); + } +} diff --git a/tests/Unit/SpotifyTest.php b/tests/Unit/SpotifyTest.php new file mode 100644 index 0000000..1ae41c3 --- /dev/null +++ b/tests/Unit/SpotifyTest.php @@ -0,0 +1,91 @@ +spotify = Mockery::mock(SpotifyWebAPI::class); + $this->user = factory(User::class)->create(); + } + + public function testGenerateChart() + { + $spotifyService = new Spotify($this->spotify); + + $this->spotify->shouldReceive('setAccessToken') + ->once() + ->with($this->user->spotify_access_token); + + $this->spotify->shouldReceive('getMyTop') + ->once() + ->with('tracks', ['limit' => 20, 'time_range' => 'short_term']) + ->andReturn((object)[ + 'items' => [ + (object)[ + 'id' => 'track1', + 'name' => 'Track 1', + 'artists' => [(object)['name' => 'Artist 1']] + ], + (object)[ + 'id' => 'track2', + 'name' => 'Track 2', + 'artists' => [(object)['name' => 'Artist 2']] + ] + ] + ]); + + $spotifyService->generateChart($this->user); + + $this->assertDatabaseHas('charts', [ + 'user_id' => $this->user->id, + 'track_spotify_id' => 'track1', + 'track_name' => 'Track 1', + 'track_artist' => 'Artist 1', + 'position' => 1 + ]); + + $this->assertDatabaseHas('charts', [ + 'user_id' => $this->user->id, + 'track_spotify_id' => 'track2', + 'track_name' => 'Track 2', + 'track_artist' => 'Artist 2', + 'position' => 2 + ]); + } + + public function testCreatePlaylist() + { + $spotifyService = new Spotify($this->spotify); + + $this->spotify->shouldReceive('createUserPlaylist') + ->once() + ->with($this->user->spotify_id, ['name' => 'Your Top Songs 2018']) + ->andReturn((object)['id' => 'playlist1']); + + $this->spotify->shouldReceive('addUserPlaylistTracks') + ->once() + ->with($this->user->spotify_id, 'playlist1', ['track1', 'track2']); + + $newPlaylist = $spotifyService->createPlaylist($this->user, [ + 'title' => 'Your Top Songs 2018', + 'tracks' => ['track1', 'track2'] + ]); + + $this->assertEquals('playlist1', $newPlaylist->id); + } +} diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php new file mode 100644 index 0000000..abb6437 --- /dev/null +++ b/tests/Unit/UserTest.php @@ -0,0 +1,27 @@ +create(); + $chart = factory(Chart::class)->create(['user_id' => $user->id]); + + $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $user->charts); + $this->assertTrue($user->charts->contains($chart)); + } +}