121 lines
4.4 KiB
PHP
121 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Services\Administrator;
|
|
|
|
use App\Models\AdminNotificationSubject;
|
|
use App\Services\Administrator\AdminNotificationSubjectService;
|
|
use App\Services\Administrator\AdminNotificationUserService;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Mockery;
|
|
use Tests\TestCase;
|
|
|
|
class AdminNotificationSubjectServiceTest extends TestCase
|
|
{
|
|
protected function tearDown(): void
|
|
{
|
|
Mockery::close();
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_alerts_data_returns_expected_payload_when_table_exists(): void
|
|
{
|
|
DB::shouldReceive('getSchemaBuilder->hasTable')
|
|
->once()
|
|
->with('admin_notification_subjects')
|
|
->andReturn(true);
|
|
|
|
$userService = Mockery::mock(AdminNotificationUserService::class);
|
|
$userService->shouldReceive('fetchAdminNotificationUsers')
|
|
->once()
|
|
->andReturn([
|
|
['id' => 1, 'firstname' => 'Admin', 'lastname' => 'One', 'email' => 'a1@test.com'],
|
|
['id' => 2, 'firstname' => 'Admin', 'lastname' => 'Two', 'email' => 'a2@test.com'],
|
|
]);
|
|
|
|
$row1 = (object) ['id' => 10, 'admin_id' => 1, 'subject' => 'academics'];
|
|
$row2 = (object) ['id' => 11, 'admin_id' => 1, 'subject' => 'finance'];
|
|
$row3 = (object) ['id' => 12, 'admin_id' => 2, 'subject' => 'general'];
|
|
|
|
AdminNotificationSubject::shouldReceive('query->select->whereIn->get')
|
|
->once()
|
|
->andReturn(collect([$row1, $row2, $row3]));
|
|
|
|
$service = new AdminNotificationSubjectService($userService);
|
|
|
|
$result = $service->alertsData();
|
|
|
|
$this->assertTrue($result['tableReady']);
|
|
$this->assertCount(2, $result['admins']);
|
|
$this->assertArrayHasKey('academics', $result['subjects']);
|
|
$this->assertTrue($result['assignedSubjects'][1]['academics']);
|
|
$this->assertTrue($result['assignedSubjects'][1]['finance']);
|
|
$this->assertTrue($result['assignedSubjects'][2]['general']);
|
|
}
|
|
|
|
public function test_save_returns_error_when_table_missing(): void
|
|
{
|
|
DB::shouldReceive('getSchemaBuilder->hasTable')
|
|
->once()
|
|
->with('admin_notification_subjects')
|
|
->andReturn(false);
|
|
|
|
$userService = Mockery::mock(AdminNotificationUserService::class);
|
|
$service = new AdminNotificationSubjectService($userService);
|
|
|
|
$result = $service->save([
|
|
1 => ['academics'],
|
|
]);
|
|
|
|
$this->assertFalse($result['success']);
|
|
$this->assertSame(422, $result['status']);
|
|
}
|
|
|
|
public function test_save_inserts_and_deletes_subjects(): void
|
|
{
|
|
DB::shouldReceive('getSchemaBuilder->hasTable')
|
|
->once()
|
|
->with('admin_notification_subjects')
|
|
->andReturn(true);
|
|
|
|
$userService = Mockery::mock(AdminNotificationUserService::class);
|
|
$userService->shouldReceive('fetchAdminNotificationUsers')
|
|
->once()
|
|
->andReturn([
|
|
['id' => 1, 'firstname' => 'Admin', 'lastname' => 'One', 'email' => 'a1@test.com'],
|
|
['id' => 2, 'firstname' => 'Admin', 'lastname' => 'Two', 'email' => 'a2@test.com'],
|
|
]);
|
|
|
|
$existing1 = (object) ['id' => 101, 'admin_id' => 1, 'subject' => 'academics'];
|
|
$existing2 = (object) ['id' => 102, 'admin_id' => 1, 'subject' => 'finance'];
|
|
$existing3 = (object) ['id' => 103, 'admin_id' => 2, 'subject' => 'general'];
|
|
|
|
AdminNotificationSubject::shouldReceive('query->select->whereIn->get')
|
|
->once()
|
|
->andReturn(collect([$existing1, $existing2, $existing3]));
|
|
|
|
AdminNotificationSubject::shouldReceive('query->where->whereIn->delete')
|
|
->once()
|
|
->andReturn(1);
|
|
|
|
AdminNotificationSubject::shouldReceive('insert')
|
|
->once()
|
|
->with(Mockery::on(function ($batch) {
|
|
return count($batch) === 2
|
|
&& $batch[0]['admin_id'] === 1
|
|
&& $batch[0]['subject'] === 'general'
|
|
&& $batch[1]['admin_id'] === 2
|
|
&& $batch[1]['subject'] === 'attendance';
|
|
}))
|
|
->andReturn(true);
|
|
|
|
$service = new AdminNotificationSubjectService($userService);
|
|
|
|
$result = $service->save([
|
|
1 => ['academics', 'general'],
|
|
2 => ['general', 'attendance'],
|
|
]);
|
|
|
|
$this->assertTrue($result['success']);
|
|
$this->assertSame(200, $result['status']);
|
|
}
|
|
} |