koel/tests/Feature/TestCase.php

100 lines
2.3 KiB
PHP
Raw Normal View History

2017-02-14 06:53:02 +00:00
<?php
2017-08-05 16:56:11 +00:00
namespace Tests\Feature;
2017-02-14 06:53:02 +00:00
use App\Models\Album;
use App\Models\Artist;
use App\Models\Song;
use App\Models\User;
2018-08-19 09:05:33 +00:00
use Exception;
2017-02-14 06:53:02 +00:00
use JWTAuth;
2017-08-05 16:56:11 +00:00
use Laravel\BrowserKitTesting\DatabaseTransactions;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
use Mockery;
2017-08-05 16:56:11 +00:00
use Tests\CreatesApplication;
2018-08-19 09:05:33 +00:00
use Tests\Traits\InteractsWithIoc;
2017-02-14 06:53:02 +00:00
2017-08-05 16:56:11 +00:00
abstract class TestCase extends BaseTestCase
2017-02-14 06:53:02 +00:00
{
2018-08-19 09:05:33 +00:00
use CreatesApplication, DatabaseTransactions, InteractsWithIoc;
2017-02-14 06:53:02 +00:00
public function setUp()
{
parent::setUp();
$this->prepareForTests();
}
2017-08-05 18:55:53 +00:00
/**
2017-02-14 06:53:02 +00:00
* Create a sample media set, with a complete artist+album+song trio.
2018-08-19 09:06:59 +00:00
*
2018-08-19 09:05:33 +00:00
* @throws Exception
2017-02-14 06:53:02 +00:00
*/
protected function createSampleMediaSet()
{
$artist = factory(Artist::class)->create();
// Sample 3 albums
$albums = factory(Album::class, 3)->create([
'artist_id' => $artist->id,
]);
// 7-15 songs per albums
foreach ($albums as $album) {
factory(Song::class, random_int(7, 15))->create([
'album_id' => $album->id,
'artist_id' => $artist->id,
2017-02-14 06:53:02 +00:00
]);
}
}
protected function getAsUser($url, $user = null)
{
if (!$user) {
$user = factory(User::class)->create();
}
return $this->get($url, [
'Authorization' => 'Bearer '.JWTAuth::fromUser($user),
]);
}
protected function deleteAsUser($url, $data = [], $user = null)
{
if (!$user) {
$user = factory(User::class)->create();
}
return $this->delete($url, $data, [
'Authorization' => 'Bearer '.JWTAuth::fromUser($user),
]);
}
protected function postAsUser($url, $data, $user = null)
{
if (!$user) {
$user = factory(User::class)->create();
}
return $this->post($url, $data, [
'Authorization' => 'Bearer '.JWTAuth::fromUser($user),
]);
}
protected function putAsUser($url, $data, $user = null)
{
if (!$user) {
$user = factory(User::class)->create();
}
return $this->put($url, $data, [
'Authorization' => 'Bearer '.JWTAuth::fromUser($user),
]);
}
protected function tearDown()
{
Mockery::close();
parent::tearDown();
}
2017-02-14 06:53:02 +00:00
}