From 2d82b95b36a6604182352d5f8ad0ee62157961fa Mon Sep 17 00:00:00 2001 From: Juan Claude Date: Wed, 11 Nov 2026 15:03:17 +0000 Subject: [PATCH 1/2] test(support): pin the result-to-array readers of the intake, permit, capacity and advice services Assisted-by: ClaudeCode:claude-opus-5-5 --- .../SerialisedArrayIntakeCopiesTest.php | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 tests/Unit/Service/Support/SerialisedArrayIntakeCopiesTest.php diff --git a/tests/Unit/Service/Support/SerialisedArrayIntakeCopiesTest.php b/tests/Unit/Service/Support/SerialisedArrayIntakeCopiesTest.php new file mode 100644 index 0000000000..70f7341ddb --- /dev/null +++ b/tests/Unit/Service/Support/SerialisedArrayIntakeCopiesTest.php @@ -0,0 +1,149 @@ + + * @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\Support; + +use ArrayObject; +use JsonSerializable; +use OCA\Dossiq\Service\Advice\AdviceRepository; +use OCA\Dossiq\Service\DsoCaseService; +use OCA\Dossiq\Service\FormsIntakeService; +use OCA\Dossiq\Service\Intake\CaseIntakeFiling; +use OCA\Dossiq\Service\Intake\ChannelIntake; +use OCA\Dossiq\Service\Transitions\CapacityGuard; +use PHPUnit\Framework\TestCase; +use ReflectionClass; +use ReflectionMethod; +use stdClass; + +/** + * Every copy answers the same array for the same result. + * + * @covers \OCA\Dossiq\Service\Advice\AdviceRepository + * @covers \OCA\Dossiq\Service\DsoCaseService + * @covers \OCA\Dossiq\Service\FormsIntakeService + * @covers \OCA\Dossiq\Service\Intake\CaseIntakeFiling + * @covers \OCA\Dossiq\Service\Intake\ChannelIntake + * @covers \OCA\Dossiq\Service\Transitions\CapacityGuard + * + * @spec openspec/specs/case-management/spec.md + */ +class SerialisedArrayIntakeCopiesTest extends TestCase { + /** + * Every class carrying a copy, crossed with every kind of result. + * + * @return array}> + */ + public static function cases(): array { + $classes = [ + AdviceRepository::class => 'normalize', + DsoCaseService::class => 'normalizeToArray', + FormsIntakeService::class => 'arrayOf', + CaseIntakeFiling::class => 'asArray', + ChannelIntake::class => 'objectAsArray', + CapacityGuard::class => 'arrayOf', + ]; + $cases = []; + foreach ($classes as $class => $method) { + foreach (self::results() as $kind => [$value, $expected]) { + $cases[$class . ' ' . $kind] = [$class . '::' . $method, $value, $expected]; + } + } + + return $cases; + }//end cases() + + /** + * Each kind of result with the array it becomes. + * + * @return array}> + */ + private static function results(): array { + $object = new stdClass(); + $object->id = 'plain'; + + return [ + 'array' => [['id' => 'a'], ['id' => 'a']], + 'empty array' => [[], []], + 'serialisable to an array' => [self::serialisable(value: ['id' => 'b']), ['id' => 'b']], + 'serialisable to a string' => [self::serialisable(value: 'b'), []], + 'object without jsonSerialize' => [$object, []], + 'array object without jsonSerialize' => [new ArrayObject(['id' => 'c']), []], + 'string' => ['abc', []], + 'integer' => [7, []], + 'null' => [null, []], + ]; + }//end results() + + /** + * A result whose jsonSerialize() answers the given value. + * + * @param mixed $value What jsonSerialize() answers. + * + * @return JsonSerializable The result. + */ + private static function serialisable(mixed $value): JsonSerializable { + return new class ($value) implements JsonSerializable { + /** + * Constructor. + * + * @param mixed $value What jsonSerialize() answers. + */ + public function __construct(private readonly mixed $value) { + }//end __construct() + + /** + * The configured value. + * + * @return mixed The value. + */ + public function jsonSerialize(): mixed { + return $this->value; + }//end jsonSerialize() + }; + }//end serialisable() + + /** + * The copy turns the result into the expected array. + * + * @param string $copy The copy, as Class::method. + * @param mixed $value The result. + * @param array $expected The array it becomes. + * + * @dataProvider cases + * + * @return void + */ + public function testTheResultBecomesTheExpectedArray(string $copy, mixed $value, array $expected): void { + [$class, $method] = explode(separator: '::', string: $copy); + $subject = (new ReflectionClass($class))->newInstanceWithoutConstructor(); + + self::assertSame(expected: $expected, actual: (new ReflectionMethod($subject, $method))->invoke($subject, $value)); + }//end testTheResultBecomesTheExpectedArray() +}//end class From f165220bd45db6d7bee384d3dc4753bd38bf7102 Mon Sep 17 00:00:00 2001 From: Juan Claude Date: Wed, 11 Nov 2026 15:03:36 +0000 Subject: [PATCH 2/2] refactor(support): share the result-to-array reading of the intake, permit, capacity and advice services Six more classes carried the jsonSerialize-only reading under other names (normalize, normalizeToArray, arrayOf, asArray, objectAsArray). Each now delegates to Support\SerialisedArray::of(), added here with the same content as in the sibling changes so any of them can merge first. Assisted-by: ClaudeCode:claude-opus-5-5 --- lib/Service/Advice/AdviceRepository.php | 14 +----- lib/Service/DsoCaseService.php | 14 +----- lib/Service/FormsIntakeService.php | 14 +----- lib/Service/Intake/CaseIntakeFiling.php | 14 +----- lib/Service/Intake/ChannelIntake.php | 14 +----- lib/Service/Support/SerialisedArray.php | 59 +++++++++++++++++++++++ lib/Service/Transitions/CapacityGuard.php | 14 +----- 7 files changed, 71 insertions(+), 72 deletions(-) create mode 100644 lib/Service/Support/SerialisedArray.php diff --git a/lib/Service/Advice/AdviceRepository.php b/lib/Service/Advice/AdviceRepository.php index 3d2152e749..448a712e88 100644 --- a/lib/Service/Advice/AdviceRepository.php +++ b/lib/Service/Advice/AdviceRepository.php @@ -42,6 +42,7 @@ use OCA\Dossiq\AppInfo\Application; use OCA\Dossiq\Service\SettingsService; use OCA\Dossiq\Service\Support\SearchesObjects; +use OCA\Dossiq\Service\Support\SerialisedArray; use Psr\Log\LoggerInterface; use RuntimeException; use Throwable; @@ -232,17 +233,6 @@ public function save(array $update, string $adviceId): array { * @spec openspec/specs/advice-management/spec.md */ public function normalize(mixed $result): array { - if (is_array($result) === true) { - return $result; - } - - if (is_object($result) === true && method_exists($result, 'jsonSerialize') === true) { - $data = $result->jsonSerialize(); - if (is_array($data) === true) { - return $data; - } - } - - return []; + return SerialisedArray::of(value: $result); }//end normalize() }//end class diff --git a/lib/Service/DsoCaseService.php b/lib/Service/DsoCaseService.php index 49c48f8677..c18077ebff 100644 --- a/lib/Service/DsoCaseService.php +++ b/lib/Service/DsoCaseService.php @@ -30,6 +30,7 @@ use OCA\Dossiq\AppInfo\Application; use OCA\Dossiq\Service\Lifecycle\CaseJournal; use OCA\Dossiq\Service\Support\SearchesObjects; +use OCA\Dossiq\Service\Support\SerialisedArray; use OCA\OpenRegister\Contract\ObjectServiceInterface; use OCP\IAppConfig; use OCP\IUser; @@ -437,18 +438,7 @@ private function getObjectService(): object { * @return array The normalised array (empty when not coercible). */ private function normalizeToArray(mixed $value): array { - if (is_array($value) === true) { - return $value; - } - - if (is_object($value) === true && method_exists($value, 'jsonSerialize') === true) { - $serialized = $value->jsonSerialize(); - if (is_array($serialized) === true) { - return $serialized; - } - } - - return []; + return SerialisedArray::of(value: $value); }//end normalizeToArray() /** diff --git a/lib/Service/FormsIntakeService.php b/lib/Service/FormsIntakeService.php index ccb7a56a18..fc6ce3e6fd 100644 --- a/lib/Service/FormsIntakeService.php +++ b/lib/Service/FormsIntakeService.php @@ -45,6 +45,7 @@ namespace OCA\Dossiq\Service; use OCA\Dossiq\Service\Intake\IntakeFormBinding; +use OCA\Dossiq\Service\Support\SerialisedArray; use OCA\OpenRegister\Service\Form\FormSubmission; use OCA\OpenRegister\Service\Form\FormSubmitter; use Psr\Log\LoggerInterface; @@ -351,18 +352,7 @@ private function rowsOf(mixed $result): array { * @return array The row as an array. */ private function arrayOf(mixed $row): array { - if (is_array($row) === true) { - return $row; - } - - if (is_object($row) === true && method_exists($row, 'jsonSerialize') === true) { - $serialised = $row->jsonSerialize(); - if (is_array($serialised) === true) { - return $serialised; - } - } - - return []; + return SerialisedArray::of(value: $row); }//end arrayOf() /** diff --git a/lib/Service/Intake/CaseIntakeFiling.php b/lib/Service/Intake/CaseIntakeFiling.php index b408dc4cfb..9d01efe519 100644 --- a/lib/Service/Intake/CaseIntakeFiling.php +++ b/lib/Service/Intake/CaseIntakeFiling.php @@ -49,6 +49,7 @@ use OCA\Dossiq\Exception\RefusedException; use OCA\Dossiq\Service\CaseTypeStore; use OCA\Dossiq\Service\SettingsService; +use OCA\Dossiq\Service\Support\SerialisedArray; use Psr\Log\LoggerInterface; use Throwable; @@ -270,17 +271,6 @@ private function reasonOf(Throwable $error): string { * @return array The array. */ private function asArray(mixed $value): array { - if (is_array($value) === true) { - return $value; - } - - if (is_object($value) === true && method_exists($value, 'jsonSerialize') === true) { - $serialised = $value->jsonSerialize(); - if (is_array($serialised) === true) { - return $serialised; - } - } - - return []; + return SerialisedArray::of(value: $value); }//end asArray() }//end class diff --git a/lib/Service/Intake/ChannelIntake.php b/lib/Service/Intake/ChannelIntake.php index 70a3705113..d40da26583 100644 --- a/lib/Service/Intake/ChannelIntake.php +++ b/lib/Service/Intake/ChannelIntake.php @@ -61,6 +61,7 @@ use OCA\Dossiq\Service\Email\IntakeLog; use OCA\Dossiq\Service\SettingsService; use OCA\Dossiq\Service\Support\SearchesObjects; +use OCA\Dossiq\Service\Support\SerialisedArray; use Psr\Log\LoggerInterface; use Throwable; @@ -479,17 +480,6 @@ private function refuse(string $channel, string $externalId, array $message, str * @return array The row, or an empty array. */ private function objectAsArray(mixed $value): array { - if (is_array($value) === true) { - return $value; - } - - if (is_object($value) === true && method_exists($value, 'jsonSerialize') === true) { - $serialized = $value->jsonSerialize(); - if (is_array($serialized) === true) { - return $serialized; - } - } - - return []; + return SerialisedArray::of(value: $value); }//end objectAsArray() }//end class diff --git a/lib/Service/Support/SerialisedArray.php b/lib/Service/Support/SerialisedArray.php new file mode 100644 index 0000000000..a18fe82910 --- /dev/null +++ b/lib/Service/Support/SerialisedArray.php @@ -0,0 +1,59 @@ + + * @copyright 2026 Conduction B.V. + * @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 + * + * SPDX-License-Identifier: EUPL-1.2 + * SPDX-FileCopyrightText: 2026 Conduction B.V. + * + * @link https://conduction.nl + * + * @spec openspec/specs/case-management/spec.md + */ + +declare(strict_types=1); + +namespace OCA\Dossiq\Service\Support; + +/** + * Turns a result into the array it serialises to, or an empty array. + * + * @spec openspec/specs/case-management/spec.md + */ +final class SerialisedArray { + /** + * The result as an array: itself, or its jsonSerialize() array, or empty. + * + * @param mixed $value An array, an entity or anything else OpenRegister answered. + * + * @return array The array; empty when the value serialises to no array. + * + * @spec openspec/specs/case-management/spec.md + */ + public static function of(mixed $value): array { + if (is_array($value) === true) { + return $value; + } + + if (is_object($value) === true && method_exists($value, 'jsonSerialize') === true) { + $serialised = $value->jsonSerialize(); + if (is_array($serialised) === true) { + return $serialised; + } + } + + return []; + }//end of() +}//end class diff --git a/lib/Service/Transitions/CapacityGuard.php b/lib/Service/Transitions/CapacityGuard.php index d4b45e1bf7..b4eabd7d0e 100644 --- a/lib/Service/Transitions/CapacityGuard.php +++ b/lib/Service/Transitions/CapacityGuard.php @@ -42,6 +42,7 @@ namespace OCA\Dossiq\Service\Transitions; use OCA\Dossiq\Service\SettingsService; +use OCA\Dossiq\Service\Support\SerialisedArray; use OCP\IL10N; use Throwable; @@ -370,17 +371,6 @@ private static function firstId(array $row): string { * @return array The row. */ private function arrayOf(mixed $row): array { - if (is_array($row) === true) { - return $row; - } - - if (is_object($row) === true && method_exists($row, 'jsonSerialize') === true) { - $serialised = $row->jsonSerialize(); - if (is_array($serialised) === true) { - return $serialised; - } - } - - return []; + return SerialisedArray::of(value: $row); }//end arrayOf() }//end class