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;
|
2018-08-24 15:27:19 +00:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2017-08-05 16:56:11 +00:00
|
|
|
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
|
2018-08-19 11:08:16 +00:00
|
|
|
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;
|
2018-08-31 13:47:15 +00:00
|
|
|
use Tymon\JWTAuth\JWTAuth;
|
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
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
/** @var JWTAuth */
|
|
|
|
private $auth;
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2018-08-31 13:47:15 +00:00
|
|
|
|
|
|
|
$this->auth = app(JWTAuth::class);
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
$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
|
|
|
*/
|
2018-08-31 13:47:15 +00:00
|
|
|
protected function createSampleMediaSet(): void
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
|
|
|
$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,
|
2017-04-29 03:49:14 +00:00
|
|
|
'artist_id' => $artist->id,
|
2017-02-14 06:53:02 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
protected function getAsUser($url, $user = null): self
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
|
|
|
return $this->get($url, [
|
2018-08-31 13:47:45 +00:00
|
|
|
'Authorization' => 'Bearer '.$this->generateJwtToken($user),
|
2017-02-14 06:53:02 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
protected function deleteAsUser($url, $data = [], $user = null): self
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
|
|
|
return $this->delete($url, $data, [
|
2018-08-31 13:47:45 +00:00
|
|
|
'Authorization' => 'Bearer '.$this->generateJwtToken($user),
|
2017-02-14 06:53:02 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
protected function postAsUser($url, $data, $user = null): self
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
|
|
|
return $this->post($url, $data, [
|
2018-08-31 13:47:45 +00:00
|
|
|
'Authorization' => 'Bearer '.$this->generateJwtToken($user),
|
2017-02-14 06:53:02 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
protected function putAsUser($url, $data, $user = null): self
|
2017-02-14 06:53:02 +00:00
|
|
|
{
|
|
|
|
return $this->put($url, $data, [
|
2018-08-31 13:47:45 +00:00
|
|
|
'Authorization' => 'Bearer '.$this->generateJwtToken($user),
|
2017-02-14 06:53:02 +00:00
|
|
|
]);
|
|
|
|
}
|
2018-08-19 11:08:16 +00:00
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
private function generateJwtToken(?User $user): string
|
|
|
|
{
|
|
|
|
return $this->auth->fromUser($user ?: factory(User::class)->create());
|
|
|
|
}
|
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
protected function tearDown()
|
|
|
|
{
|
2018-09-03 12:41:49 +00:00
|
|
|
$this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount());
|
2018-08-22 19:19:30 +00:00
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
Mockery::close();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
2017-02-14 06:53:02 +00:00
|
|
|
}
|