mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-04 04:37:46 +08:00
Added testing.
This commit is contained in:
parent
a0e7aa931b
commit
f34add7f0b
@ -36,15 +36,7 @@ class ProviderConfig
|
|||||||
{
|
{
|
||||||
if (! static::$privoderConfigs) {
|
if (! static::$privoderConfigs) {
|
||||||
$providers = Composer::getMergedExtra('hyperf')['config'] ?? [];
|
$providers = Composer::getMergedExtra('hyperf')['config'] ?? [];
|
||||||
$providerConfigs = [];
|
static::$privoderConfigs = static::loadProviders($providers);
|
||||||
foreach ($providers ?? [] as $provider) {
|
|
||||||
if (is_string($provider) && class_exists($provider) && method_exists($provider, '__invoke')) {
|
|
||||||
$providerConfigs[] = (new $provider())();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static::$privoderConfigs = static::merge(...$providerConfigs);
|
|
||||||
unset($providerConfigs);
|
|
||||||
}
|
}
|
||||||
return static::$privoderConfigs;
|
return static::$privoderConfigs;
|
||||||
}
|
}
|
||||||
@ -54,6 +46,18 @@ class ProviderConfig
|
|||||||
static::$privoderConfigs = [];
|
static::$privoderConfigs = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static function loadProviders(array $providers): array
|
||||||
|
{
|
||||||
|
$providerConfigs = [];
|
||||||
|
foreach ($providers ?? [] as $provider) {
|
||||||
|
if (is_string($provider) && class_exists($provider) && method_exists($provider, '__invoke')) {
|
||||||
|
$providerConfigs[] = (new $provider())();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return static::merge(...$providerConfigs);
|
||||||
|
}
|
||||||
|
|
||||||
protected static function merge(...$arrays): array
|
protected static function merge(...$arrays): array
|
||||||
{
|
{
|
||||||
$result = array_merge_recursive(...$arrays);
|
$result = array_merge_recursive(...$arrays);
|
||||||
|
@ -12,6 +12,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace HyperfTest\Config;
|
namespace HyperfTest\Config;
|
||||||
|
|
||||||
|
use Hyperf\Utils\Arr;
|
||||||
|
use HyperfTest\Config\Stub\FooConfigProvider;
|
||||||
use HyperfTest\Config\Stub\ProviderConfig;
|
use HyperfTest\Config\Stub\ProviderConfig;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
@ -87,4 +89,36 @@ class ProviderConfigTest extends TestCase
|
|||||||
$result = ProviderConfig::merge($c1, $c2);
|
$result = ProviderConfig::merge($c1, $c2);
|
||||||
$this->assertSame(['L1', null], $result['listeners']);
|
$this->assertSame(['L1', null], $result['listeners']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testProviderConfigLoadProviders()
|
||||||
|
{
|
||||||
|
$config = json_decode(file_get_contents(BASE_PATH . '/composer.json'), true);
|
||||||
|
|
||||||
|
$providers = $config['extra']['hyperf']['config'];
|
||||||
|
|
||||||
|
$res = ProviderConfig::loadProviders($providers);
|
||||||
|
|
||||||
|
$dependencies = $res['dependencies'];
|
||||||
|
$commands = $res['commands'];
|
||||||
|
$scanPaths = $res['scan']['paths'];
|
||||||
|
$publish = $res['publish'];
|
||||||
|
$listeners = $res['listeners'];
|
||||||
|
|
||||||
|
$this->assertFalse(Arr::isAssoc($commands));
|
||||||
|
$this->assertFalse(Arr::isAssoc($scanPaths));
|
||||||
|
$this->assertFalse(Arr::isAssoc($listeners));
|
||||||
|
$this->assertFalse(Arr::isAssoc($publish));
|
||||||
|
$this->assertTrue(Arr::isAssoc($dependencies));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProviderConfigLoadProvidersHasCallable()
|
||||||
|
{
|
||||||
|
$res = ProviderConfig::loadProviders([
|
||||||
|
FooConfigProvider::class,
|
||||||
|
]);
|
||||||
|
|
||||||
|
foreach ($res['dependencies'] as $dependency) {
|
||||||
|
$this->assertIsCallable($dependency);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
28
src/config/tests/Stub/Foo.php
Normal file
28
src/config/tests/Stub/Foo.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
/**
|
||||||
|
* This file is part of Hyperf.
|
||||||
|
*
|
||||||
|
* @link https://www.hyperf.io
|
||||||
|
* @document https://doc.hyperf.io
|
||||||
|
* @contact group@hyperf.io
|
||||||
|
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace HyperfTest\Config\Stub;
|
||||||
|
|
||||||
|
class Foo
|
||||||
|
{
|
||||||
|
public $id;
|
||||||
|
|
||||||
|
public function __construct($id = 0)
|
||||||
|
{
|
||||||
|
$this->id = $id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function make()
|
||||||
|
{
|
||||||
|
return new self(2);
|
||||||
|
}
|
||||||
|
}
|
28
src/config/tests/Stub/FooConfigProvider.php
Normal file
28
src/config/tests/Stub/FooConfigProvider.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
/**
|
||||||
|
* This file is part of Hyperf.
|
||||||
|
*
|
||||||
|
* @link https://www.hyperf.io
|
||||||
|
* @document https://doc.hyperf.io
|
||||||
|
* @contact group@hyperf.io
|
||||||
|
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace HyperfTest\Config\Stub;
|
||||||
|
|
||||||
|
class FooConfigProvider
|
||||||
|
{
|
||||||
|
public function __invoke()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'dependencies' => [
|
||||||
|
'Foo' => function () {
|
||||||
|
return new Foo(1);
|
||||||
|
},
|
||||||
|
'Foo2' => [Foo::class, 'make'],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -14,6 +14,11 @@ namespace HyperfTest\Config\Stub;
|
|||||||
|
|
||||||
class ProviderConfig extends \Hyperf\Config\ProviderConfig
|
class ProviderConfig extends \Hyperf\Config\ProviderConfig
|
||||||
{
|
{
|
||||||
|
public static function loadProviders(array $providers): array
|
||||||
|
{
|
||||||
|
return parent::loadProviders($providers);
|
||||||
|
}
|
||||||
|
|
||||||
public static function merge(...$arrays): array
|
public static function merge(...$arrays): array
|
||||||
{
|
{
|
||||||
return parent::merge(...$arrays);
|
return parent::merge(...$arrays);
|
||||||
|
Loading…
Reference in New Issue
Block a user