add more controllers and fix tests

This commit is contained in:
root
2026-03-09 11:54:13 -04:00
parent 0c3e9b16f7
commit 1cb3573d4b
74 changed files with 2761 additions and 2728 deletions
@@ -0,0 +1,79 @@
<?php
namespace Tests\Feature\Api\V1\Email;
use App\Models\User;
use App\Services\Email\EmailDispatchService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Mockery;
use Tests\TestCase;
class EmailControllerTest extends TestCase
{
use RefreshDatabase;
public function test_senders_endpoint_returns_options(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
putenv('MAIL_SENDERS={"general":{"name":"General","email":"general@example.com"}}');
$response = $this->getJson('/api/v1/email/senders');
$response->assertOk();
$response->assertJsonPath('ok', true);
$this->assertNotEmpty($response->json('senders'));
}
public function test_send_endpoint_uses_dispatch_service(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$mock = Mockery::mock(EmailDispatchService::class);
$mock->shouldReceive('send')
->once()
->andReturn(true);
$this->app->instance(EmailDispatchService::class, $mock);
$response = $this->postJson('/api/v1/email/send', [
'recipient' => 'test@example.com',
'subject' => 'Hello',
'html_message' => '<p>Test</p>',
'profile' => 'general',
]);
$response->assertOk();
$response->assertJsonPath('ok', true);
}
private function createUser(): User
{
DB::table('users')->insert([
'id' => 1,
'school_id' => 1,
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '5555555555',
'email' => 'admin@example.com',
'address_street' => '123 Main',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'is_verified' => 1,
'status' => 'Active',
'is_suspended' => 0,
'failed_attempts' => 0,
'password' => bcrypt('secret'),
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
return User::query()->findOrFail(1);
}
}
@@ -0,0 +1,86 @@
<?php
namespace Tests\Feature\Api\V1\Email;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class EmailExtractorControllerTest extends TestCase
{
use RefreshDatabase;
public function test_emails_endpoint_returns_users_and_parents(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('parents')->insert([
'secondparent_firstname' => 'Parent',
'secondparent_lastname' => 'Two',
'secondparent_email' => 'parent2@example.com',
'secondparent_phone' => '5555555555',
'firstparent_id' => 1,
'secondparent_id' => 2,
'school_year' => '2025-2026',
]);
$response = $this->getJson('/api/v1/email-extractor/emails');
$response->assertOk();
$response->assertJsonPath('ok', true);
$this->assertContains('parent2@example.com', $response->json('emails.parents'));
}
public function test_compare_endpoint_returns_comparison(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('parents')->insert([
'secondparent_firstname' => 'Parent',
'secondparent_lastname' => 'Two',
'secondparent_email' => 'parent2@example.com',
'secondparent_phone' => '5555555555',
'firstparent_id' => 1,
'secondparent_id' => 2,
'school_year' => '2025-2026',
]);
$response = $this->postJson('/api/v1/email-extractor/compare', [
'csv_emails' => ['parent2@example.com', 'missing@example.com'],
]);
$response->assertOk();
$response->assertJsonPath('ok', true);
$this->assertContains('parent2@example.com', $response->json('comparison.existed'));
}
private function createUser(): User
{
DB::table('users')->insert([
'id' => 1,
'school_id' => 1,
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '5555555555',
'email' => 'admin@example.com',
'address_street' => '123 Main',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'is_verified' => 1,
'status' => 'Active',
'is_suspended' => 0,
'failed_attempts' => 0,
'password' => bcrypt('secret'),
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
return User::query()->findOrFail(1);
}
}
@@ -0,0 +1,123 @@
<?php
namespace Tests\Feature\Api\V1\Finance;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class FeeCalculationControllerTest extends TestCase
{
use RefreshDatabase;
public function test_refund_endpoint_returns_amount(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$this->seedConfig();
$this->seedClassSection();
DB::table('payments')->insert([
'id' => 1,
'parent_id' => 10,
'paid_amount' => 200.00,
'total_amount' => 200.00,
'balance' => 0.00,
'number_of_installments' => 1,
'payment_date' => '2025-01-10',
'school_year' => '2025-2026',
'status' => 'Paid',
]);
$response = $this->postJson('/api/v1/finance/fees/refund', [
'parent_id' => 10,
'students' => [
[
'student_id' => 1,
'class_section_id' => 101,
'enrollment_status' => 'withdrawn',
'admission_status' => 'accepted',
'withdrawal_date' => '2025-01-15',
],
],
]);
$response->assertOk();
$response->assertJsonPath('ok', true);
$this->assertGreaterThan(0, (float) $response->json('refund.refund_amount'));
}
public function test_tuition_total_endpoint_returns_total(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$this->seedConfig();
$this->seedClassSection();
$response = $this->postJson('/api/v1/finance/fees/tuition-total', [
'students' => [
['class_section_id' => 101],
['class_section_id' => 101],
],
]);
$response->assertOk();
$response->assertJsonPath('ok', true);
$this->assertGreaterThan(0, (float) $response->json('tuition.total_tuition'));
}
private function seedConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'refund_deadline', 'config_value' => '2025-02-01'],
['config_key' => 'weeks_study', 'config_value' => '8'],
['config_key' => 'last_school_day', 'config_value' => '2025-06-01'],
['config_key' => 'first_student_fee', 'config_value' => '350'],
['config_key' => 'second_student_fee', 'config_value' => '200'],
['config_key' => 'youth_fee', 'config_value' => '180'],
]);
}
private function seedClassSection(): void
{
DB::table('classSection')->insert([
'id' => 1,
'class_id' => 1,
'class_section_id' => 101,
'class_section_name' => '3',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
}
private function createUser(): User
{
DB::table('users')->insert([
'id' => 1,
'school_id' => 1,
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '5555555555',
'email' => 'admin@example.com',
'address_street' => '123 Main',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'is_verified' => 1,
'status' => 'Active',
'is_suspended' => 0,
'failed_attempts' => 0,
'password' => bcrypt('secret'),
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
return User::query()->findOrFail(1);
}
}
@@ -0,0 +1,212 @@
<?php
namespace Tests\Feature\Api\V1\Finance;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class PurchaseOrderControllerTest extends TestCase
{
use RefreshDatabase;
public function test_index_returns_orders(): void
{
$this->seedUsers();
$supplierId = DB::table('suppliers')->insertGetId(['name' => 'Alpha Supplies']);
DB::table('purchase_orders')->insert([
'id' => 1,
'po_number' => 'PO-001',
'supplier_id' => $supplierId,
'status' => 'ordered',
'subtotal' => 25,
'tax' => 0,
'total' => 25,
'created_at' => now(),
'updated_at' => now(),
]);
Sanctum::actingAs(User::query()->findOrFail(1));
$response = $this->getJson('/api/v1/finance/purchase-orders?q=PO-001');
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertNotEmpty($response->json('orders'));
}
public function test_options_returns_suppliers_and_supplies(): void
{
$this->seedUsers();
DB::table('suppliers')->insert([
['id' => 1, 'name' => 'Alpha Supplies'],
['id' => 2, 'name' => 'Beta Supplies'],
]);
DB::table('supplies')->insert([
['id' => 1, 'name' => 'Paper', 'unit' => 'box', 'qty_on_hand' => 0],
['id' => 2, 'name' => 'Ink', 'unit' => 'each', 'qty_on_hand' => 5],
]);
Sanctum::actingAs(User::query()->findOrFail(1));
$response = $this->getJson('/api/v1/finance/purchase-orders/options');
$response->assertOk();
$response->assertJson(['ok' => true]);
$this->assertCount(2, $response->json('suppliers'));
$this->assertCount(2, $response->json('supplies'));
}
public function test_store_creates_purchase_order_and_items(): void
{
$this->seedUsers();
$supplierId = DB::table('suppliers')->insertGetId(['name' => 'Alpha Supplies']);
$supplyId = DB::table('supplies')->insertGetId(['name' => 'Paper', 'unit' => 'box', 'qty_on_hand' => 0]);
Sanctum::actingAs(User::query()->findOrFail(1));
$response = $this->postJson('/api/v1/finance/purchase-orders', [
'po_number' => 'PO-100',
'supplier_id' => $supplierId,
'order_date' => '2026-03-10',
'expected_date' => '2026-03-15',
'notes' => 'Initial order',
'items' => [
[
'supply_id' => $supplyId,
'description' => 'Paper for class',
'quantity' => 2,
'unit_cost' => 5,
],
],
]);
$response->assertStatus(201);
$response->assertJson(['ok' => true]);
$this->assertDatabaseHas('purchase_orders', [
'po_number' => 'PO-100',
'supplier_id' => $supplierId,
'subtotal' => 10,
'total' => 10,
]);
$this->assertDatabaseHas('purchase_order_items', [
'supply_id' => $supplyId,
'quantity' => 2,
'unit_cost' => 5,
]);
}
public function test_receive_updates_inventory(): void
{
$this->seedUsers();
$supplierId = DB::table('suppliers')->insertGetId(['name' => 'Alpha Supplies']);
$supplyId = DB::table('supplies')->insertGetId(['name' => 'Paper', 'unit' => 'box', 'qty_on_hand' => 1]);
$poId = DB::table('purchase_orders')->insertGetId([
'po_number' => 'PO-200',
'supplier_id' => $supplierId,
'status' => 'ordered',
'subtotal' => 20,
'tax' => 0,
'total' => 20,
'created_at' => now(),
'updated_at' => now(),
]);
$itemId = DB::table('purchase_order_items')->insertGetId([
'purchase_order_id' => $poId,
'supply_id' => $supplyId,
'description' => 'Paper for class',
'quantity' => 4,
'received_qty' => 0,
'unit_cost' => 5,
'created_at' => now(),
'updated_at' => now(),
]);
Sanctum::actingAs(User::query()->findOrFail(1));
$response = $this->postJson("/api/v1/finance/purchase-orders/{$poId}/receive", [
'items' => [
['id' => $itemId, 'quantity' => 4],
],
]);
$response->assertOk();
$response->assertJson(['ok' => true, 'completed' => true]);
$this->assertDatabaseHas('purchase_order_items', [
'id' => $itemId,
'received_qty' => 4,
]);
$this->assertDatabaseHas('supplies', [
'id' => $supplyId,
'qty_on_hand' => 5,
]);
$this->assertDatabaseHas('supply_transactions', [
'supply_id' => $supplyId,
'type' => 'in',
'quantity' => 4,
]);
}
public function test_cancel_marks_purchase_order(): void
{
$this->seedUsers();
$supplierId = DB::table('suppliers')->insertGetId(['name' => 'Alpha Supplies']);
$poId = DB::table('purchase_orders')->insertGetId([
'po_number' => 'PO-300',
'supplier_id' => $supplierId,
'status' => 'ordered',
'subtotal' => 0,
'tax' => 0,
'total' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
Sanctum::actingAs(User::query()->findOrFail(1));
$response = $this->postJson("/api/v1/finance/purchase-orders/{$poId}/cancel");
$response->assertOk();
$response->assertJson(['ok' => true, 'status' => 'canceled']);
$this->assertDatabaseHas('purchase_orders', [
'id' => $poId,
'status' => 'canceled',
]);
}
private function seedUsers(): void
{
DB::table('roles')->insert([
['id' => 1, 'name' => 'admin', 'slug' => 'admin', 'is_active' => 1],
]);
DB::table('users')->insert([
'id' => 1,
'school_id' => 1,
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '5555555555',
'email' => 'admin@example.com',
'address_street' => '123 Main',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'is_verified' => 1,
'status' => 'Active',
'is_suspended' => 0,
'failed_attempts' => 0,
'password' => bcrypt('secret'),
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
DB::table('user_roles')->insert([
['user_id' => 1, 'role_id' => 1],
]);
}
}
@@ -71,6 +71,8 @@ class QuizControllerTest extends TestCase
'class_section_id' => 1,
'class_section_name' => '1A',
'class_id' => 1,
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
DB::table('students')->insert([
@@ -79,6 +81,10 @@ class QuizControllerTest extends TestCase
'firstname' => 'Kid',
'lastname' => 'User',
'school_id' => 1,
'age' => 10,
'gender' => 'M',
'photo_consent' => 1,
'year_of_registration' => '2025',
'is_active' => 1,
]);
@@ -87,6 +93,7 @@ class QuizControllerTest extends TestCase
'class_section_id' => 1,
'school_year' => '2025-2026',
'is_event_only' => 0,
'semester' => 'Fall',
]);
}
}
@@ -47,7 +47,11 @@ class ScoreCommentControllerTest extends TestCase
'created_at' => now(),
]);
$response = $this->getJson('/api/v1/scores/comments?class_section_id=1&semester=Fall&school_year=2025-2026');
$response = $this->json('GET', '/api/v1/scores/comments', [
'class_section_id' => 1,
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
$response->assertOk();
$response->assertJson(['ok' => true]);
@@ -60,6 +64,11 @@ class ScoreCommentControllerTest extends TestCase
['id' => 1, 'name' => 'teacher', 'slug' => 'teacher', 'is_active' => 1],
]);
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
DB::table('users')->insert([
'id' => 1,
'school_id' => 1,
@@ -90,6 +99,8 @@ class ScoreCommentControllerTest extends TestCase
'class_section_id' => 1,
'class_section_name' => '1A',
'class_id' => 1,
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
DB::table('students')->insert([
@@ -98,6 +109,10 @@ class ScoreCommentControllerTest extends TestCase
'firstname' => 'Kid',
'lastname' => 'User',
'school_id' => 1,
'age' => 10,
'gender' => 'M',
'photo_consent' => 1,
'year_of_registration' => '2025',
'is_active' => 1,
]);
@@ -106,6 +121,7 @@ class ScoreCommentControllerTest extends TestCase
'class_section_id' => 1,
'school_year' => '2025-2026',
'is_event_only' => 0,
'semester' => 'Fall',
]);
}
}
@@ -24,7 +24,11 @@ class ScoreControllerTest extends TestCase
'semester' => 'Fall',
]);
$response = $this->getJson('/api/v1/scores/overview?class_section_id=1&semester=Fall&school_year=2025-2026');
$response = $this->json('GET', '/api/v1/scores/overview', [
'class_section_id' => 1,
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
$response->assertOk();
$response->assertJson(['ok' => true]);
@@ -116,7 +116,10 @@ class UserControllerTest extends TestCase
'school_year' => '2025-2026',
]);
$response = $this->getJson('/api/v1/users/login-activity?per_page=1&page=2');
$response = $this->json('GET', '/api/v1/users/login-activity', [
'per_page' => 1,
'page' => 2,
]);
$response->assertOk();
$response->assertJson(['ok' => true]);
@@ -0,0 +1,58 @@
<?php
namespace Tests\Unit\Services\Email;
use App\Services\Email\EmailExtractorService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class EmailExtractorServiceTest extends TestCase
{
use RefreshDatabase;
public function test_list_and_compare_emails(): void
{
DB::table('users')->insert([
'id' => 1,
'school_id' => 1,
'firstname' => 'User',
'lastname' => 'One',
'cellphone' => '5555555555',
'email' => 'user1@example.com',
'address_street' => '123 Main',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'is_verified' => 1,
'status' => 'Active',
'is_suspended' => 0,
'failed_attempts' => 0,
'password' => bcrypt('secret'),
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
DB::table('parents')->insert([
'secondparent_firstname' => 'Parent',
'secondparent_lastname' => 'Two',
'secondparent_email' => 'parent2@example.com',
'secondparent_phone' => '5555555555',
'firstparent_id' => 1,
'secondparent_id' => 2,
'school_year' => '2025-2026',
]);
$service = new EmailExtractorService();
$emails = $service->listEmails();
$this->assertContains('user1@example.com', $emails['users']);
$this->assertContains('parent2@example.com', $emails['parents']);
$result = $service->compare(['user1@example.com', 'missing@example.com']);
$this->assertContains('user1@example.com', $result['existed']);
$this->assertContains('parent2@example.com', $result['needToAdd']);
}
}
@@ -0,0 +1,33 @@
<?php
namespace Tests\Unit\Services\Email;
use App\Services\Email\EmailProfileService;
use Tests\TestCase;
class EmailProfileServiceTest extends TestCase
{
public function test_resolve_profile_maps_aliases(): void
{
$service = new EmailProfileService();
$this->assertSame('communication', $service->resolveProfile('comm'));
$this->assertSame('default', $service->resolveProfile('system'));
$this->assertSame('payment', $service->resolveProfile('payment'));
}
public function test_env_key_from_profile(): void
{
$service = new EmailProfileService();
$this->assertSame('PAYMENT_ALERTS', $service->envKeyFromProfile('payment alerts'));
}
public function test_sanitize_reply_to_name(): void
{
$service = new EmailProfileService();
$this->assertSame('Fallback', $service->sanitizeReplyToName('no-reply', 'Fallback'));
$this->assertSame('Support', $service->sanitizeReplyToName('Support', 'Fallback'));
}
}
@@ -0,0 +1,28 @@
<?php
namespace Tests\Unit\Services\Fees;
use App\Services\Fees\FeeGradeService;
use Tests\TestCase;
class FeeGradeServiceTest extends TestCase
{
public function test_grade_level_parsing(): void
{
$service = new FeeGradeService();
$this->assertSame(0, $service->getGradeLevel('K'));
$this->assertSame(99, $service->getGradeLevel('Y'));
$this->assertSame(3, $service->getGradeLevel('3-A'));
$this->assertSame(999, $service->getGradeLevel('Unknown'));
}
public function test_compare_grades_orders_numeric_then_suffix(): void
{
$service = new FeeGradeService();
$this->assertSame(-1, $service->compareGrades('2', '3'));
$this->assertSame(-1, $service->compareGrades('3-A', '3-B'));
$this->assertSame(1, $service->compareGrades('4', '3'));
}
}
@@ -0,0 +1,67 @@
<?php
namespace Tests\Unit\Services\Fees;
use App\Services\Fees\FeeConfigService;
use App\Services\Fees\FeeGradeService;
use App\Services\Fees\FeeRefundCalculatorService;
use App\Services\Fees\FeeStudentFeeService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class FeeRefundCalculatorServiceTest extends TestCase
{
use RefreshDatabase;
public function test_calculate_refund_caps_at_total_paid(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'refund_deadline', 'config_value' => '2025-02-01'],
['config_key' => 'weeks_study', 'config_value' => '8'],
['config_key' => 'last_school_day', 'config_value' => '2025-06-01'],
['config_key' => 'first_student_fee', 'config_value' => '350'],
['config_key' => 'second_student_fee', 'config_value' => '200'],
['config_key' => 'youth_fee', 'config_value' => '180'],
]);
DB::table('classSection')->insert([
'id' => 1,
'class_id' => 1,
'class_section_id' => 101,
'class_section_name' => '3',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
DB::table('payments')->insert([
'id' => 1,
'parent_id' => 99,
'paid_amount' => 100.00,
'total_amount' => 100.00,
'balance' => 0.00,
'number_of_installments' => 1,
'payment_date' => '2025-01-10',
'school_year' => '2025-2026',
'status' => 'Paid',
]);
$service = new FeeRefundCalculatorService(
new FeeConfigService(),
new FeeStudentFeeService(new FeeGradeService(), new FeeConfigService())
);
$result = $service->calculateRefund([
[
'student_id' => 1,
'class_section_id' => 101,
'enrollment_status' => 'withdrawn',
'admission_status' => 'accepted',
'withdrawal_date' => '2025-01-15',
],
], 99);
$this->assertSame(100.0, $result['refund_amount']);
}
}
@@ -0,0 +1,50 @@
<?php
namespace Tests\Unit\Services\PurchaseOrders;
use App\Services\PurchaseOrders\PurchaseOrderCreateService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class PurchaseOrderCreateServiceTest extends TestCase
{
use RefreshDatabase;
public function test_create_persists_purchase_order_and_items(): void
{
$supplierId = DB::table('suppliers')->insertGetId(['name' => 'Alpha Supplies']);
$supplyId = DB::table('supplies')->insertGetId(['name' => 'Paper', 'unit' => 'box', 'qty_on_hand' => 0]);
$service = new PurchaseOrderCreateService();
$po = $service->create([
'po_number' => 'PO-500',
'supplier_id' => $supplierId,
'order_date' => '2026-03-10',
'expected_date' => '2026-03-15',
'notes' => 'Initial order',
'items' => [
[
'supply_id' => $supplyId,
'description' => 'Paper for class',
'quantity' => 3,
'unit_cost' => 4,
],
],
]);
$this->assertNotNull($po->id);
$this->assertDatabaseHas('purchase_orders', [
'id' => $po->id,
'po_number' => 'PO-500',
'subtotal' => 12,
'total' => 12,
]);
$this->assertDatabaseHas('purchase_order_items', [
'purchase_order_id' => $po->id,
'supply_id' => $supplyId,
'quantity' => 3,
'unit_cost' => 4,
]);
}
}
@@ -0,0 +1,62 @@
<?php
namespace Tests\Unit\Services\PurchaseOrders;
use App\Models\PurchaseOrder;
use App\Models\PurchaseOrderItem;
use App\Models\Supply;
use App\Services\PurchaseOrders\PurchaseOrderReceiveService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PurchaseOrderReceiveServiceTest extends TestCase
{
use RefreshDatabase;
public function test_receive_updates_items_and_inventory(): void
{
$supply = Supply::query()->create([
'name' => 'Paper',
'unit' => 'box',
'qty_on_hand' => 2,
]);
$po = PurchaseOrder::query()->create([
'po_number' => 'PO-600',
'supplier_id' => 1,
'status' => PurchaseOrder::STATUS_ORDERED,
'subtotal' => 10,
'tax' => 0,
'total' => 10,
]);
$item = PurchaseOrderItem::query()->create([
'purchase_order_id' => $po->id,
'supply_id' => $supply->id,
'description' => 'Paper for class',
'quantity' => 3,
'received_qty' => 0,
'unit_cost' => 3,
]);
$service = new PurchaseOrderReceiveService();
$result = $service->receive($po->id, [
['id' => $item->id, 'quantity' => 3],
], 'tester@example.com');
$this->assertTrue($result['completed']);
$this->assertDatabaseHas('purchase_order_items', [
'id' => $item->id,
'received_qty' => 3,
]);
$this->assertDatabaseHas('supplies', [
'id' => $supply->id,
'qty_on_hand' => 5,
]);
$this->assertDatabaseHas('supply_transactions', [
'supply_id' => $supply->id,
'type' => 'in',
'quantity' => 3,
]);
}
}
@@ -0,0 +1,34 @@
<?php
namespace Tests\Unit\Services\PurchaseOrders;
use App\Models\PurchaseOrder;
use App\Services\PurchaseOrders\PurchaseOrderStatusService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PurchaseOrderStatusServiceTest extends TestCase
{
use RefreshDatabase;
public function test_cancel_marks_purchase_order_canceled(): void
{
$po = PurchaseOrder::query()->create([
'po_number' => 'PO-700',
'supplier_id' => 1,
'status' => PurchaseOrder::STATUS_ORDERED,
'subtotal' => 0,
'tax' => 0,
'total' => 0,
]);
$service = new PurchaseOrderStatusService();
$updated = $service->cancel($po->id);
$this->assertSame(PurchaseOrder::STATUS_CANCELED, $updated->status);
$this->assertDatabaseHas('purchase_orders', [
'id' => $po->id,
'status' => PurchaseOrder::STATUS_CANCELED,
]);
}
}