From 635b39d1092e89f42753ae9de915d87ca13793d9 Mon Sep 17 00:00:00 2001 From: Laravel Shift Date: Sun, 14 Feb 2021 19:11:17 +0000 Subject: [PATCH] Shift to class based factories --- app/Models/Album.php | 3 + app/Models/Genre.php | 3 + app/Models/Track.php | 3 + app/Models/User.php | 3 + database/factories/AlbumFactory.php | 59 +++++++++++++++++ database/factories/GenreFactory.php | 76 ++++++++++++++++++++++ database/factories/ModelFactory.php | 98 ----------------------------- database/factories/TrackFactory.php | 72 +++++++++++++++++++++ database/factories/UserFactory.php | 65 +++++++++++++++++++ tests/ApiAuthTest.php | 4 +- tests/ApiTest.php | 12 ++-- tests/TestCase.php | 2 +- 12 files changed, 293 insertions(+), 107 deletions(-) create mode 100644 database/factories/AlbumFactory.php create mode 100644 database/factories/GenreFactory.php delete mode 100644 database/factories/ModelFactory.php create mode 100644 database/factories/TrackFactory.php create mode 100644 database/factories/UserFactory.php diff --git a/app/Models/Album.php b/app/Models/Album.php index c43b343f..cc88c867 100644 --- a/app/Models/Album.php +++ b/app/Models/Album.php @@ -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'; diff --git a/app/Models/Genre.php b/app/Models/Genre.php index c1548b79..0b9f2131 100644 --- a/app/Models/Genre.php +++ b/app/Models/Genre.php @@ -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']; diff --git a/app/Models/Track.php b/app/Models/Track.php index 89af711a..4ba00926 100644 --- a/app/Models/Track.php +++ b/app/Models/Track.php @@ -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'; diff --git a/app/Models/User.php b/app/Models/User.php index 8a195748..1426793d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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'; diff --git a/database/factories/AlbumFactory.php b/database/factories/AlbumFactory.php new file mode 100644 index 00000000..2017e0ae --- /dev/null +++ b/database/factories/AlbumFactory.php @@ -0,0 +1,59 @@ +. + */ + +/* +|-------------------------------------------------------------------------- +| 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), + ]; + } +} diff --git a/database/factories/GenreFactory.php b/database/factories/GenreFactory.php new file mode 100644 index 00000000..69014a44 --- /dev/null +++ b/database/factories/GenreFactory.php @@ -0,0 +1,76 @@ +. + */ + +/* +|-------------------------------------------------------------------------- +| 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, + ]; + } +} diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php deleted file mode 100644 index c6da6be3..00000000 --- a/database/factories/ModelFactory.php +++ /dev/null @@ -1,98 +0,0 @@ -. - */ - -/* -|-------------------------------------------------------------------------- -| 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), - ]; -}); diff --git a/database/factories/TrackFactory.php b/database/factories/TrackFactory.php new file mode 100644 index 00000000..7120d694 --- /dev/null +++ b/database/factories/TrackFactory.php @@ -0,0 +1,72 @@ +. + */ + +/* +|-------------------------------------------------------------------------- +| 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), + ]; + } +} diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php new file mode 100644 index 00000000..79ce36ad --- /dev/null +++ b/database/factories/UserFactory.php @@ -0,0 +1,65 @@ +. + */ + +/* +|-------------------------------------------------------------------------- +| 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, + ]; + } +} diff --git a/tests/ApiAuthTest.php b/tests/ApiAuthTest.php index e5f09530..a4f3af22 100644 --- a/tests/ApiAuthTest.php +++ b/tests/ApiAuthTest.php @@ -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); diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 698534f2..46b53b82 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -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]); diff --git a/tests/TestCase.php b/tests/TestCase.php index ee4dc629..37e2ba38 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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');