Skip to content

Commit

Permalink
test: add tests for aggregate model and forUser scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
alkrauss48 committed Apr 21, 2024
1 parent aabe897 commit fd50852
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
25 changes: 25 additions & 0 deletions database/factories/AggregateViewFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Database\Factories;

use App\Models\Presentation;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\AggregateView>
*/
class AggregateViewFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'adhoc_slug' => fake()->optional()->slug(),
'presentation_id' => fake()->boolean() ? Presentation::factory() : null,
];
}
}
85 changes: 85 additions & 0 deletions tests/Unit/AggregateViewModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

use App\Models\AggregateView;
use App\Models\Presentation;
use App\Models\User;

describe('Instructions', function () {
test('Aggregate views return true for instructions', function () {
$view = AggregateView::factory()->create([
'presentation_id' => null,
'adhoc_slug' => null,
]);

expect($view)->isInstructions->toBeTrue();
});

test('Aggregate views return false for instructions, because it\'s adhoc', function () {
$view = AggregateView::factory()->create([
'adhoc_slug' => 'abc',
]);

expect($view)->isInstructions->toBeFalse();
});

test('Aggregate views return false for instructions, because it\'s a presentation', function () {
$view = AggregateView::factory()->create([
'presentation_id' => Presentation::factory(),
]);

expect($view)->isInstructions->toBeFalse();
});
});

describe('Adhoc', function () {
test('Aggregate views return true for adhoc', function () {
$view = AggregateView::factory()->create([
'presentation_id' => null,
'adhoc_slug' => 'abc',
]);

expect($view)->isAdhoc->toBeTrue();
});

test('Aggregate views return false for adhoc, because it\'s instructions', function () {
$view = AggregateView::factory()->create([
'presentation_id' => null,
'adhoc_slug' => null,
]);

expect($view)->isAdhoc->toBeFalse();
});

test('Aggregate views return false for adhoc, because it\'s a presentation', function () {
$view = AggregateView::factory()->create([
'presentation_id' => Presentation::factory(),
]);

expect($view)->isAdhoc->toBeFalse();
});
});

describe('forUser', function () {
beforeEach(function () {
$this->admin = User::factory()->admin()->create();
$this->user = User::factory()->hasPresentations(1)->create();

AggregateView::factory()->count(10)->create();

AggregateView::factory()->count(2)->create([
'presentation_id' => $this->user->presentations()->first()->id,
]);
});

test('Admins can see all aggregate views', function () {
$this->actingAs($this->admin);

expect(AggregateView::forUser()->count())->toBe(12);
});

test('Users can see only their aggregate views', function () {
$this->actingAs($this->user);

expect(AggregateView::forUser()->count())->toBe(2);
});
});
26 changes: 26 additions & 0 deletions tests/Unit/DailyViewModelTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Models\DailyView;
use App\Models\User;

test('Daily views can get created for adhoc slugs', function () {
$view = DailyView::createForAdhocPresentation('abc123');
Expand All @@ -15,3 +16,28 @@
expect($view)
->adhoc_slug->toBeNull();
});

describe('forUser', function () {
beforeEach(function () {
$this->admin = User::factory()->admin()->create();
$this->user = User::factory()->hasPresentations(1)->create();

DailyView::factory()->count(10)->create();

DailyView::factory()->count(2)->create([
'presentation_id' => $this->user->presentations()->first()->id,
]);
});

test('Admins can see all daily views', function () {
$this->actingAs($this->admin);

expect(DailyView::forUser()->count())->toBe(12);
});

test('Users can see only their daily views', function () {
$this->actingAs($this->user);

expect(DailyView::forUser()->count())->toBe(2);
});
});
22 changes: 22 additions & 0 deletions tests/Unit/PresentationModelTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Models\Presentation;
use App\Models\User;
use Illuminate\Support\Str;

test('Presentation automatically sets slug on create', function () {
Expand Down Expand Up @@ -30,3 +31,24 @@
expect($view)
->presentation_id->toBe($presentation->id);
});

describe('forUser', function () {
beforeEach(function () {
$this->admin = User::factory()->admin()->create();
$this->user = User::factory()->hasPresentations(2)->create();

Presentation::factory()->count(10)->create();
});

test('Admins can see all presentations', function () {
$this->actingAs($this->admin);

expect(Presentation::forUser()->count())->toBe(12);
});

test('Users can see only their presentations', function () {
$this->actingAs($this->user);

expect(Presentation::forUser()->count())->toBe(2);
});
});

0 comments on commit fd50852

Please # to comment.