mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-11 17:52:15 +01:00
Add tests for export endpoints
This commit is contained in:
committed by
Constantin Graf
parent
5a1e05374c
commit
9a60e2b911
@@ -6,6 +6,7 @@ namespace Tests\Unit\Model;
|
||||
|
||||
use App\Models\Organization;
|
||||
use App\Models\Tag;
|
||||
use App\Models\TimeEntry;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\UsesClass;
|
||||
|
||||
@@ -17,14 +18,39 @@ class TagModelTest extends ModelTestAbstract
|
||||
{
|
||||
// Arrange
|
||||
$organization = Organization::factory()->create();
|
||||
$task = Tag::factory()->forOrganization($organization)->create();
|
||||
$tag = Tag::factory()->forOrganization($organization)->create();
|
||||
|
||||
// Act
|
||||
$task->refresh();
|
||||
$organizationRel = $task->organization;
|
||||
$tag->refresh();
|
||||
$organizationRel = $tag->organization;
|
||||
|
||||
// Assert
|
||||
$this->assertNotNull($organizationRel);
|
||||
$this->assertTrue($organizationRel->is($organization));
|
||||
}
|
||||
|
||||
public function test_it_has_many_time_entries_via_json_field(): void
|
||||
{
|
||||
// Arrange
|
||||
$organization = Organization::factory()->create();
|
||||
$tag1 = Tag::factory()->forOrganization($organization)->create();
|
||||
$tag2 = Tag::factory()->forOrganization($organization)->create();
|
||||
$timeEntry1 = TimeEntry::factory()->forOrganization($organization)->create([
|
||||
'tags' => [$tag1->id, $tag2->id],
|
||||
]);
|
||||
$timeEntry2 = TimeEntry::factory()->forOrganization($organization)->create([
|
||||
'tags' => [$tag1->id],
|
||||
]);
|
||||
$timeEntry3 = TimeEntry::factory()->forOrganization($organization)->create([
|
||||
'tags' => [$tag2->id],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$tag1->refresh();
|
||||
$timeEntries = $tag1->timeEntries;
|
||||
|
||||
// Assert
|
||||
$this->assertCount(2, $timeEntries);
|
||||
$this->assertEqualsCanonicalizing([$timeEntry1->getKey(), $timeEntry2->getKey()], $timeEntries->pluck('id')->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,4 +179,50 @@ class TimeEntryModelTest extends ModelTestAbstract
|
||||
// Assert
|
||||
$this->assertSame($project->client_id, $clientId);
|
||||
}
|
||||
|
||||
public function test_has_many_tags_via_json_relation(): void
|
||||
{
|
||||
// Arrange
|
||||
$tag1 = Tag::factory()->create();
|
||||
$tag2 = Tag::factory()->create();
|
||||
$timeEntry = TimeEntry::factory()->create([
|
||||
'tags' => [$tag1->getKey(), $tag2->getKey()],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$timeEntry->refresh();
|
||||
$tags = $timeEntry->tagsRelation;
|
||||
|
||||
// Assert
|
||||
$this->assertCount(2, $tags);
|
||||
$this->assertTrue($tags->contains($tag1));
|
||||
$this->assertTrue($tags->contains($tag2));
|
||||
}
|
||||
|
||||
public function test_has_many_tags_via_json_relation_eager_loaded(): void
|
||||
{
|
||||
// Arrange
|
||||
$tag1 = Tag::factory()->create();
|
||||
$tag2 = Tag::factory()->create();
|
||||
$timeEntry1 = TimeEntry::factory()->create([
|
||||
'tags' => [$tag1->getKey(), $tag2->getKey()],
|
||||
'created_at' => Carbon::now()->subDay(),
|
||||
]);
|
||||
$timeEntry2 = TimeEntry::factory()->create([
|
||||
'tags' => [$tag1->getKey()],
|
||||
'created_at' => Carbon::now()->subDays(2),
|
||||
]);
|
||||
|
||||
// Act
|
||||
$timeEntries = TimeEntry::with('tagsRelation')->orderBy('created_at', 'desc')->get();
|
||||
$tags1 = $timeEntries->get(0)->tagsRelation;
|
||||
$tags2 = $timeEntries->get(1)->tagsRelation;
|
||||
|
||||
// Assert
|
||||
$this->assertCount(2, $tags1);
|
||||
$this->assertTrue($tags1->contains($tag1));
|
||||
$this->assertTrue($tags1->contains($tag2));
|
||||
$this->assertCount(1, $tags2);
|
||||
$this->assertTrue($tags2->contains($tag1));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user