109 lines
3.1 KiB
PHP
109 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Models;
|
|
|
|
use App\Models\Institution;
|
|
use App\Models\InstitutionLink;
|
|
use App\Models\Lists\InstitutionCategory;
|
|
use App\Models\UserAffiliation;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Str;
|
|
use Tests\TestCase;
|
|
|
|
class InstitutionTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_uuid_is_generated_on_create(): void
|
|
{
|
|
$institution = Institution::factory()->create();
|
|
|
|
$this->assertNotEmpty($institution->uuid);
|
|
$this->assertSame(36, strlen((string) $institution->uuid));
|
|
}
|
|
|
|
public function test_two_institutions_get_distinct_uuids(): void
|
|
{
|
|
$a = Institution::factory()->create();
|
|
$b = Institution::factory()->create();
|
|
|
|
$this->assertNotSame($a->uuid, $b->uuid);
|
|
}
|
|
|
|
public function test_a_provided_uuid_is_not_overwritten(): void
|
|
{
|
|
$uuid = (string) Str::uuid();
|
|
|
|
$institution = Institution::factory()->make();
|
|
$institution->uuid = $uuid;
|
|
$institution->save();
|
|
|
|
$this->assertSame($uuid, $institution->fresh()->uuid);
|
|
}
|
|
|
|
public function test_route_key_is_the_uuid(): void
|
|
{
|
|
$this->assertSame('uuid', (new Institution)->getRouteKeyName());
|
|
}
|
|
|
|
public function test_attributes_are_cast(): void
|
|
{
|
|
$institution = Institution::factory()->create([
|
|
'is_storage_place' => 1,
|
|
'lat' => '55.700000',
|
|
'lon' => '13.190000',
|
|
'legacy_id' => '42',
|
|
]);
|
|
|
|
$fresh = $institution->fresh();
|
|
|
|
$this->assertIsBool($fresh->is_storage_place);
|
|
$this->assertIsFloat($fresh->lat);
|
|
$this->assertIsFloat($fresh->lon);
|
|
$this->assertSame(42, $fresh->legacy_id);
|
|
}
|
|
|
|
public function test_it_belongs_to_a_category(): void
|
|
{
|
|
$category = InstitutionCategory::factory()->create();
|
|
$institution = Institution::factory()->create(['category_id' => $category->id]);
|
|
|
|
$this->assertTrue($institution->category->is($category));
|
|
}
|
|
|
|
public function test_a_category_has_many_institutions(): void
|
|
{
|
|
$category = InstitutionCategory::factory()->create();
|
|
Institution::factory()->count(2)->create(['category_id' => $category->id]);
|
|
|
|
$this->assertCount(2, $category->institutions);
|
|
}
|
|
|
|
public function test_it_has_many_links(): void
|
|
{
|
|
$institution = Institution::factory()->create();
|
|
InstitutionLink::factory()->create(['institution_id' => $institution->id]);
|
|
|
|
$this->assertCount(1, $institution->refresh()->links);
|
|
}
|
|
|
|
public function test_it_has_many_affiliations(): void
|
|
{
|
|
$institution = Institution::factory()->create();
|
|
UserAffiliation::factory()->create(['institution_id' => $institution->id]);
|
|
|
|
$this->assertCount(1, $institution->refresh()->affiliations);
|
|
}
|
|
|
|
public function test_it_is_soft_deleted(): void
|
|
{
|
|
$institution = Institution::factory()->create();
|
|
|
|
$institution->delete();
|
|
|
|
$this->assertSoftDeleted($institution);
|
|
$this->assertSame(0, Institution::count());
|
|
$this->assertSame(1, Institution::withTrashed()->count());
|
|
}
|
|
}
|