76 lines
2.2 KiB
PHP
76 lines
2.2 KiB
PHP
<?php
|
|
|
|
use App\Models\School;
|
|
use App\Models\Student;
|
|
use App\Rules\UniqueFullNameAtSchool;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
describe('UniqueFullNameAtSchool validation rule', function () {
|
|
it('fails validation when student with same name exists at school', function () {
|
|
// Arrange
|
|
$school = School::factory()->create();
|
|
Student::factory()->create([
|
|
'first_name' => 'John',
|
|
'last_name' => 'Doe',
|
|
'school_id' => $school->id,
|
|
]);
|
|
|
|
$rule = new UniqueFullNameAtSchool('John', 'Doe', $school->id);
|
|
$fails = false;
|
|
|
|
// Act
|
|
$rule->validate('name', 'value', function ($message) use (&$fails) {
|
|
$fails = true;
|
|
});
|
|
|
|
// Assert
|
|
expect($fails)->toBeTrue()
|
|
->and($rule->message())->toBe('There is already a student with that name at the school you are trying to add them to');
|
|
});
|
|
|
|
it('passes validation when no student with same name exists at school', function () {
|
|
// Arrange
|
|
$school = School::factory()->create();
|
|
Student::factory()->create([
|
|
'first_name' => 'Jane',
|
|
'last_name' => 'Doe',
|
|
'school_id' => $school->id,
|
|
]);
|
|
|
|
$rule = new UniqueFullNameAtSchool('John', 'Doe', $school->id);
|
|
$fails = false;
|
|
|
|
// Act
|
|
$rule->validate('name', 'value', function ($message) use (&$fails) {
|
|
$fails = true;
|
|
});
|
|
|
|
// Assert
|
|
expect($fails)->toBeFalse();
|
|
});
|
|
|
|
it('passes validation when student with same name exists at different school', function () {
|
|
// Arrange
|
|
$school1 = School::factory()->create();
|
|
$school2 = School::factory()->create();
|
|
Student::factory()->create([
|
|
'first_name' => 'John',
|
|
'last_name' => 'Doe',
|
|
'school_id' => $school1->id,
|
|
]);
|
|
|
|
$rule = new UniqueFullNameAtSchool('John', 'Doe', $school2->id);
|
|
$fails = false;
|
|
|
|
// Act
|
|
$rule->validate('name', 'value', function ($message) use (&$fails) {
|
|
$fails = true;
|
|
});
|
|
|
|
// Assert
|
|
expect($fails)->toBeFalse();
|
|
});
|
|
});
|