forked from apoloz/alfa-bank-sdk
refactor(core): enhance SDK structure and documentation
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
128
tests/Pest.php
Normal file
128
tests/Pest.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
use Apoloz\AlfaBank\Core\AlfaConfig;
|
||||
|
||||
/**
|
||||
* Define a mock app() helper for tests running outside of Laravel.
|
||||
*/
|
||||
if (!function_exists('app')) {
|
||||
function app($abstract = null, array $parameters = []) {
|
||||
static $container = [];
|
||||
|
||||
if ($abstract === null) {
|
||||
return new class {
|
||||
public function make($abstract) { return app($abstract); }
|
||||
};
|
||||
}
|
||||
|
||||
if (isset($container[$abstract])) {
|
||||
return $container[$abstract];
|
||||
}
|
||||
|
||||
// Minimal implementations for laravel-data
|
||||
if ($abstract === \Spatie\LaravelData\Support\DataConfig::class) {
|
||||
if (isset($container[$abstract])) return $container[$abstract];
|
||||
return $container[$abstract] = \Spatie\LaravelData\Support\DataConfig::createFromConfig(config('data'));
|
||||
}
|
||||
|
||||
if ($abstract === \Spatie\LaravelData\Support\DataContainer::class) {
|
||||
return \Spatie\LaravelData\Support\DataContainer::get();
|
||||
}
|
||||
|
||||
// Auto-wiring via Reflection
|
||||
try {
|
||||
$reflector = new ReflectionClass($abstract);
|
||||
|
||||
if (! $reflector->isInstantiable()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$constructor = $reflector->getConstructor();
|
||||
|
||||
if (is_null($constructor)) {
|
||||
return $container[$abstract] = new $abstract();
|
||||
}
|
||||
|
||||
$dependencies = [];
|
||||
foreach ($constructor->getParameters() as $parameter) {
|
||||
$type = $parameter->getType();
|
||||
if ($type instanceof ReflectionNamedType && ! $type->isBuiltin()) {
|
||||
$dependencies[] = app($type->getName());
|
||||
} else {
|
||||
if ($parameter->isDefaultValueAvailable()) {
|
||||
$dependencies[] = $parameter->getDefaultValue();
|
||||
} else {
|
||||
$dependencies[] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $container[$abstract] = $reflector->newInstanceArgs($dependencies);
|
||||
} catch (\Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Define a mock config() helper for tests running outside of Laravel.
|
||||
*/
|
||||
if (!function_exists('config')) {
|
||||
function config($key = null, $default = null) {
|
||||
$dataConfig = [
|
||||
'validation_strategy' => 'disabled',
|
||||
'transformers' => [],
|
||||
'casters' => [],
|
||||
'rule_inferrers' => [],
|
||||
'normalizers' => [
|
||||
\Spatie\LaravelData\Normalizers\ModelNormalizer::class,
|
||||
\Spatie\LaravelData\Normalizers\FormRequestNormalizer::class,
|
||||
\Spatie\LaravelData\Normalizers\ArrayableNormalizer::class,
|
||||
\Spatie\LaravelData\Normalizers\ObjectNormalizer::class,
|
||||
\Spatie\LaravelData\Normalizers\ArrayNormalizer::class,
|
||||
\Spatie\LaravelData\Normalizers\JsonNormalizer::class,
|
||||
],
|
||||
'features' => [
|
||||
'cast_and_transform_iterables' => true,
|
||||
],
|
||||
'max_transformation_depth' => null,
|
||||
'throw_when_max_transformation_depth_reached' => true,
|
||||
'wrap_execution_type' => 'disabled',
|
||||
'var_dumper_caster_mode' => 'narrow',
|
||||
];
|
||||
|
||||
if ($key === 'data') {
|
||||
return $dataConfig;
|
||||
}
|
||||
|
||||
if (is_string($key) && str_starts_with($key, 'data.')) {
|
||||
$subKey = substr($key, 5);
|
||||
// Handle simple dot notation for one level deep
|
||||
if (array_key_exists($subKey, $dataConfig)) {
|
||||
return $dataConfig[$subKey];
|
||||
}
|
||||
// Handle features.key
|
||||
if (str_starts_with($subKey, 'features.')) {
|
||||
$featureKey = substr($subKey, 9);
|
||||
return $dataConfig['features'][$featureKey] ?? $default;
|
||||
}
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to create a test config.
|
||||
*/
|
||||
function createTestConfig(): AlfaConfig {
|
||||
// Create temp files to satisfy file_exists checks
|
||||
$cert = tempnam(sys_get_temp_dir(), 'cert');
|
||||
$key = tempnam(sys_get_temp_dir(), 'key');
|
||||
|
||||
return new AlfaConfig(
|
||||
baseUrl: 'https://test.alfabank.ru',
|
||||
certificatePath: $cert,
|
||||
privateKeyPath: $key
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user