Added testing.

This commit is contained in:
李铭昕 2019-08-07 10:23:45 +08:00
parent a0e7aa931b
commit f34add7f0b
5 changed files with 108 additions and 9 deletions

View File

@ -36,15 +36,7 @@ class ProviderConfig
{
if (! static::$privoderConfigs) {
$providers = Composer::getMergedExtra('hyperf')['config'] ?? [];
$providerConfigs = [];
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);
static::$privoderConfigs = static::loadProviders($providers);
}
return static::$privoderConfigs;
}
@ -54,6 +46,18 @@ class ProviderConfig
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
{
$result = array_merge_recursive(...$arrays);

View File

@ -12,6 +12,8 @@ declare(strict_types=1);
namespace HyperfTest\Config;
use Hyperf\Utils\Arr;
use HyperfTest\Config\Stub\FooConfigProvider;
use HyperfTest\Config\Stub\ProviderConfig;
use PHPUnit\Framework\TestCase;
@ -87,4 +89,36 @@ class ProviderConfigTest extends TestCase
$result = ProviderConfig::merge($c1, $c2);
$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);
}
}
}

View 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);
}
}

View 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'],
],
];
}
}

View File

@ -14,6 +14,11 @@ namespace HyperfTest\Config\Stub;
class ProviderConfig extends \Hyperf\Config\ProviderConfig
{
public static function loadProviders(array $providers): array
{
return parent::loadProviders($providers);
}
public static function merge(...$arrays): array
{
return parent::merge(...$arrays);