add stickers logic

This commit is contained in:
root
2026-03-10 16:40:11 -04:00
parent 3bf4c687da
commit 2e9a391280
17 changed files with 1047 additions and 390 deletions
@@ -0,0 +1,156 @@
<?php
namespace Tests\Feature\Api\V1\Reports;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class StickersControllerTest extends TestCase
{
use RefreshDatabase;
public function test_form_data_returns_classes_students_and_presets(): void
{
$this->seedConfig();
$user = $this->seedUser();
$this->seedClassAndStudent();
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/reports/stickers/form-data?school_year=2025-2026');
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertNotEmpty($response->json('data.classes'));
$this->assertNotEmpty($response->json('data.students'));
$this->assertNotEmpty($response->json('data.presets'));
$this->assertArrayHasKey('value', $response->json('data.presets.0'));
}
public function test_students_by_class_returns_students(): void
{
$this->seedConfig();
$user = $this->seedUser();
$this->seedClassAndStudent();
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/reports/stickers/students?class_section_id=200&school_year=2025-2026');
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertNotEmpty($response->json('data.students'));
}
public function test_print_returns_pdf(): void
{
$this->seedConfig();
$user = $this->seedUser();
$this->seedClassAndStudent();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/reports/stickers/print', [
'student_id' => 100,
'school_year' => '2025-2026',
'sticker_size' => '102x34',
]);
$response->assertOk();
$this->assertSame('application/pdf', $response->headers->get('content-type'));
}
public function test_print_validation_rejects_missing_selection(): void
{
$this->seedConfig();
$user = $this->seedUser();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/reports/stickers/print', [
'school_year' => '2025-2026',
]);
$response->assertStatus(422);
$response->assertJsonPath('message', 'Validation failed.');
}
public function test_requires_authentication(): void
{
$response = $this->getJson('/api/v1/reports/stickers/form-data');
$response->assertStatus(401);
}
private function seedConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
}
private function seedUser(): User
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '9999999999',
'email' => 'stickers@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
return User::query()->findOrFail($userId);
}
private function seedClassAndStudent(): void
{
DB::table('classes')->insert([
'id' => 1,
'class_name' => 'Grade 1',
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('classSection')->insert([
'class_section_id' => 200,
'class_section_name' => 'Grade 1',
'class_id' => 1,
'semester' => 'Fall',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('students')->insert([
'id' => 100,
'school_id' => 1,
'firstname' => 'Student',
'lastname' => 'One',
'parent_id' => 10,
'semester' => 'Fall',
'school_year' => '2025-2026',
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('student_class')->insert([
'student_id' => 100,
'class_section_id' => 200,
'school_year' => '2025-2026',
'semester' => 'Fall',
'is_event_only' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
}
}
@@ -0,0 +1,20 @@
<?php
namespace Tests\Unit\Services\Reports\Stickers;
use App\Services\Reports\Stickers\StickerPresetService;
use Tests\TestCase;
class StickerPresetServiceTest extends TestCase
{
public function test_presets_include_alias_fields(): void
{
$service = new StickerPresetService();
$presets = $service->presets();
$this->assertNotEmpty($presets);
$this->assertArrayHasKey('title', $presets[0]);
$this->assertArrayHasKey('text', $presets[0]);
$this->assertArrayHasKey('per_page', $presets[0]);
}
}
@@ -0,0 +1,83 @@
<?php
namespace Tests\Unit\Services\Reports\Stickers;
use App\Services\Reports\Stickers\StickerContextService;
use App\Services\Reports\Stickers\StickerPrintService;
use App\Services\Reports\Stickers\StickerQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class StickerPrintServiceTest extends TestCase
{
use RefreshDatabase;
public function test_generate_returns_pdf_content(): void
{
$this->seedData();
$service = new StickerPrintService(new StickerQueryService(new StickerContextService()));
$result = $service->generate([
'student_id' => 100,
'school_year' => '2025-2026',
'sticker_size' => '102x34',
]);
$this->assertTrue($result['ok']);
$this->assertNotEmpty($result['pdf']);
}
public function test_generate_returns_error_when_no_students(): void
{
$service = new StickerPrintService(new StickerQueryService(new StickerContextService()));
$result = $service->generate([
'class_section_id' => 999,
'school_year' => '2025-2026',
]);
$this->assertFalse($result['ok']);
$this->assertSame('No students found in this class.', $result['message']);
}
private function seedData(): void
{
DB::table('classes')->insert([
['id' => 1, 'class_name' => 'Grade 1', 'created_at' => now(), 'updated_at' => now()],
]);
DB::table('classSection')->insert([
'class_section_id' => 200,
'class_section_name' => 'Grade 1',
'class_id' => 1,
'semester' => 'Fall',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('students')->insert([
'id' => 100,
'school_id' => 1,
'firstname' => 'Student',
'lastname' => 'One',
'parent_id' => 10,
'semester' => 'Fall',
'school_year' => '2025-2026',
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('student_class')->insert([
'student_id' => 100,
'class_section_id' => 200,
'school_year' => '2025-2026',
'semester' => 'Fall',
'is_event_only' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
}
}
@@ -0,0 +1,117 @@
<?php
namespace Tests\Unit\Services\Reports\Stickers;
use App\Services\Reports\Stickers\StickerContextService;
use App\Services\Reports\Stickers\StickerQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class StickerQueryServiceTest extends TestCase
{
use RefreshDatabase;
public function test_list_students_by_class_returns_rows(): void
{
$this->seedData();
$service = new StickerQueryService(new StickerContextService());
$students = $service->listStudentsByClass(200, '2025-2026');
$this->assertCount(1, $students);
$this->assertSame(100, $students[0]['id']);
}
public function test_list_students_for_print_all_excludes_youth_and_kg(): void
{
$this->seedData();
$this->seedYouthClass();
$service = new StickerQueryService(new StickerContextService());
$students = $service->listStudentsForPrintAll('2025-2026');
$this->assertCount(1, $students);
$this->assertSame('Grade 1', $students[0]['class_section_name']);
}
private function seedData(): void
{
DB::table('classes')->insert([
['id' => 1, 'class_name' => 'Grade 1', 'created_at' => now(), 'updated_at' => now()],
]);
DB::table('classSection')->insert([
'class_section_id' => 200,
'class_section_name' => 'Grade 1',
'class_id' => 1,
'semester' => 'Fall',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('students')->insert([
'id' => 100,
'school_id' => 1,
'firstname' => 'Student',
'lastname' => 'One',
'parent_id' => 10,
'semester' => 'Fall',
'school_year' => '2025-2026',
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('student_class')->insert([
'student_id' => 100,
'class_section_id' => 200,
'school_year' => '2025-2026',
'semester' => 'Fall',
'is_event_only' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
}
private function seedYouthClass(): void
{
DB::table('classes')->insert([
['id' => 2, 'class_name' => 'Youth', 'created_at' => now(), 'updated_at' => now()],
]);
DB::table('classSection')->insert([
'class_section_id' => 201,
'class_section_name' => 'Youth',
'class_id' => 2,
'semester' => 'Fall',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('students')->insert([
'id' => 101,
'school_id' => 1,
'firstname' => 'Youth',
'lastname' => 'Student',
'parent_id' => 11,
'semester' => 'Fall',
'school_year' => '2025-2026',
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('student_class')->insert([
'student_id' => 101,
'class_section_id' => 201,
'school_year' => '2025-2026',
'semester' => 'Fall',
'is_event_only' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
}
}