99 lines
3.2 KiB
PHP
99 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Models;
|
|
|
|
use App\Enums\PageStatusEnum;
|
|
use App\Models\Page;
|
|
use App\Models\PageLink;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Lvl0\FediDiscover\Config\InstanceType;
|
|
use Lvl0\FediDiscover\Models\Instance;
|
|
use Tests\TestCase;
|
|
|
|
class PageTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_page_model_fillable_fields_can_be_mass_assigned(): void
|
|
{
|
|
$page = Page::create([
|
|
'url' => 'https://example.com/article',
|
|
'status' => 'discovered',
|
|
'title' => 'An Example Article',
|
|
'instance_id' => null,
|
|
'posted_at' => null,
|
|
'fetched_at' => null,
|
|
]);
|
|
|
|
$fresh = $page->fresh();
|
|
|
|
$this->assertNotNull($fresh);
|
|
$this->assertSame('https://example.com/article', $fresh->url);
|
|
$this->assertSame('An Example Article', $fresh->title);
|
|
$this->assertNull($fresh->instance_id);
|
|
}
|
|
|
|
public function test_page_instance_relationship_returns_the_owning_instance(): void
|
|
{
|
|
$instance = Instance::factory()
|
|
->type(InstanceType::Mastodon)
|
|
->enabled()
|
|
->create();
|
|
|
|
$page = Page::create([
|
|
'url' => 'https://example.com/post/1',
|
|
'status' => 'discovered',
|
|
'instance_id' => $instance->id,
|
|
]);
|
|
|
|
$fresh = $page->fresh();
|
|
|
|
$this->assertInstanceOf(Instance::class, $fresh->instance);
|
|
$this->assertSame($instance->id, $fresh->instance->id);
|
|
}
|
|
|
|
public function test_page_outgoing_and_incoming_links_relationships(): void
|
|
{
|
|
$source = Page::factory()->create(['url' => 'https://example.com/source']);
|
|
$target = Page::factory()->create(['url' => 'https://example.com/target']);
|
|
|
|
PageLink::create([
|
|
'source_page_id' => $source->id,
|
|
'target_page_id' => $target->id,
|
|
]);
|
|
|
|
$freshSource = $source->fresh();
|
|
$freshTarget = $target->fresh();
|
|
|
|
$this->assertCount(1, $freshSource->outgoingLinks);
|
|
$this->assertCount(0, $freshSource->incomingLinks);
|
|
$this->assertCount(1, $freshTarget->incomingLinks);
|
|
$this->assertCount(0, $freshTarget->outgoingLinks);
|
|
|
|
$this->assertSame($source->id, $freshTarget->incomingLinks->first()->source_page_id);
|
|
$this->assertSame($target->id, $freshSource->outgoingLinks->first()->target_page_id);
|
|
}
|
|
|
|
public function test_page_status_is_cast_to_enum(): void
|
|
{
|
|
$cases = [
|
|
['string' => 'discovered', 'enum' => PageStatusEnum::Discovered],
|
|
['string' => 'fetched', 'enum' => PageStatusEnum::Fetched],
|
|
['string' => 'failed', 'enum' => PageStatusEnum::Failed],
|
|
];
|
|
|
|
foreach ($cases as ['string' => $raw, 'enum' => $expected]) {
|
|
$page = Page::create([
|
|
'url' => 'https://example.com/' . $raw,
|
|
'status' => $raw,
|
|
]);
|
|
|
|
$fresh = $page->fresh();
|
|
|
|
$this->assertInstanceOf(PageStatusEnum::class, $fresh->status, "status '{$raw}' should cast to PageStatusEnum");
|
|
$this->assertSame($expected, $fresh->status, "status '{$raw}' should equal PageStatusEnum::{$expected->name}");
|
|
}
|
|
}
|
|
}
|