From 9f0a6a3ae2e941f6813366efa62513d63da48684 Mon Sep 17 00:00:00 2001 From: Kyrch Date: Sun, 2 Jun 2024 14:56:18 -0300 Subject: [PATCH] refactor: changed thread pk type (#688) --- app/Models/Discord/DiscordThread.php | 14 +++- .../Discord/DiscordThreadFactory.php | 39 +++++++++++ ...31_052841_create_discord_threads_table.php | 2 +- .../Jobs/Discord/DiscordThreadTest.php | 67 +++++++++++++++++++ .../Unit/Models/Discord/DiscordThreadTest.php | 44 ++++++++++++ 5 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 database/factories/Discord/DiscordThreadFactory.php create mode 100644 tests/Feature/Jobs/Discord/DiscordThreadTest.php create mode 100644 tests/Unit/Models/Discord/DiscordThreadTest.php diff --git a/app/Models/Discord/DiscordThread.php b/app/Models/Discord/DiscordThread.php index 2343e5b89..767a8a0b7 100644 --- a/app/Models/Discord/DiscordThread.php +++ b/app/Models/Discord/DiscordThread.php @@ -8,6 +8,7 @@ use App\Events\Discord\DiscordThread\DiscordThreadUpdated; use App\Models\BaseModel; use App\Models\Wiki\Anime; +use Database\Factories\Discord\DiscordThreadFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; /** @@ -15,8 +16,10 @@ * * @property Anime $anime * @property int $anime_id - * @property int $thread_id + * @property string $thread_id * @property string $name + * + * @method static DiscordThreadFactory factory(...$parameters) */ class DiscordThread extends BaseModel { @@ -53,6 +56,13 @@ class DiscordThread extends BaseModel */ protected $primaryKey = DiscordThread::ATTRIBUTE_ID; + /** + * The data type of the primary key ID. + * + * @var string + */ + protected $keyType = 'string'; + /** * The event map for the model. * @@ -82,7 +92,7 @@ public function getName(): string */ public function getSubtitle(): string { - return $this->getKey(); + return $this->anime->getName(); } /** diff --git a/database/factories/Discord/DiscordThreadFactory.php b/database/factories/Discord/DiscordThreadFactory.php new file mode 100644 index 000000000..b4899b5ad --- /dev/null +++ b/database/factories/Discord/DiscordThreadFactory.php @@ -0,0 +1,39 @@ + + */ +class DiscordThreadFactory extends Factory +{ + /** + * The name of the factory's corresponding model. + * + * @var class-string + */ + protected $model = DiscordThread::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + DiscordThread::ATTRIBUTE_NAME => fake()->words(3, true), + DiscordThread::ATTRIBUTE_ID => fake()->words(3, true), + ]; + } +} \ No newline at end of file diff --git a/database/migrations/2024_05_31_052841_create_discord_threads_table.php b/database/migrations/2024_05_31_052841_create_discord_threads_table.php index 4eb9fca09..1eeeea515 100644 --- a/database/migrations/2024_05_31_052841_create_discord_threads_table.php +++ b/database/migrations/2024_05_31_052841_create_discord_threads_table.php @@ -20,7 +20,7 @@ public function up(): void Schema::create(DiscordThread::TABLE, function (Blueprint $table) { $table->timestamps(6); $table->softDeletes(BaseModel::ATTRIBUTE_DELETED_AT, 6); - $table->bigInteger(DiscordThread::ATTRIBUTE_ID)->primary(); + $table->string(DiscordThread::ATTRIBUTE_ID)->primary(); $table->string(DiscordThread::ATTRIBUTE_NAME); $table->unsignedBigInteger(DiscordThread::ATTRIBUTE_ANIME); diff --git a/tests/Feature/Jobs/Discord/DiscordThreadTest.php b/tests/Feature/Jobs/Discord/DiscordThreadTest.php new file mode 100644 index 000000000..7d5d6b64b --- /dev/null +++ b/tests/Feature/Jobs/Discord/DiscordThreadTest.php @@ -0,0 +1,67 @@ +for(Anime::factory()) + ->createOne(); + + Feature::activate(FeatureConstants::ALLOW_DISCORD_NOTIFICATIONS); + Bus::fake(SendDiscordNotificationJob::class); + Event::fakeExcept(DiscordThreadDeleted::class); + + $thread->delete(); + + Bus::assertDispatched(SendDiscordNotificationJob::class); + } + + /** + * When a thread is updated, a SendDiscordNotification job shall be dispatched. + * + * @return void + */ + public function testThreadUpdatedSendsDiscordNotification(): void + { + $thread = DiscordThread::factory() + ->for(Anime::factory()) + ->createOne(); + + Feature::activate(FeatureConstants::ALLOW_DISCORD_NOTIFICATIONS); + Bus::fake(SendDiscordNotificationJob::class); + Event::fakeExcept(DiscordThreadUpdated::class); + + $changes = DiscordThread::factory() + ->for(Anime::factory()) + ->makeOne(); + + $thread->fill($changes->getAttributes()); + $thread->save(); + + Bus::assertDispatched(SendDiscordNotificationJob::class); + } +} \ No newline at end of file diff --git a/tests/Unit/Models/Discord/DiscordThreadTest.php b/tests/Unit/Models/Discord/DiscordThreadTest.php new file mode 100644 index 000000000..faf09d0da --- /dev/null +++ b/tests/Unit/Models/Discord/DiscordThreadTest.php @@ -0,0 +1,44 @@ +for(Anime::factory()) + ->createOne(); + + static::assertIsString($thread->getName()); + } + + /** + * Thread shall have subtitle. + * + * @return void + */ + public function testHasSubtitle(): void + { + $thread = DiscordThread::factory() + ->for(Anime::factory()) + ->createOne(); + + static::assertIsString($thread->getSubtitle()); + } +}