koel/tests/Feature/TestCase.php

106 lines
2.9 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;
2018-08-24 15:27:19 +00:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2020-09-06 18:21:39 +00:00
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Testing\TestResponse;
use Mockery;
use ReflectionClass;
2020-06-13 12:19:24 +00:00
use Tests\Traits\CreatesApplication;
2018-08-19 09:05:33 +00:00
use Tests\Traits\InteractsWithIoc;
2020-06-13 12:19:24 +00:00
use Tests\Traits\SandboxesTests;
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
{
use CreatesApplication;
use DatabaseTransactions;
use InteractsWithIoc;
2020-06-13 12:19:24 +00:00
use SandboxesTests;
2019-07-22 07:03:23 +00:00
public function setUp(): void
2017-02-14 06:53:02 +00:00
{
parent::setUp();
2018-08-31 13:47:15 +00:00
2017-02-14 06:53:02 +00:00
$this->prepareForTests();
2020-06-13 12:19:24 +00:00
self::createSandbox();
2017-02-14 06:53:02 +00:00
}
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 static function createSampleMediaSet(): void
2017-02-14 06:53:02 +00:00
{
/** @var Artist $artist */
2017-02-14 06:53:02 +00:00
$artist = factory(Artist::class)->create();
// Sample 3 albums
/** @var Album[] $albums */
2017-02-14 06:53:02 +00:00
$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
]);
}
}
2020-09-06 18:21:39 +00:00
private function jsonAsUser(?User $user, string $method, $uri, array $data = [], array $headers = []): TestResponse
2017-02-14 06:53:02 +00:00
{
2020-09-06 18:21:39 +00:00
$user = $user ?: factory(User::class)->create();
$headers['X-Requested-With'] = 'XMLHttpRequest';
$headers['Authorization'] = 'Bearer '.$user->createToken('koel')->plainTextToken;
return parent::json($method, $uri, $data, $headers);
2017-02-14 06:53:02 +00:00
}
2020-09-06 18:21:39 +00:00
protected function getAsUser(string $url, ?User $user = null): TestResponse
2017-02-14 06:53:02 +00:00
{
2020-09-06 18:21:39 +00:00
return $this->jsonAsUser($user, 'get', $url);
2017-02-14 06:53:02 +00:00
}
2020-09-06 18:21:39 +00:00
protected function deleteAsUser(string $url, array $data = [], ?User $user = null): TestResponse
2017-02-14 06:53:02 +00:00
{
2020-09-06 18:21:39 +00:00
return $this->jsonAsUser($user, 'delete', $url, $data);
2017-02-14 06:53:02 +00:00
}
2020-09-06 18:21:39 +00:00
protected function postAsUser(string $url, array $data, ?User $user = null): TestResponse
2017-02-14 06:53:02 +00:00
{
2020-09-06 18:21:39 +00:00
return $this->jsonAsUser($user, 'post', $url, $data);
2017-02-14 06:53:02 +00:00
}
2020-09-06 18:21:39 +00:00
protected function putAsUser(string $url, array $data, ?User $user = null): TestResponse
2018-08-31 13:47:15 +00:00
{
2020-09-06 18:21:39 +00:00
return $this->jsonAsUser($user, 'put', $url, $data);
2018-08-31 13:47:15 +00:00
}
protected static function getNonPublicProperty($object, string $property)
{
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($property);
$property->setAccessible(true);
return $property->getValue($object);
}
2019-07-22 07:03:23 +00:00
protected function tearDown(): void
{
$this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount());
Mockery::close();
2020-06-13 12:19:24 +00:00
self::destroySandbox();
2020-09-06 18:21:39 +00:00
parent::tearDown();
}
2017-02-14 06:53:02 +00:00
}