Strengthen service workflow tests

This commit is contained in:
2026-03-19 20:28:04 +01:00
parent f7b03926b0
commit 56c3e30f43
3 changed files with 224 additions and 47 deletions

View File

@@ -9,6 +9,7 @@ use MintyPHP\Service\Directory\DirectorySettingsGateway;
use MintyPHP\Service\Org\DepartmentService;
use MintyPHP\Service\Tenant\TenantScopeService;
use MintyPHP\Service\User\UserServicesFactory;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
@@ -109,26 +110,16 @@ class DepartmentServiceTest extends TestCase
$this->assertSame('Beta', $result[2][0]['description']);
}
public function testGroupActiveByTenantIdsReturnsEmptyForInvalidIds(): void
#[DataProvider('groupActiveByTenantIdsInputProvider')]
public function testGroupActiveByTenantIdsNormalizesInput(array $tenantIds, array $expectedRepositoryIds): void
{
$this->departmentRepository
->expects($this->never())
->method('listByTenantIds');
$result = $this->service->groupActiveByTenantIds([0, -1, -5]);
$this->assertSame([], $result);
}
public function testGroupActiveByTenantIdsDeduplicatesIds(): void
{
$this->departmentRepository
->expects($this->once())
->expects($expectedRepositoryIds ? $this->once() : $this->never())
->method('listByTenantIds')
->with([1])
->with($expectedRepositoryIds)
->willReturn([]);
$this->service->groupActiveByTenantIds([1, 1, 1]);
$this->assertSame([], $this->service->groupActiveByTenantIds($tenantIds));
}
public function testCreateFromAdminSucceeds(): void
@@ -193,36 +184,9 @@ class DepartmentServiceTest extends TestCase
$this->assertTrue($result['ok']);
}
public function testCreateFromAdminRejectsEmptyDescription(): void
#[DataProvider('invalidDepartmentInputProvider')]
public function testCreateFromAdminRejectsInvalidInput(array $input): void
{
$input = [
'description' => '',
'tenant_id' => 1,
'code' => '',
'cost_center' => '',
'active' => 1,
];
$this->departmentRepository
->expects($this->never())
->method('create');
$result = $this->service->createFromAdmin($input);
$this->assertFalse($result['ok']);
$this->assertNotEmpty($result['errors']);
}
public function testCreateFromAdminRejectsMissingTenantId(): void
{
$input = [
'description' => 'Sales',
'tenant_id' => 0,
'code' => '',
'cost_center' => '',
'active' => 1,
];
$this->departmentRepository
->expects($this->never())
->method('create');
@@ -291,6 +255,31 @@ class DepartmentServiceTest extends TestCase
$this->assertSame(404, $result['status']);
}
public function testDeleteByUuidReturnsDeleteFailedOnRepositoryFailure(): void
{
$this->departmentRepository
->expects($this->once())
->method('findByUuid')
->with('abc-999')
->willReturn(['id' => 9, 'uuid' => 'abc-999', 'description' => 'Ops']);
$this->departmentRepository
->expects($this->once())
->method('delete')
->with(9)
->willReturn(false);
$this->systemAuditService
->expects($this->never())
->method('record');
$result = $this->service->deleteByUuid('abc-999');
$this->assertFalse($result['ok']);
$this->assertSame(500, $result['status']);
$this->assertSame('delete_failed', $result['error']);
}
public function testSyncTenantsUsesFirstId(): void
{
$this->departmentRepository
@@ -322,4 +311,32 @@ class DepartmentServiceTest extends TestCase
$this->assertSame(-1, $result);
}
public static function groupActiveByTenantIdsInputProvider(): array
{
return [
'invalid ids are ignored completely' => [[0, -1, -5], []],
'duplicate ids are reduced before querying' => [[1, 1, 1], [1]],
];
}
public static function invalidDepartmentInputProvider(): array
{
return [
'missing description' => [[
'description' => '',
'tenant_id' => 1,
'code' => '',
'cost_center' => '',
'active' => 1,
]],
'missing tenant id' => [[
'description' => 'Sales',
'tenant_id' => 0,
'code' => '',
'cost_center' => '',
'active' => 1,
]],
];
}
}