83 lines
2.7 KiB
PHP
83 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Services;
|
|
|
|
use App\Services\AssignmentService;
|
|
use Mockery;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class AssignmentServiceTest extends TestCase
|
|
{
|
|
protected function tearDown(): void
|
|
{
|
|
Mockery::close();
|
|
parent::tearDown();
|
|
}
|
|
|
|
/** @test */
|
|
public function save_student_assignment_uses_config_defaults_and_calls_update_or_create(): void
|
|
{
|
|
$userModel = Mockery::mock();
|
|
$studentModel = Mockery::mock();
|
|
$classSectionModel = Mockery::mock();
|
|
$teacherClassModel = Mockery::mock();
|
|
|
|
$configModel = Mockery::mock();
|
|
$studentClassModel = Mockery::mock();
|
|
$studentClassQuery = Mockery::mock();
|
|
|
|
// Config defaults
|
|
$configModel->shouldReceive('getConfig')->with('semester')->andReturn('Fall');
|
|
$configModel->shouldReceive('getConfig')->with('school_year')->andReturn('2025-2026');
|
|
|
|
// Eloquent chain: newQuery()->updateOrCreate(...)
|
|
$studentClassModel->shouldReceive('newQuery')->once()->andReturn($studentClassQuery);
|
|
|
|
$expectedRecord = (object) [
|
|
'id' => 99,
|
|
'student_id' => 10,
|
|
'class_section_id' => 7,
|
|
'semester' => 'Fall',
|
|
'school_year' => '2025-2026',
|
|
'description' => 'Auto-assigned',
|
|
'updated_by' => 55,
|
|
];
|
|
|
|
$studentClassQuery->shouldReceive('updateOrCreate')
|
|
->once()
|
|
->with(
|
|
[
|
|
'student_id' => 10,
|
|
'school_year' => '2025-2026',
|
|
'semester' => 'Fall',
|
|
],
|
|
Mockery::on(function ($payload) {
|
|
return $payload['student_id'] === 10
|
|
&& $payload['class_section_id'] === 7
|
|
&& $payload['semester'] === 'Fall'
|
|
&& $payload['school_year'] === '2025-2026'
|
|
&& $payload['description'] === 'Auto-assigned'
|
|
&& $payload['updated_by'] === 55;
|
|
})
|
|
)
|
|
->andReturn($expectedRecord);
|
|
|
|
$service = new AssignmentService(
|
|
$userModel,
|
|
$configModel,
|
|
$studentModel,
|
|
$classSectionModel,
|
|
$teacherClassModel,
|
|
$studentClassModel
|
|
);
|
|
|
|
$result = $service->saveStudentAssignment([
|
|
'student_id' => 10,
|
|
'class_section_id' => 7,
|
|
'description' => 'Auto-assigned',
|
|
// semester/school_year intentionally omitted to test fallback
|
|
], 55);
|
|
|
|
$this->assertSame($expectedRecord, $result);
|
|
}
|
|
} |