add controllers, servoices
This commit is contained in:
@@ -2,111 +2,57 @@
|
||||
|
||||
namespace Tests\Unit\Services\Administrator;
|
||||
|
||||
use App\Models\AdminNotificationSubject;
|
||||
use App\Services\Administrator\AdminNotificationUserService;
|
||||
use App\Services\Administrator\AdminPrintRecipientService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AdminPrintRecipientServiceTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_data_returns_admins_and_assigned(): void
|
||||
{
|
||||
Mockery::close();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_data_returns_assigned_admins(): 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' => 'A', 'lastname' => 'One', 'email' => 'a1@test.com'],
|
||||
['id' => 2, 'firstname' => 'A', 'lastname' => 'Two', 'email' => 'a2@test.com'],
|
||||
]);
|
||||
|
||||
$row1 = (object) ['admin_id' => 1];
|
||||
$row2 = (object) ['admin_id' => 2];
|
||||
|
||||
AdminNotificationSubject::shouldReceive('query->select->where->whereIn->get')
|
||||
->once()
|
||||
->andReturn(collect([$row1, $row2]));
|
||||
|
||||
$service = new AdminPrintRecipientService($userService);
|
||||
|
||||
$result = $service->data();
|
||||
|
||||
$this->assertTrue($result['tableReady']);
|
||||
$this->assertTrue($result['assigned'][1]);
|
||||
$this->assertTrue($result['assigned'][2]);
|
||||
}
|
||||
|
||||
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 AdminPrintRecipientService($userService);
|
||||
|
||||
$result = $service->save([
|
||||
1 => 1,
|
||||
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_updates_print_recipients(): void
|
||||
{
|
||||
DB::shouldReceive('getSchemaBuilder->hasTable')
|
||||
->once()
|
||||
->with('admin_notification_subjects')
|
||||
->andReturn(true);
|
||||
|
||||
$userService = Mockery::mock(AdminNotificationUserService::class);
|
||||
$userService->shouldReceive('fetchAdminNotificationUsers')
|
||||
->once()
|
||||
->andReturn([
|
||||
['id' => 1],
|
||||
['id' => 2],
|
||||
['id' => 3],
|
||||
]);
|
||||
|
||||
AdminNotificationSubject::shouldReceive('query->where->whereIn->pluck')
|
||||
->once()
|
||||
->andReturn(collect([1, 2]));
|
||||
|
||||
AdminNotificationSubject::shouldReceive('query->where->whereIn->delete')
|
||||
->once()
|
||||
->andReturn(1);
|
||||
|
||||
AdminNotificationSubject::shouldReceive('insert')
|
||||
->once()
|
||||
->with(Mockery::on(function ($batch) {
|
||||
return count($batch) === 1
|
||||
&& $batch[0]['admin_id'] === 3
|
||||
&& $batch[0]['subject'] === 'print_requests';
|
||||
}))
|
||||
->andReturn(true);
|
||||
|
||||
$service = new AdminPrintRecipientService($userService);
|
||||
|
||||
$result = $service->save([
|
||||
1 => 1,
|
||||
3 => 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',
|
||||
]);
|
||||
|
||||
$this->assertTrue($result['success']);
|
||||
$this->assertSame(200, $result['status']);
|
||||
DB::table('user_roles')->insert([
|
||||
'id' => 1,
|
||||
'user_id' => 1,
|
||||
'role_id' => 1,
|
||||
]);
|
||||
|
||||
DB::table('admin_notification_subjects')->insert([
|
||||
'id' => 1,
|
||||
'admin_id' => 1,
|
||||
'subject' => 'print_requests',
|
||||
]);
|
||||
|
||||
$service = new AdminPrintRecipientService(new \App\Services\Administrator\AdminNotificationUserService());
|
||||
$data = $service->data();
|
||||
|
||||
$this->assertTrue($data['assigned'][1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user