From fd50852ada4d90e044e6067d70f832531b11a112 Mon Sep 17 00:00:00 2001 From: aaron Date: Sun, 21 Apr 2024 01:39:55 -0500 Subject: [PATCH] test: add tests for aggregate model and forUser scopes --- database/factories/AggregateViewFactory.php | 25 ++++++ tests/Unit/AggregateViewModelTest.php | 85 +++++++++++++++++++++ tests/Unit/DailyViewModelTest.php | 26 +++++++ tests/Unit/PresentationModelTest.php | 22 ++++++ 4 files changed, 158 insertions(+) create mode 100644 database/factories/AggregateViewFactory.php create mode 100644 tests/Unit/AggregateViewModelTest.php diff --git a/database/factories/AggregateViewFactory.php b/database/factories/AggregateViewFactory.php new file mode 100644 index 0000000..df95ea6 --- /dev/null +++ b/database/factories/AggregateViewFactory.php @@ -0,0 +1,25 @@ + + */ +class AggregateViewFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'adhoc_slug' => fake()->optional()->slug(), + 'presentation_id' => fake()->boolean() ? Presentation::factory() : null, + ]; + } +} diff --git a/tests/Unit/AggregateViewModelTest.php b/tests/Unit/AggregateViewModelTest.php new file mode 100644 index 0000000..dd29134 --- /dev/null +++ b/tests/Unit/AggregateViewModelTest.php @@ -0,0 +1,85 @@ +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); + }); +}); diff --git a/tests/Unit/DailyViewModelTest.php b/tests/Unit/DailyViewModelTest.php index 691b404..ebac4fc 100644 --- a/tests/Unit/DailyViewModelTest.php +++ b/tests/Unit/DailyViewModelTest.php @@ -1,6 +1,7 @@ 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); + }); +}); diff --git a/tests/Unit/PresentationModelTest.php b/tests/Unit/PresentationModelTest.php index af3f570..6d9015a 100644 --- a/tests/Unit/PresentationModelTest.php +++ b/tests/Unit/PresentationModelTest.php @@ -1,6 +1,7 @@ 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); + }); +});