From 79edf75ee90a19a46e3714f289f57fdfc32e0978 Mon Sep 17 00:00:00 2001 From: Gregor Date: Mon, 31 Aug 2026 16:17:27 +0200 Subject: [PATCH] refactor: remove dependency on kba-team/memory-container and implement in-memory storage for mocks --- composer.json | 1 - src/SapRfcModuleMocks.php | 45 +++++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 42a2421..8984e6f 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,6 @@ "php": "^8.1", "ext-json": "*", "phpunit/phpunit": "^9.6.33", - "kba-team/memory-container": "^2.0", "php-sap/interfaces": "^5.1.4", "php-sap/common": "^6.1.3", "php-sap/datetime": "^1.5.1" diff --git a/src/SapRfcModuleMocks.php b/src/SapRfcModuleMocks.php index 9c42a09..ad16318 100644 --- a/src/SapRfcModuleMocks.php +++ b/src/SapRfcModuleMocks.php @@ -6,19 +6,18 @@ use Closure; use InvalidArgumentException; -use kbATeam\MemoryContainer\Container; use RuntimeException; /** * Class \phpsap\IntegrationTests\SapRfcModuleMocks * - * Container holding mock logic for the SAP RFC module. + * In-memory registry holding mock logic for the SAP RFC module. * * @package phpsap\IntegrationTests * @author Gregor J. * @license MIT */ -class SapRfcModuleMocks extends Container +class SapRfcModuleMocks { /** * @var array Valid SAP RFC module function or class method names. @@ -30,6 +29,11 @@ class SapRfcModuleMocks extends Container */ protected static string $requireFile; + /** + * @var array In-memory storage of mocked logic. + */ + private array $storage = []; + /** * Set the file to require. * @param string $file @@ -61,9 +65,25 @@ public static function validModuleFunctions(array $moduleFunctions): void * @param Closure $logic */ public function mock(string $name, Closure $logic): void + { + $this->storage[$this->validateId($name)] = $logic; + } + + /** + * Retrieve the mocked logic for a SAP RFC module specific function or method. + * @param string $name + * @return Closure + * @throws InvalidArgumentException The function name was no string or an empty + * string, or not in the list of templates. + * @throws RuntimeException No mock has been registered for this name. + */ + public function get(string $name): Closure { $nameValid = $this->validateId($name); - $this->set($nameValid, $logic); + if (!array_key_exists($nameValid, $this->storage)) { + throw new RuntimeException(sprintf('%s not found', $nameValid)); + } + return $this->storage[$nameValid]; } /** @@ -75,8 +95,8 @@ public function mock(string $name, Closure $logic): void */ protected function validateId(string $id): string { - $return = parent::validateId($id); - if (!in_array($return, static::$validModuleFunctions, true)) { + $return = trim($id); + if ($return === '' || !in_array($return, static::$validModuleFunctions, true)) { throw new InvalidArgumentException(sprintf( '%s function not defined in template.', $return @@ -97,4 +117,17 @@ public function __construct() } require_once static::$requireFile; } + + /** + * Always returns the same instance for the duration of the process. + * @return self + */ + public static function singleton(): self + { + static $instance = null; + if ($instance === null) { + $instance = new self(); + } + return $instance; + } }