Skip to content

Commit

Permalink
refactor: changed thread pk type (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored Jun 2, 2024
1 parent d4dea63 commit 9f0a6a3
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 3 deletions.
14 changes: 12 additions & 2 deletions app/Models/Discord/DiscordThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
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;

/**
* Class DiscordThread.
*
* @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
{
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -82,7 +92,7 @@ public function getName(): string
*/
public function getSubtitle(): string
{
return $this->getKey();
return $this->anime->getName();
}

/**
Expand Down
39 changes: 39 additions & 0 deletions database/factories/Discord/DiscordThreadFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Database\Factories\Discord;

use App\Models\Discord\DiscordThread;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* Class DiscordThreadFactory.
*
* @method DiscordThread createOne($attributes = [])
* @method DiscordThread makeOne($attributes = [])
*
* @extends Factory<DiscordThread>
*/
class DiscordThreadFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var class-string<DiscordThread>
*/
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),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
67 changes: 67 additions & 0 deletions tests/Feature/Jobs/Discord/DiscordThreadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Jobs\Discord;

use App\Constants\FeatureConstants;
use App\Events\Discord\DiscordThread\DiscordThreadDeleted;
use App\Events\Discord\DiscordThread\DiscordThreadUpdated;
use App\Jobs\SendDiscordNotificationJob;
use App\Models\Discord\DiscordThread;
use App\Models\Wiki\Anime;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Event;
use Laravel\Pennant\Feature;
use Tests\TestCase;

/**
* Class DiscordThreadTest.
*/
class DiscordThreadTest extends TestCase
{
/**
* When a thread is deleted, a SendDiscordNotification job shall be dispatched.
*
* @return void
*/
public function testThreadDeletedSendsDiscordNotification(): void
{
$thread = DiscordThread::factory()
->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);
}
}
44 changes: 44 additions & 0 deletions tests/Unit/Models/Discord/DiscordThreadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

use App\Models\Discord\DiscordThread;
use App\Models\Wiki\Anime;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

/**
* Class DiscordThreadTest.
*/
class DiscordThreadTest extends TestCase
{
use WithFaker;

/**
* Thread shall be nameable.
*
* @return void
*/
public function testNameable(): void
{
$thread = DiscordThread::factory()
->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());
}
}

0 comments on commit 9f0a6a3

Please sign in to comment.