fix enrollment for the new school-year
Tests / PHPUnit (push) Successful in 1m26s

This commit is contained in:
root
2026-08-07 23:43:31 -04:00
parent b6f3b14e7b
commit 1ef2800f12
66 changed files with 8997 additions and 498 deletions
@@ -12,9 +12,14 @@ use Config\App;
final class SchoolYearContextRequest extends MockIncomingRequest
{
public function __construct(private readonly array $gets = [])
public function __construct(
private readonly array $gets = [],
private readonly array $posts = [],
string $method = 'GET'
)
{
parent::__construct(config(App::class), new URI('https://test.alrahmaisgl.org'), 'php://input', new UserAgent());
$this->setMethod($method);
}
public function getGet($key = null, $filter = null, $default = null)
@@ -25,6 +30,15 @@ final class SchoolYearContextRequest extends MockIncomingRequest
return $this->gets[$key] ?? $default;
}
public function getPost($index = null, $filter = null, $flags = null)
{
if ($index === null) {
return $this->posts;
}
return $this->posts[$index] ?? null;
}
}
final class SchoolYearContextFakeModel extends SchoolYearModel
@@ -79,6 +93,11 @@ final class SchoolYearContextFakeModel extends SchoolYearModel
return $limit !== null ? array_slice($rows, $offset, $limit) : array_slice($rows, $offset);
}
public function first()
{
return $this->findAll(1)[0] ?? null;
}
}
final class SchoolYearContextServiceTest extends CIUnitTestCase
@@ -110,6 +129,56 @@ final class SchoolYearContextServiceTest extends CIUnitTestCase
$this->assertSame(1, session()->get('selected_school_year_id'));
}
public function testPostSchoolYearNameTakesPrecedenceOverActiveYearForWrites(): void
{
$service = new SchoolYearContextService(new SchoolYearContextFakeModel([
1 => ['id' => 1, 'name' => '2025-2026', 'status' => 'closed'],
2 => ['id' => 2, 'name' => '2026-2027', 'status' => 'active'],
]));
$context = $service->resolve(new SchoolYearContextRequest(
posts: ['school_year' => '2025-2026'],
method: 'POST'
));
$this->assertSame(1, $context->id());
$this->assertSame('2025-2026', $context->yearName());
$this->assertTrue($context->isReadonly());
$this->assertTrue($context->isExplicitSelection());
}
public function testPostSchoolYearIdTakesPrecedenceOverActiveYearForWrites(): void
{
$service = new SchoolYearContextService(new SchoolYearContextFakeModel([
1 => ['id' => 1, 'name' => '2025-2026', 'status' => 'closed'],
2 => ['id' => 2, 'name' => '2026-2027', 'status' => 'active'],
]));
$context = $service->resolve(new SchoolYearContextRequest(
posts: ['school_year_id' => '1'],
method: 'POST'
));
$this->assertSame(1, $context->id());
$this->assertSame('2025-2026', $context->yearName());
$this->assertTrue($context->isReadonly());
}
public function testCanResolveContextFromStoredSchoolYearName(): void
{
$service = new SchoolYearContextService(new SchoolYearContextFakeModel([
1 => ['id' => 1, 'name' => '2025-2026', 'status' => 'closed'],
2 => ['id' => 2, 'name' => '2026-2027', 'status' => 'active'],
]));
$context = $service->forYearName('2025-2026');
$this->assertSame(1, $context->id());
$this->assertSame('2025-2026', $context->yearName());
$this->assertTrue($context->isReadonly());
$this->assertTrue($context->isExplicitSelection());
}
public function testInvalidSessionSelectionFallsBackToActiveYearAndClearsSession(): void
{
session()->set('selected_school_year_id', 99);