Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 39 additions & 6 deletions src/SapRfcModuleMocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string> Valid SAP RFC module function or class method names.
Expand All @@ -30,6 +29,11 @@ class SapRfcModuleMocks extends Container
*/
protected static string $requireFile;

/**
* @var array<string, Closure> In-memory storage of mocked logic.
*/
private array $storage = [];

/**
* Set the file to require.
* @param string $file
Expand Down Expand Up @@ -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];
}

/**
Expand All @@ -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
Expand All @@ -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;
}
}