add controllers, servoices
This commit is contained in:
@@ -2,120 +2,54 @@
|
||||
|
||||
namespace Tests\Unit\Services\Administrator;
|
||||
|
||||
use App\Models\AdminNotificationSubject;
|
||||
use App\Services\Administrator\AdminNotificationSubjectService;
|
||||
use App\Services\Administrator\AdminNotificationUserService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AdminNotificationSubjectServiceTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_alerts_data_includes_admins(): 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'],
|
||||
DB::table('roles')->insert([
|
||||
['id' => 1, 'name' => 'Admin', 'slug' => 'admin', 'dashboard_route' => 'admin', 'priority' => 1, 'is_active' => 1],
|
||||
]);
|
||||
|
||||
$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'],
|
||||
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',
|
||||
]);
|
||||
|
||||
$this->assertTrue($result['success']);
|
||||
$this->assertSame(200, $result['status']);
|
||||
DB::table('user_roles')->insert([
|
||||
'id' => 1,
|
||||
'user_id' => 1,
|
||||
'role_id' => 1,
|
||||
]);
|
||||
|
||||
$userService = new \App\Services\Administrator\AdminNotificationUserService();
|
||||
$service = new AdminNotificationSubjectService($userService);
|
||||
|
||||
$data = $service->alertsData();
|
||||
|
||||
$this->assertTrue($data['tableReady']);
|
||||
$this->assertCount(1, $data['admins']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user