Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/Unit/ChartTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;
use App\Models\Chart;
use App\Models\User;

class ChartTest extends TestCase
{
public function testUser()
{
$chart = factory(Chart::class)->create();
$this->assertInstanceOf(User::class, $chart->user);
}
}
32 changes: 32 additions & 0 deletions tests/Unit/GenerateChartsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Console\Commands\GenerateCharts;
use App\Services\Spotify;
use App\Models\User;
use Mockery;

class GenerateChartsTest extends TestCase
{
use RefreshDatabase;

public function testHandle()
{
$spotifyMock = Mockery::mock(Spotify::class);
$spotifyMock->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);
}
}
109 changes: 109 additions & 0 deletions tests/Unit/HomeControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Http\Controllers\HomeController;
use App\Services\Spotify;
use App\Models\User;
use App\Models\Chart;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class HomeControllerTest extends TestCase
{
use RefreshDatabase;

public function testGetIndex()
{
$spotifyMock = $this->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());
}
}
91 changes: 91 additions & 0 deletions tests/Unit/SpotifyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;
use App\Services\Spotify;
use App\Models\User;
use App\Models\Chart;
use Mockery;
use Carbon\Carbon;
use SpotifyWebAPI\SpotifyWebAPI;

class SpotifyTest extends TestCase
{
protected $spotify;
protected $user;

protected function setUp(): void
{
parent::setUp();

$this->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);
}
}
27 changes: 27 additions & 0 deletions tests/Unit/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Models\User;
use App\Models\Chart;

class UserTest extends TestCase
{
use RefreshDatabase;

/**
* Test the charts method of the User model.
*
* @return void
*/
public function testCharts()
{
$user = factory(User::class)->create();
$chart = factory(Chart::class)->create(['user_id' => $user->id]);

$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $user->charts);
$this->assertTrue($user->charts->contains($chart));
}
}