From eb90e766fae94ec22c12215500f74c5a3643283e Mon Sep 17 00:00:00 2001 From: Juan Claude Date: Wed, 11 Nov 2026 14:38:50 +0000 Subject: [PATCH 1/2] test(people): pin the register context of the seat, handover and role services before sharing it Assisted-by: ClaudeCode:claude-opus-5-5 --- .../People/RegisterContextCopiesTest.php | 235 ++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 tests/Unit/Service/People/RegisterContextCopiesTest.php diff --git a/tests/Unit/Service/People/RegisterContextCopiesTest.php b/tests/Unit/Service/People/RegisterContextCopiesTest.php new file mode 100644 index 0000000000..437e3e933e --- /dev/null +++ b/tests/Unit/Service/People/RegisterContextCopiesTest.php @@ -0,0 +1,235 @@ + + * @copyright 2026 Conduction B.V. + * @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 + * + * @link https://conduction.nl + * + * SPDX-FileCopyrightText: 2026 Conduction B.V. + * SPDX-License-Identifier: EUPL-1.2 + * + * @spec openspec/specs/case-management/spec.md + */ + +declare(strict_types=1); + +namespace OCA\Dossiq\Tests\Unit\Service\People; + +use OCA\Dossiq\Service\People\CaseRoleProjection; +use OCA\Dossiq\Service\People\CaseSeats; +use OCA\Dossiq\Service\People\LeaverHandoverService; +use OCA\Dossiq\Service\SettingsService; +use OCA\Dossiq\Service\Transfer\InternalHandover; +use PHPUnit\Framework\TestCase; +use ReflectionClass; +use ReflectionMethod; +use ReflectionNamedType; +use RuntimeException; +use stdClass; + +/** + * Every one of these services reads the register and its schemas the same way. + * + * @covers \OCA\Dossiq\Service\People\CaseRoleProjection + * @covers \OCA\Dossiq\Service\People\CaseSeats + * @covers \OCA\Dossiq\Service\People\LeaverHandoverService + * @covers \OCA\Dossiq\Service\Transfer\InternalHandover + * + * @spec openspec/specs/case-management/spec.md + */ +class RegisterContextCopiesTest extends TestCase { + /** + * Each service with the name of its private register reading. + * + * @return array + */ + public static function services(): array { + return [ + 'case seats' => [CaseSeats::class, 'context'], + 'internal handover' => [InternalHandover::class, 'context'], + 'leaver handover' => [LeaverHandoverService::class, 'context'], + 'case role projection' => [CaseRoleProjection::class, 'requireRegister'], + ]; + }//end services() + + /** + * The object service and the configured register come back as a pair. + * + * @param class-string $class The service. + * @param string $context Its register reading. + * + * @dataProvider services + * + * @return void + */ + public function testContextIsTheObjectServiceAndTheRegister(string $class, string $context): void { + $service = new stdClass(); + $subject = $this->build(class: $class, settings: $this->settings(service: $service, config: ['register' => 'dossiq'])); + + self::assertSame(expected: [$service, 'dossiq'], actual: $this->call(subject: $subject, method: $context)); + }//end testContextIsTheObjectServiceAndTheRegister() + + /** + * Without OpenRegister the context is refused, before the register is read. + * + * @param class-string $class The service. + * @param string $context Its register reading. + * + * @dataProvider services + * + * @return void + */ + public function testContextRefusesWithoutOpenRegister(string $class, string $context): void { + $settings = $this->createMock(originalClassName: SettingsService::class); + $settings->method('getObjectService')->willReturn(null); + $settings->expects(self::never())->method('getConfigValue'); + $subject = $this->build(class: $class, settings: $settings); + + $this->expectException(exception: RuntimeException::class); + $this->expectExceptionMessage(message: 'OpenRegister is not available'); + $this->call(subject: $subject, method: $context); + }//end testContextRefusesWithoutOpenRegister() + + /** + * Without a configured register the context is refused. + * + * @param class-string $class The service. + * @param string $context Its register reading. + * + * @dataProvider services + * + * @return void + */ + public function testContextRefusesWithoutARegister(string $class, string $context): void { + $subject = $this->build(class: $class, settings: $this->settings(service: new stdClass(), config: [])); + + $this->expectException(exception: RuntimeException::class); + $this->expectExceptionMessage(message: 'Dossier register not configured'); + $this->call(subject: $subject, method: $context); + }//end testContextRefusesWithoutARegister() + + /** + * A configured schema is the value its key holds. + * + * @param class-string $class The service. + * + * @dataProvider services + * + * @return void + */ + public function testSchemaIsTheConfiguredValue(string $class): void { + $subject = $this->build(class: $class, settings: $this->settings(service: null, config: ['role_schema' => 'role'])); + + self::assertSame(expected: 'role', actual: $this->schema(subject: $subject, key: 'role_schema')); + }//end testSchemaIsTheConfiguredValue() + + /** + * An unset schema is refused with the key in the sentence. + * + * @param class-string $class The service. + * + * @dataProvider services + * + * @return void + */ + public function testSchemaRefusesAnUnsetKey(string $class): void { + $subject = $this->build(class: $class, settings: $this->settings(service: null, config: [])); + + $this->expectException(exception: RuntimeException::class); + $this->expectExceptionMessage(message: 'Dossiq schema role_schema not configured'); + $this->schema(subject: $subject, key: 'role_schema'); + }//end testSchemaRefusesAnUnsetKey() + + /** + * Settings answering the given object service and configuration. + * + * @param object|null $service The object service, or null. + * @param array $config The configuration values. + * + * @return SettingsService The settings double. + */ + private function settings(?object $service, array $config): SettingsService { + $settings = $this->createMock(originalClassName: SettingsService::class); + $settings->method('getObjectService')->willReturn($service); + $settings->method('getConfigValue')->willReturnCallback( + static fn (string $key, string $default = ''): string => ($config[$key] ?? $default) + ); + + return $settings; + }//end settings() + + /** + * The service over the given settings, every other dependency a double or its default. + * + * @param class-string $class The service. + * @param SettingsService $settings The settings. + * + * @return object The service. + */ + private function build(string $class, SettingsService $settings): object { + $arguments = []; + foreach ((new ReflectionClass($class))->getConstructor()->getParameters() as $parameter) { + if ($parameter->isDefaultValueAvailable() === true) { + break; + } + + $type = $parameter->getType(); + self::assertInstanceOf(expected: ReflectionNamedType::class, actual: $type); + $arguments[] = $this->argument(type: $type->getName(), settings: $settings); + } + + return new $class(...$arguments); + }//end build() + + /** + * The settings for a SettingsService parameter, a double for any other. + * + * @param class-string $type The parameter's class. + * @param SettingsService $settings The settings. + * + * @return object The argument. + */ + private function argument(string $type, SettingsService $settings): object { + if ($type === SettingsService::class) { + return $settings; + } + + return $this->createMock(originalClassName: $type); + }//end argument() + + /** + * Call a private method of the service that takes no argument. + * + * @param object $subject The service. + * @param string $method The method. + * + * @return mixed What the method returned. + */ + private function call(object $subject, string $method): mixed { + return (new ReflectionMethod($subject, $method))->invoke($subject); + }//end call() + + /** + * Call the service's private schema(). + * + * @param object $subject The service. + * @param string $key The schema key. + * + * @return mixed What schema() returned. + */ + private function schema(object $subject, string $key): mixed { + return (new ReflectionMethod($subject, 'schema'))->invoke($subject, $key); + }//end schema() +}//end class From a836649f37b1b1f5485a0a80c5e08b4139874a9f Mon Sep 17 00:00:00 2001 From: Juan Claude Date: Wed, 11 Nov 2026 14:40:27 +0000 Subject: [PATCH 2/2] refactor(people): share the register context of the seat, handover and role services CaseSeats, InternalHandover, LeaverHandoverService and CaseRoleProjection each carried a private copy of the object service, register and schema reading that Custody\DossierRegister already holds. Each now builds one DossierRegister from its settings and delegates to it; constructor signatures, return values and exception messages are unchanged. Assisted-by: ClaudeCode:claude-opus-5-5 --- lib/Service/People/CaseRoleProjection.php | 30 ++++++++------------ lib/Service/People/CaseSeats.php | 30 ++++++++------------ lib/Service/People/LeaverHandoverService.php | 30 ++++++++------------ lib/Service/Transfer/InternalHandover.php | 30 ++++++++------------ 4 files changed, 48 insertions(+), 72 deletions(-) diff --git a/lib/Service/People/CaseRoleProjection.php b/lib/Service/People/CaseRoleProjection.php index 532599d8c3..c51ae0dff7 100644 --- a/lib/Service/People/CaseRoleProjection.php +++ b/lib/Service/People/CaseRoleProjection.php @@ -13,6 +13,7 @@ namespace OCA\Dossiq\Service\People; +use OCA\Dossiq\Service\Custody\DossierRegister; use OCA\Dossiq\Service\SettingsService; use OCA\Dossiq\Service\Support\SearchesObjects; use Psr\Log\LoggerInterface; @@ -38,16 +39,24 @@ class CaseRoleProjection { */ public const INITIATOR = 'initiator'; + /** + * The register and the configured schemas, read and refused the one shared way. + * + * @var DossierRegister + */ + private readonly DossierRegister $dossierRegister; + /** * @param SettingsService $settingsService OpenRegister access and the configured register and schemas. * @param PersonLinkReader $people Reads a link's name and address. * @param LoggerInterface $logger Says what a link could not become. */ public function __construct( - private readonly SettingsService $settingsService, + SettingsService $settingsService, private readonly PersonLinkReader $people, private readonly LoggerInterface $logger, ) { + $this->dossierRegister = new DossierRegister(settingsService: $settingsService); }//end __construct() /** @@ -318,17 +327,7 @@ private function uuidOf(mixed $saved): string { * @throws RuntimeException When OpenRegister or the register is not configured. */ private function requireRegister(): array { - $objectService = $this->settingsService->getObjectService(); - if ($objectService === null) { - throw new RuntimeException('OpenRegister is not available'); - } - - $register = $this->settingsService->getConfigValue('register'); - if ($register === '') { - throw new RuntimeException('Dossier register not configured'); - } - - return [$objectService, $register]; + return $this->dossierRegister->context(); }//end requireRegister() /** @@ -341,11 +340,6 @@ private function requireRegister(): array { * @throws RuntimeException When the key is not configured. */ private function schema(string $key): string { - $schema = $this->settingsService->getConfigValue($key); - if ($schema === '') { - throw new RuntimeException('Dossiq schema ' . $key . ' not configured'); - } - - return $schema; + return $this->dossierRegister->schema(key: $key); }//end schema() }//end class diff --git a/lib/Service/People/CaseSeats.php b/lib/Service/People/CaseSeats.php index 20641f4fa4..8f47347d02 100644 --- a/lib/Service/People/CaseSeats.php +++ b/lib/Service/People/CaseSeats.php @@ -43,6 +43,7 @@ namespace OCA\Dossiq\Service\People; +use OCA\Dossiq\Service\Custody\DossierRegister; use OCA\Dossiq\Service\SettingsService; use OCA\Dossiq\Service\Support\SearchesObjects; use Psr\Log\LoggerInterface; @@ -93,6 +94,13 @@ class CaseSeats { */ public const PAGE_SIZE = 200; + /** + * The register and the configured schemas, read and refused the one shared way. + * + * @var DossierRegister + */ + private readonly DossierRegister $dossierRegister; + /** * Constructor. * @@ -102,9 +110,10 @@ class CaseSeats { * @spec openspec/changes/handing-a-case-over/specs/people-on-the-case/spec.md */ public function __construct( - private readonly SettingsService $settingsService, + SettingsService $settingsService, private readonly LoggerInterface $logger, ) { + $this->dossierRegister = new DossierRegister(settingsService: $settingsService); }//end __construct() /** @@ -358,17 +367,7 @@ private function rows(string $schemaKey, array $filters): array { * @throws RuntimeException When OpenRegister is absent or unconfigured. */ private function context(): array { - $objectService = $this->settingsService->getObjectService(); - if ($objectService === null) { - throw new RuntimeException('OpenRegister is not available'); - } - - $register = $this->settingsService->getConfigValue('register'); - if ($register === '') { - throw new RuntimeException('Dossier register not configured'); - } - - return [$objectService, $register]; + return $this->dossierRegister->context(); }//end context() /** @@ -381,12 +380,7 @@ private function context(): array { * @throws RuntimeException When the key is unset. */ private function schema(string $key): string { - $schema = $this->settingsService->getConfigValue($key); - if ($schema === '') { - throw new RuntimeException('Dossiq schema ' . $key . ' not configured'); - } - - return $schema; + return $this->dossierRegister->schema(key: $key); }//end schema() /** diff --git a/lib/Service/People/LeaverHandoverService.php b/lib/Service/People/LeaverHandoverService.php index 2e4b6a59a5..ddae2b3bf3 100644 --- a/lib/Service/People/LeaverHandoverService.php +++ b/lib/Service/People/LeaverHandoverService.php @@ -59,6 +59,7 @@ use DateTimeImmutable; use InvalidArgumentException; use OCA\Dossiq\Service\CaseReassignmentService; +use OCA\Dossiq\Service\Custody\DossierRegister; use OCA\Dossiq\Service\SettingsService; use OCA\Dossiq\Service\Support\SearchesObjects; use OCA\Dossiq\Service\Support\TranslatedText; @@ -82,6 +83,13 @@ class LeaverHandoverService { */ public const PAGE_SIZE = 200; + /** + * The register and the configured schemas, read and refused the one shared way. + * + * @var DossierRegister + */ + private readonly DossierRegister $dossierRegister; + /** * Constructor. * @@ -96,10 +104,11 @@ class LeaverHandoverService { public function __construct( private readonly CaseReassignmentService $reassignment, private readonly CaseSeats $seats, - private readonly SettingsService $settings, + SettingsService $settings, private readonly LoggerInterface $logger, private readonly TranslatedText $text = new TranslatedText(), ) { + $this->dossierRegister = new DossierRegister(settingsService: $settings); }//end __construct() /** @@ -416,17 +425,7 @@ public function draftsOf(string $uid): array { * @throws RuntimeException When OpenRegister is absent or unconfigured. */ private function context(): array { - $objectService = $this->settings->getObjectService(); - if ($objectService === null) { - throw new RuntimeException('OpenRegister is not available'); - } - - $register = $this->settings->getConfigValue('register'); - if ($register === '') { - throw new RuntimeException('Dossier register not configured'); - } - - return [$objectService, $register]; + return $this->dossierRegister->context(); }//end context() /** @@ -439,11 +438,6 @@ private function context(): array { * @throws RuntimeException When the key is unset. */ private function schema(string $key): string { - $schema = $this->settings->getConfigValue($key); - if ($schema === '') { - throw new RuntimeException('Dossiq schema ' . $key . ' not configured'); - } - - return $schema; + return $this->dossierRegister->schema(key: $key); }//end schema() }//end class diff --git a/lib/Service/Transfer/InternalHandover.php b/lib/Service/Transfer/InternalHandover.php index d7e4ba4442..160330b437 100644 --- a/lib/Service/Transfer/InternalHandover.php +++ b/lib/Service/Transfer/InternalHandover.php @@ -52,6 +52,7 @@ use DateTimeImmutable; use OCA\Dossiq\Exception\RefusedException; +use OCA\Dossiq\Service\Custody\DossierRegister; use OCA\Dossiq\Service\Routing\RoutingNote; use OCA\Dossiq\Service\SettingsService; use OCA\Dossiq\Service\Support\SearchesObjects; @@ -103,6 +104,13 @@ class InternalHandover { */ public const PAGE_SIZE = 200; + /** + * The register and the configured schemas, read and refused the one shared way. + * + * @var DossierRegister + */ + private readonly DossierRegister $dossierRegister; + /** * Constructor. * @@ -115,12 +123,13 @@ class InternalHandover { * @spec openspec/changes/handing-a-case-over/specs/case-management/spec.md */ public function __construct( - private readonly SettingsService $settingsService, + SettingsService $settingsService, private readonly TeamDirectory $teams, private readonly CaseSeatReconciler $seats, private readonly DoorzendingNotifier $doorzending, private readonly LoggerInterface $logger, ) { + $this->dossierRegister = new DossierRegister(settingsService: $settingsService); }//end __construct() /** @@ -571,17 +580,7 @@ private function writeCase(array $case, array $changes): void { * @throws RuntimeException When OpenRegister is absent or unconfigured. */ private function context(): array { - $objectService = $this->settingsService->getObjectService(); - if ($objectService === null) { - throw new RuntimeException('OpenRegister is not available'); - } - - $register = $this->settingsService->getConfigValue('register'); - if ($register === '') { - throw new RuntimeException('Dossier register not configured'); - } - - return [$objectService, $register]; + return $this->dossierRegister->context(); }//end context() /** @@ -594,11 +593,6 @@ private function context(): array { * @throws RuntimeException When the key is unset. */ private function schema(string $key): string { - $schema = $this->settingsService->getConfigValue($key); - if ($schema === '') { - throw new RuntimeException('Dossiq schema ' . $key . ' not configured'); - } - - return $schema; + return $this->dossierRegister->schema(key: $key); }//end schema() }//end class