chore: clean up TestCase classes

This commit is contained in:
Phan An 2020-09-09 18:18:53 +02:00
parent 448d33c2c3
commit d6eeb8a2e0
2 changed files with 41 additions and 70 deletions

View file

@ -2,60 +2,12 @@
namespace Tests\Feature;
use App\Models\Album;
use App\Models\Artist;
use App\Models\Song;
use App\Models\User;
use Exception;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Testing\TestResponse;
use Mockery;
use ReflectionClass;
use Tests\Traits\CreatesApplication;
use Tests\Traits\InteractsWithIoc;
use Tests\Traits\SandboxesTests;
use Tests\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use DatabaseTransactions;
use InteractsWithIoc;
use SandboxesTests;
public function setUp(): void
{
parent::setUp();
$this->prepareForTests();
self::createSandbox();
}
/**
* Create a sample media set, with a complete artist+album+song trio.
*
* @throws Exception
*/
protected static function createSampleMediaSet(): void
{
/** @var Artist $artist */
$artist = factory(Artist::class)->create();
// Sample 3 albums
/** @var Album[] $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,
]);
}
}
private function jsonAsUser(?User $user, string $method, $uri, array $data = [], array $headers = []): TestResponse
{
$user = $user ?: factory(User::class)->create();
@ -84,22 +36,4 @@ abstract class TestCase extends BaseTestCase
{
return $this->jsonAsUser($user, 'put', $url, $data);
}
protected static function getNonPublicProperty($object, string $property)
{
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($property);
$property->setAccessible(true);
return $property->getValue($object);
}
protected function tearDown(): void
{
$this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount());
Mockery::close();
self::destroySandbox();
parent::tearDown();
}
}

View file

@ -2,9 +2,13 @@
namespace Tests;
use App\Models\Album;
use App\Models\Artist;
use App\Models\Song;
use Exception;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Mockery;
use ReflectionClass;
use Tests\Traits\CreatesApplication;
use Tests\Traits\InteractsWithIoc;
use Tests\Traits\SandboxesTests;
@ -19,15 +23,48 @@ abstract class TestCase extends BaseTestCase
public function setUp(): void
{
parent::setUp();
$this->prepareForTests();
self::createSandbox();
}
protected function tearDown(): void
{
$this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount());
Mockery::close();
self::destroySandbox();
parent::tearDown();
}
/**
* Create a sample media set, with a complete artist+album+song trio.
*
* @throws Exception
*/
protected static function createSampleMediaSet(): void
{
/** @var Artist $artist */
$artist = factory(Artist::class)->create();
// Sample 3 albums
/** @var Album[] $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,
]);
}
}
protected static function getNonPublicProperty($object, string $property)
{
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($property);
$property->setAccessible(true);
return $property->getValue($object);
}
}