Write tests - Write tests for what was done to this point that will be kept #11
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
|
@ -40,4 +41,16 @@ class Ensemble extends Model
|
|||
{
|
||||
return $this->hasMany(Seat::class);
|
||||
}
|
||||
|
||||
public function scopeForAudition(Builder $query, $audition_id): Builder
|
||||
{
|
||||
$audition = Audition::find($audition_id);
|
||||
// get instances of this class where the event_id is equal to the event_id of the audition with $audition_id
|
||||
return $query->where('event_id', $audition->event_id);
|
||||
}
|
||||
|
||||
public function scopeForEvent(Builder $query, $event_id): Builder
|
||||
{
|
||||
return $query->where('event_id', $event_id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ class AuditionFactory extends Factory
|
|||
return [
|
||||
'event_id' => $event->id,
|
||||
'name' => $this->faker->randomElement($instruments).$this->faker->randomNumber(3),
|
||||
'score_order' => 1,
|
||||
'score_order' => $this->faker->numberBetween(2, 50),
|
||||
'entry_deadline' => Carbon::tomorrow(),
|
||||
'entry_fee' => 1000,
|
||||
'minimum_grade' => 7,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Ensemble>
|
||||
*/
|
||||
class EnsembleFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->faker->name,
|
||||
'event_id' => \App\Models\Event::factory(),
|
||||
'code' => $this->faker->randomLetter().$this->faker->randomLetter,
|
||||
'rank' => $this->faker->numberBetween(1, 10),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
use App\Models\Audition;
|
||||
use App\Models\Ensemble;
|
||||
use App\Models\Entry;
|
||||
use App\Models\Event;
|
||||
use App\Models\Seat;
|
||||
use App\Models\SeatingLimit;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('has an event', function () {
|
||||
$event = Event::factory()->create();
|
||||
$ensemble = Ensemble::factory()->create(['event_id' => $event->id]);
|
||||
expect($ensemble->event)->toBeInstanceOf(Event::class)
|
||||
->and($ensemble->event->name)->toBe($event->name);
|
||||
});
|
||||
|
||||
it('has auditions in score order', function () {
|
||||
// Arrange
|
||||
$ensemble = Ensemble::factory()->create();
|
||||
$oboe = Audition::factory()->create(['event_id' => $ensemble->event_id, 'name' => 'Oboe', 'score_order' => 2]);
|
||||
$flute = Audition::factory()->create(['event_id' => $ensemble->event_id, 'name' => 'Flute', 'score_order' => 1]);
|
||||
$clarinet = Audition::factory()->create(['event_id' => $ensemble->event_id, 'name' => 'Clarinet',
|
||||
'score_order' => 3,
|
||||
]);
|
||||
// Act & Assert
|
||||
expect($ensemble->auditions)->toHaveCount(3)
|
||||
->sequence(
|
||||
fn ($seatingLimit) => $seatingLimit->audition_id === $flute->id,
|
||||
fn ($seatingLimit) => $seatingLimit->audition_id === $oboe->id,
|
||||
fn ($seatingLimit) => $seatingLimit->audition_id === $clarinet->id,
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
it('has seating limits',
|
||||
function () {
|
||||
// Arrange
|
||||
$ensemble = Ensemble::factory()->create();
|
||||
$auditions = Audition::factory()->count(3)->create(['event_id' => $ensemble->event_id]);
|
||||
foreach ($auditions as $audition) {
|
||||
SeatingLimit::create([
|
||||
'ensemble_id' => $ensemble->id, 'audition_id' => $audition->id,
|
||||
'maximum_accepted' => fake()->numberBetween(1, 10),
|
||||
]);
|
||||
}
|
||||
// Act & Assert
|
||||
expect($ensemble->seatingLimits)->toHaveCount(3);
|
||||
});
|
||||
|
||||
it('has seats', function () {
|
||||
// Arrange
|
||||
$ensemble = Ensemble::factory()->create();
|
||||
$audition = Audition::factory()->create(['event_id' => $ensemble->event_id]);
|
||||
$entry = Entry::factory()->create(['audition_id' => $audition->id]);
|
||||
$seat = Seat::create([
|
||||
'ensemble_id' => $ensemble->id,
|
||||
'audition_id' => $audition->id,
|
||||
'seat' => 1,
|
||||
'entry_id' => $entry->id,
|
||||
]);
|
||||
// Act & Assert
|
||||
expect($ensemble->seats)->toHaveCount(1)
|
||||
->and($ensemble->seats->first()->seat)->toBe(1);
|
||||
|
||||
});
|
||||
|
||||
it('returns only ensembles for a given audition in the audition scope', function () {
|
||||
// Arrange
|
||||
$audition = Audition::factory()->create();
|
||||
$ensemble = Ensemble::factory()->create(['event_id' => $audition->event_id]);
|
||||
$ensemble2 = Ensemble::factory()->create(['event_id' => $audition->event_id]);
|
||||
$ensemble3 = Ensemble::factory()->create();
|
||||
// Act & Assert
|
||||
expect(Ensemble::forAudition($audition->id)->get())->toHaveCount(2);
|
||||
});
|
||||
|
||||
it('returns only ensembles for a given event in the event scope', function () {
|
||||
// Arrange
|
||||
$event = Event::factory()->create();
|
||||
$ensemble = Ensemble::factory()->create(['event_id' => $event->id]);
|
||||
$ensemble2 = Ensemble::factory()->create(['event_id' => $event->id]);
|
||||
$ensemble3 = Ensemble::factory()->create();
|
||||
// Act & Assert
|
||||
expect(Ensemble::forEvent($event->id)->get())->toHaveCount(2);
|
||||
|
||||
});
|
||||
Loading…
Reference in New Issue