Shift to class based factories

This commit is contained in:
Laravel Shift 2021-02-14 19:11:17 +00:00
parent 75cf92eeaf
commit 635b39d109
12 changed files with 293 additions and 107 deletions

View file

@ -20,6 +20,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Contracts\Commentable;
use App\Contracts\Favouritable;
use App\Contracts\Searchable;
@ -89,6 +90,8 @@ use Venturecraft\Revisionable\RevisionableTrait;
*/
class Album extends Model implements Searchable, Commentable, Favouritable
{
use HasFactory;
use SoftDeletes, SlugTrait, TrackCollection, RevisionableTrait, IndexedInElasticsearchTrait;
protected $elasticsearchType = 'album';

View file

@ -20,6 +20,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Traits\SlugTrait;
use DB;
use Illuminate\Database\Eloquent\Model;
@ -57,6 +58,8 @@ use Venturecraft\Revisionable\RevisionableTrait;
*/
class Genre extends Model
{
use HasFactory;
protected $table = 'genres';
protected $fillable = ['name', 'slug'];

View file

@ -20,6 +20,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Contracts\Commentable;
use App\Contracts\Favouritable;
use App\Contracts\Searchable;
@ -148,6 +149,8 @@ use Venturecraft\Revisionable\RevisionableTrait;
*/
class Track extends Model implements Searchable, Commentable, Favouritable
{
use HasFactory;
use SoftDeletes, IndexedInElasticsearchTrait;
protected $elasticsearchType = 'track';

View file

@ -20,6 +20,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Contracts\Commentable;
use App\Contracts\Searchable;
use App\Traits\IndexedInElasticsearchTrait;
@ -104,6 +105,8 @@ use Venturecraft\Revisionable\RevisionableTrait;
*/
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, \Illuminate\Contracts\Auth\Access\Authorizable, Searchable, Commentable
{
use HasFactory;
use Authenticatable, CanResetPassword, Authorizable, RevisionableTrait, IndexedInElasticsearchTrait;
protected $elasticsearchType = 'user';

View file

@ -0,0 +1,59 @@
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2015-2017 Feld0.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\User;
class AlbumFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Album::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'title' => $this->faker->sentence(5),
'slug' => $this->faker->slug,
'description' => $this->faker->paragraph(5),
];
}
}

View file

@ -0,0 +1,76 @@
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2015-2017 Feld0.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\User;
/**
* @property int $id
* @property int $user_id
* @property string $title
* @property string $slug
* @property string $description
* @property int $cover_id
* @property int $track_count
* @property int $view_count
* @property int $download_count
* @property int $favourite_count
* @property int $comment_count
* @property \Carbon\Carbon $created_at
* @property string $updated_at
* @property \Carbon\Carbon $deleted_at
*/
class GenreFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Genre::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->word,
'slug' => $this->faker->slug,
];
}
}

View file

@ -1,98 +0,0 @@
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2015-2017 Feld0.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
use App\Models\User;
$factory->define(App\Models\User::class, function (Faker\Generator $faker) {
return [
'username' => $faker->userName,
'display_name' => $faker->userName,
'slug' => $faker->slug,
'email' => $faker->email,
'can_see_explicit_content' => true,
'uses_gravatar' => true,
'bio' => $faker->paragraph,
'track_count' => 0,
'comment_count' => 0,
];
});
$factory->define(\App\Models\Track::class, function (Faker\Generator $faker) {
$user = factory(User::class)->create();
return [
'user_id' => $user->id,
'hash' => $faker->md5,
'title' => $faker->sentence(5),
'slug' => $faker->slug,
'track_type_id' => \App\Models\TrackType::UNCLASSIFIED_TRACK,
'track_number' => null,
'description' => $faker->paragraph(5),
'lyrics' => $faker->paragraph(5),
'is_vocal' => true,
'is_explicit' => false,
'is_downloadable' => true,
'is_listed' => true,
'metadata' => '{"this":{"is":["very","random","metadata"]}}',
'duration' => $faker->randomFloat(null, 30, 600),
];
});
$factory->define(\App\Models\Genre::class, function (Faker\Generator $faker) {
return [
'name' => $faker->word,
'slug' => $faker->slug,
];
});
/**
* @property int $id
* @property int $user_id
* @property string $title
* @property string $slug
* @property string $description
* @property int $cover_id
* @property int $track_count
* @property int $view_count
* @property int $download_count
* @property int $favourite_count
* @property int $comment_count
* @property \Carbon\Carbon $created_at
* @property string $updated_at
* @property \Carbon\Carbon $deleted_at
*/
$factory->define(\App\Models\Album::class, function (Faker\Generator $faker) {
return [
'title' => $faker->sentence(5),
'slug' => $faker->slug,
'description' => $faker->paragraph(5),
];
});

View file

@ -0,0 +1,72 @@
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2015-2017 Feld0.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\User;
class TrackFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Track::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$user = User::factory()->create();
return [
'user_id' => $user->id,
'hash' => $this->faker->md5,
'title' => $this->faker->sentence(5),
'slug' => $this->faker->slug,
'track_type_id' => \App\Models\TrackType::UNCLASSIFIED_TRACK,
'track_number' => null,
'description' => $this->faker->paragraph(5),
'lyrics' => $this->faker->paragraph(5),
'is_vocal' => true,
'is_explicit' => false,
'is_downloadable' => true,
'is_listed' => true,
'metadata' => '{"this":{"is":["very","random","metadata"]}}',
'duration' => $this->faker->randomFloat(null, 30, 600),
];
}
}

View file

@ -0,0 +1,65 @@
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2015-2017 Feld0.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\User;
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\User::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'username' => $this->faker->userName,
'display_name' => $this->faker->userName,
'slug' => $this->faker->slug,
'email' => $this->faker->email,
'can_see_explicit_content' => true,
'uses_gravatar' => true,
'bio' => $this->faker->paragraph,
'track_count' => 0,
'comment_count' => 0,
];
}
}

View file

@ -35,7 +35,7 @@ class ApiAuthTest extends TestCase
*/
public function testApiCreatesNewUser()
{
$user = factory(User::class)->make();
$user = User::factory()->make();
$accessTokenInfo = new AccessToken('nonsense-token');
$accessTokenInfo->setIsActive(true);
$accessTokenInfo->setScopes(['basic', 'ponyfm:tracks:upload']);
@ -61,7 +61,7 @@ class ApiAuthTest extends TestCase
public function testApiClientIdIsRecordedWhenUploadingTrack()
{
$user = factory(User::class)->make();
$user = User::factory()->make();
$accessTokenInfo = new AccessToken('nonsense-token');
$accessTokenInfo->setIsActive(true);

View file

@ -35,7 +35,7 @@ class ApiTest extends TestCase
public function testUploadWithoutFile()
{
$user = factory(User::class)->create();
$user = User::factory()->create();
$this->actingAs($user)
->post('/api/v1/tracks', [])
@ -80,11 +80,11 @@ class ApiTest extends TestCase
public function testUploadWithOptionalData()
{
/** @var Track $track */
$track = factory(Track::class)->make();
$track = Track::factory()->make();
/** @var Genre $genre */
$genre = factory(Genre::class)->make();
$genre = Genre::factory()->make();
/** @var Album $album */
$album = factory(Album::class)->make();
$album = Album::factory()->make();
$this->callUploadWithParameters([
'title' => $track->title,
@ -135,9 +135,9 @@ class ApiTest extends TestCase
public function testGetTrackDetails()
{
/** @var Track $track */
$track = factory(Track::class)->create();
$track = Track::factory()->create();
/** @var Genre $genre */
$genre = factory(Genre::class)->create();
$genre = Genre::factory()->create();
$track->genre()->associate($genre);
$this->seeInDatabase('tracks', ['id' => $track->id]);

View file

@ -142,7 +142,7 @@ class TestCase extends BaseTestCase
\App\Jobs\EncodeTrackFile::class,
\App\Jobs\UpdateSearchIndexForEntity::class,
]);
$this->user = factory(User::class)->create();
$this->user = User::factory()->create();
$file = $this->getTestFileForUpload('ponyfm-test.flac');