From ff8adafd33c1d19b40082fbb749245a60b188556 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sat, 10 Jul 2021 00:59:20 +0200 Subject: [PATCH] Added more factories --- db/factories/FaqFactory.php | 23 +++++++++++ db/factories/MessageFactory.php | 26 ++++++++++++ db/factories/NewsCommentFactory.php | 26 ++++++++++++ db/factories/NewsFactory.php | 27 ++++++++++++ db/factories/QuestionFactory.php | 32 ++++++++++++++ db/factories/RoomFactory.php | 24 +++++++++++ db/factories/Shifts/ScheduleFactory.php | 26 ++++++++++++ db/factories/WorklogFactory.php | 27 ++++++++++++ src/Models/Faq.php | 3 ++ src/Models/Message.php | 2 + src/Models/News.php | 2 + src/Models/NewsComment.php | 2 + src/Models/Question.php | 2 + src/Models/Room.php | 3 ++ src/Models/Shifts/Schedule.php | 3 ++ src/Models/Worklog.php | 2 + tests/Unit/Events/Listener/NewsTest.php | 8 +--- tests/Unit/FactoriesTest.php | 55 +++++++++++++++++++++++++ 18 files changed, 287 insertions(+), 6 deletions(-) create mode 100644 db/factories/FaqFactory.php create mode 100644 db/factories/MessageFactory.php create mode 100644 db/factories/NewsCommentFactory.php create mode 100644 db/factories/NewsFactory.php create mode 100644 db/factories/QuestionFactory.php create mode 100644 db/factories/RoomFactory.php create mode 100644 db/factories/Shifts/ScheduleFactory.php create mode 100644 db/factories/WorklogFactory.php create mode 100644 tests/Unit/FactoriesTest.php diff --git a/db/factories/FaqFactory.php b/db/factories/FaqFactory.php new file mode 100644 index 00000000..616ae436 --- /dev/null +++ b/db/factories/FaqFactory.php @@ -0,0 +1,23 @@ + $this->faker->text(100), + 'text' => $this->faker->optional(.5, '')->realText(), + ]; + } +} diff --git a/db/factories/MessageFactory.php b/db/factories/MessageFactory.php new file mode 100644 index 00000000..2110bc8c --- /dev/null +++ b/db/factories/MessageFactory.php @@ -0,0 +1,26 @@ + User::factory(), + 'receiver_id' => User::factory(), + 'read' => $this->faker->boolean(), + 'text' => $this->faker->text(), + ]; + } +} diff --git a/db/factories/NewsCommentFactory.php b/db/factories/NewsCommentFactory.php new file mode 100644 index 00000000..4a8880aa --- /dev/null +++ b/db/factories/NewsCommentFactory.php @@ -0,0 +1,26 @@ + News::factory(), + 'user_id' => User::factory(), + 'text' => $this->faker->text(), + ]; + } +} diff --git a/db/factories/NewsFactory.php b/db/factories/NewsFactory.php new file mode 100644 index 00000000..e7125386 --- /dev/null +++ b/db/factories/NewsFactory.php @@ -0,0 +1,27 @@ + $this->faker->text(50), + 'text' => $this->faker->realText(), + 'is_meeting' => $this->faker->boolean(), + 'is_pinned' => $this->faker->boolean(.1), + 'user_id' => User::factory(), + ]; + } +} diff --git a/db/factories/QuestionFactory.php b/db/factories/QuestionFactory.php new file mode 100644 index 00000000..cac05b5a --- /dev/null +++ b/db/factories/QuestionFactory.php @@ -0,0 +1,32 @@ + User::factory(), + 'text' => $this->faker->text(100), + 'answerer_id' => $this->faker->optional()->passthrough(User::factory()), + 'answer' => function (array $attributes) { + return $attributes['answerer_id'] ? $this->faker->text() : null; + }, + 'answered_at' => function (array $attributes) { + return $attributes['answerer_id'] ? Carbon::instance($this->faker->dateTimeThisMonth()) : null; + }, + ]; + } +} diff --git a/db/factories/RoomFactory.php b/db/factories/RoomFactory.php new file mode 100644 index 00000000..a825816c --- /dev/null +++ b/db/factories/RoomFactory.php @@ -0,0 +1,24 @@ + $this->faker->unique()->firstName(), + 'map_url' => $this->faker->url(), + 'description' => $this->faker->text(), + ]; + } +} diff --git a/db/factories/Shifts/ScheduleFactory.php b/db/factories/Shifts/ScheduleFactory.php new file mode 100644 index 00000000..7dec1d1f --- /dev/null +++ b/db/factories/Shifts/ScheduleFactory.php @@ -0,0 +1,26 @@ + $this->faker->words(4, true), + 'url' => $this->faker->parse('https://{{safeEmailDomain}}/{{slug}}.xml'), + 'shift_type' => $this->faker->numberBetween(1, 5), + 'minutes_before' => 15, + 'minutes_after' => 15, + ]; + } +} diff --git a/db/factories/WorklogFactory.php b/db/factories/WorklogFactory.php new file mode 100644 index 00000000..cc0c7c11 --- /dev/null +++ b/db/factories/WorklogFactory.php @@ -0,0 +1,27 @@ + User::factory(), + 'creator_id' => User::factory(), + 'hours' => $this->faker->randomFloat(2, 0.01, 10), + 'comment' => $this->faker->text(30), + 'worked_at' => $this->faker->dateTimeThisMonth(), + ]; + } +} diff --git a/src/Models/Faq.php b/src/Models/Faq.php index 6f57c9b8..c77e4c74 100644 --- a/src/Models/Faq.php +++ b/src/Models/Faq.php @@ -6,6 +6,7 @@ namespace Engelsystem\Models; use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Factories\HasFactory; /** * @property int $id @@ -20,6 +21,8 @@ use Illuminate\Database\Eloquent\Builder; */ class Faq extends BaseModel { + use HasFactory; + /** @var bool Enable timestamps */ public $timestamps = true; diff --git a/src/Models/Message.php b/src/Models/Message.php index f865c873..55780fc3 100644 --- a/src/Models/Message.php +++ b/src/Models/Message.php @@ -7,6 +7,7 @@ namespace Engelsystem\Models; use Engelsystem\Models\User\User; use Engelsystem\Models\User\UsesUserModel; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Support\Carbon; @@ -29,6 +30,7 @@ use Illuminate\Support\Carbon; */ class Message extends BaseModel { + use HasFactory; use UsesUserModel; /** @var bool enable timestamps */ diff --git a/src/Models/News.php b/src/Models/News.php index 4de04cd0..e1d246b8 100644 --- a/src/Models/News.php +++ b/src/Models/News.php @@ -7,6 +7,7 @@ namespace Engelsystem\Models; use Carbon\Carbon; use Engelsystem\Models\User\UsesUserModel; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Query\Builder as QueryBuilder; @@ -32,6 +33,7 @@ use Illuminate\Database\Query\Builder as QueryBuilder; */ class News extends BaseModel { + use HasFactory; use UsesUserModel; /** @var bool Enable timestamps */ diff --git a/src/Models/NewsComment.php b/src/Models/NewsComment.php index 447d980f..5adc5498 100644 --- a/src/Models/NewsComment.php +++ b/src/Models/NewsComment.php @@ -6,6 +6,7 @@ namespace Engelsystem\Models; use Carbon\Carbon; use Engelsystem\Models\User\UsesUserModel; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Query\Builder as QueryBuilder; @@ -27,6 +28,7 @@ use Illuminate\Database\Query\Builder as QueryBuilder; */ class NewsComment extends BaseModel { + use HasFactory; use UsesUserModel; /** @var bool Enable timestamps */ diff --git a/src/Models/Question.php b/src/Models/Question.php index a2952a0b..51be1cc6 100644 --- a/src/Models/Question.php +++ b/src/Models/Question.php @@ -8,6 +8,7 @@ use Carbon\Carbon; use Engelsystem\Models\User\User; use Engelsystem\Models\User\UsesUserModel; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Query\Builder as QueryBuilder; @@ -29,6 +30,7 @@ use Illuminate\Database\Query\Builder as QueryBuilder; */ class Question extends BaseModel { + use HasFactory; use UsesUserModel; /** @var bool Enable timestamps */ diff --git a/src/Models/Room.php b/src/Models/Room.php index 93389ce3..a4f0dbfa 100644 --- a/src/Models/Room.php +++ b/src/Models/Room.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Engelsystem\Models; use Carbon\Carbon; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Query\Builder as QueryBuilder; /** @@ -24,6 +25,8 @@ use Illuminate\Database\Query\Builder as QueryBuilder; */ class Room extends BaseModel { + use HasFactory; + /** @var bool Enable timestamps */ public $timestamps = true; diff --git a/src/Models/Shifts/Schedule.php b/src/Models/Shifts/Schedule.php index 3f58bcf6..9c718a25 100644 --- a/src/Models/Shifts/Schedule.php +++ b/src/Models/Shifts/Schedule.php @@ -5,6 +5,7 @@ namespace Engelsystem\Models\Shifts; use Carbon\Carbon; use Engelsystem\Models\BaseModel; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Query\Builder as QueryBuilder; @@ -31,6 +32,8 @@ use Illuminate\Database\Query\Builder as QueryBuilder; */ class Schedule extends BaseModel { + use HasFactory; + /** @var bool enable timestamps */ public $timestamps = true; diff --git a/src/Models/Worklog.php b/src/Models/Worklog.php index cc290a09..290b3da3 100644 --- a/src/Models/Worklog.php +++ b/src/Models/Worklog.php @@ -5,6 +5,7 @@ namespace Engelsystem\Models; use Carbon\Carbon; use Engelsystem\Models\User\User; use Engelsystem\Models\User\UsesUserModel; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Query\Builder as QueryBuilder; @@ -27,6 +28,7 @@ use Illuminate\Database\Query\Builder as QueryBuilder; */ class Worklog extends BaseModel { + use HasFactory; use UsesUserModel; /** @var bool Enable timestamps */ diff --git a/tests/Unit/Events/Listener/NewsTest.php b/tests/Unit/Events/Listener/NewsTest.php index 0bbf4805..5110f970 100644 --- a/tests/Unit/Events/Listener/NewsTest.php +++ b/tests/Unit/Events/Listener/NewsTest.php @@ -34,12 +34,8 @@ class NewsTest extends TestCase */ public function testCreated() { - $news = new NewsModel([ - 'title' => 'Foo', - 'text' => 'Bar', - 'user_id' => 1, - ]); - $news->save(); + /** @var NewsModel $news */ + $news = NewsModel::factory(['title' => 'Foo'])->create(); $i = 0; $this->mailer->expects($this->exactly(2)) diff --git a/tests/Unit/FactoriesTest.php b/tests/Unit/FactoriesTest.php new file mode 100644 index 00000000..dafc8638 --- /dev/null +++ b/tests/Unit/FactoriesTest.php @@ -0,0 +1,55 @@ +initDatabase(); + + foreach ($this->models as $model) { + $instance = (new $model())->factory()->create(); + $this->assertInstanceOf(Model::class, $instance); + } + } +}