2 - Add Instance Eloquent model with factory
This commit is contained in:
parent
bdd2b0f2e5
commit
fc1c8ba020
4 changed files with 153 additions and 1 deletions
|
|
@ -17,7 +17,7 @@ public function up(): void
|
||||||
$table->string('url');
|
$table->string('url');
|
||||||
$table->boolean('enabled')->default(true);
|
$table->boolean('enabled')->default(true);
|
||||||
$table->unsignedInteger('interval_seconds')->default(300);
|
$table->unsignedInteger('interval_seconds')->default(300);
|
||||||
$table->json('config')->default('{}');
|
$table->json('extras')->default('{}');
|
||||||
$table->timestampTz('last_polled_at')->nullable();
|
$table->timestampTz('last_polled_at')->nullable();
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lvl0\FediDiscover\Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Lvl0\FediDiscover\Models\Instance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<Instance>
|
||||||
|
*/
|
||||||
|
class InstanceFactory extends Factory
|
||||||
|
{
|
||||||
|
protected $model = Instance::class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
38
packages/Lvl0/FediDiscover/src/Models/Instance.php
Normal file
38
packages/Lvl0/FediDiscover/src/Models/Instance.php
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lvl0\FediDiscover\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Lvl0\FediDiscover\Config\InstanceType;
|
||||||
|
use Lvl0\FediDiscover\Database\Factories\InstanceFactory;
|
||||||
|
|
||||||
|
class Instance extends Model
|
||||||
|
{
|
||||||
|
/** @use HasFactory<InstanceFactory> */
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $table = 'fedi_discover_instances';
|
||||||
|
|
||||||
|
protected $fillable = ['type', 'url', 'enabled', 'interval_seconds', 'extras', 'last_polled_at'];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'type' => InstanceType::class,
|
||||||
|
'enabled' => 'boolean',
|
||||||
|
'extras' => 'array',
|
||||||
|
'last_polled_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function scopeEnabled($query)
|
||||||
|
{
|
||||||
|
return $query->where('enabled', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function newFactory(): Factory
|
||||||
|
{
|
||||||
|
return InstanceFactory::new();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,88 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lvl0\FediDiscover\Tests\Feature;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Lvl0\FediDiscover\Config\InstanceType;
|
||||||
|
use Lvl0\FediDiscover\Models\Instance;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class InstanceModelTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_it_persists_and_retrieves_an_instance(): void
|
||||||
|
{
|
||||||
|
Instance::create([
|
||||||
|
'type' => InstanceType::Mastodon,
|
||||||
|
'url' => 'https://mastodon.social',
|
||||||
|
'enabled' => true,
|
||||||
|
'interval_seconds' => 600,
|
||||||
|
'extras' => ['token' => 'abc123'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$instance = Instance::first();
|
||||||
|
|
||||||
|
$this->assertNotNull($instance);
|
||||||
|
$this->assertSame(InstanceType::Mastodon, $instance->type);
|
||||||
|
$this->assertSame('https://mastodon.social', $instance->url);
|
||||||
|
$this->assertTrue($instance->enabled);
|
||||||
|
$this->assertSame(600, $instance->interval_seconds);
|
||||||
|
$this->assertSame(['token' => 'abc123'], $instance->extras);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_enabled_is_fillable_and_cast_to_boolean(): void
|
||||||
|
{
|
||||||
|
$instance = Instance::create([
|
||||||
|
'type' => InstanceType::Mastodon,
|
||||||
|
'url' => 'https://mastodon.social',
|
||||||
|
'enabled' => false,
|
||||||
|
'interval_seconds' => 600,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertFalse($instance->fresh()->enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_last_polled_at_is_fillable_and_cast_to_datetime(): void
|
||||||
|
{
|
||||||
|
$polledAt = Carbon::parse('2026-04-23 12:00:00');
|
||||||
|
|
||||||
|
$instance = Instance::create([
|
||||||
|
'type' => InstanceType::Mastodon,
|
||||||
|
'url' => 'https://mastodon.social',
|
||||||
|
'enabled' => true,
|
||||||
|
'interval_seconds' => 600,
|
||||||
|
'last_polled_at' => $polledAt,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$fresh = $instance->fresh();
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Carbon::class, $fresh->last_polled_at);
|
||||||
|
$this->assertTrue($fresh->last_polled_at->equalTo($polledAt));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_enabled_scope_returns_only_enabled_instances(): void
|
||||||
|
{
|
||||||
|
Instance::create([
|
||||||
|
'type' => InstanceType::Mastodon,
|
||||||
|
'url' => 'https://enabled.example',
|
||||||
|
'enabled' => true,
|
||||||
|
'interval_seconds' => 600,
|
||||||
|
]);
|
||||||
|
|
||||||
|
Instance::create([
|
||||||
|
'type' => InstanceType::Mastodon,
|
||||||
|
'url' => 'https://disabled.example',
|
||||||
|
'enabled' => false,
|
||||||
|
'interval_seconds' => 600,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$enabled = Instance::enabled()->get();
|
||||||
|
|
||||||
|
$this->assertCount(1, $enabled);
|
||||||
|
$this->assertSame('https://enabled.example', $enabled->first()->url);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue