-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for aggregate model and forUser scopes
- Loading branch information
1 parent
aabe897
commit fd50852
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters