mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 03:37:44 +08:00
Fixed apollo test cases (#5944)
This commit is contained in:
parent
d367f2eb2b
commit
7c8f924545
@ -9,6 +9,7 @@
|
||||
<directory suffix="Test.php">./src/circuit-breaker/tests</directory>
|
||||
<directory suffix="Test.php">./src/command/tests</directory>
|
||||
<directory suffix="Test.php">./src/config/tests</directory>
|
||||
<directory suffix="Test.php">./src/config-apollo/tests</directory>
|
||||
<directory suffix="Test.php">./src/config-center/tests</directory>
|
||||
<directory suffix="Test.php">./src/config-nacos/tests</directory>
|
||||
<directory suffix="Test.php">./src/config-zookeeper/tests</directory>
|
||||
@ -86,6 +87,7 @@
|
||||
<directory suffix=".php">./src/circuit-breaker/src</directory>
|
||||
<directory suffix=".php">./src/command/src</directory>
|
||||
<directory suffix=".php">./src/config/src</directory>
|
||||
<directory suffix=".php">./src/config-apollo/src</directory>
|
||||
<directory suffix=".php">./src/config-center/src</directory>
|
||||
<directory suffix=".php">./src/config-nacos/src</directory>
|
||||
<directory suffix=".php">./src/constants/src</directory>
|
||||
|
@ -11,12 +11,18 @@ declare(strict_types=1);
|
||||
*/
|
||||
namespace HyperfTest\ConfigApollo;
|
||||
|
||||
use GuzzleHttp;
|
||||
use Hyperf\Codec\Json;
|
||||
use Hyperf\Config\Config;
|
||||
use Hyperf\ConfigApollo\ApolloDriver;
|
||||
use Hyperf\ConfigApollo\Client;
|
||||
use Hyperf\ConfigApollo\ClientInterface;
|
||||
use Hyperf\ConfigApollo\Option;
|
||||
use Hyperf\Context\ApplicationContext;
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use Hyperf\Guzzle\ClientFactory;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\HttpMessage\Base\Response;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\Attributes\CoversNothing;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@ -36,31 +42,37 @@ class ClientTest extends TestCase
|
||||
$container = Mockery::mock(ContainerInterface::class);
|
||||
$configInstance = new Config([]);
|
||||
$configInstance->set('apollo.test-key', 'pre-value');
|
||||
$configInstance->set('config_center.drivers.apollo.namespaces', ['application']);
|
||||
// drivers.apollo.namespaces
|
||||
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn($configInstance);
|
||||
ApplicationContext::setContainer($container);
|
||||
$callbacks = [
|
||||
'application' => function ($configs) {
|
||||
$container = ApplicationContext::getContainer();
|
||||
$config = $container->get(ConfigInterface::class);
|
||||
// Mock the configurations.
|
||||
$configs['configurations'] = [
|
||||
'apollo.test-key' => 'after-value',
|
||||
];
|
||||
foreach ($configs['configurations'] ?? [] as $key => $value) {
|
||||
$config->set($key, $value);
|
||||
}
|
||||
},
|
||||
];
|
||||
$client = new Client($option, $callbacks, function (array $options = []) use ($container) {
|
||||
return (new ClientFactory($container))->create($options);
|
||||
});
|
||||
$client->pull([
|
||||
'application',
|
||||
]);
|
||||
|
||||
$config = $container->get(ConfigInterface::class);
|
||||
$this->assertSame('after-value', $config->get('apollo.test-key'));
|
||||
$this->assertSame([
|
||||
'test-key' => 'after-value',
|
||||
], $config->get('apollo'));
|
||||
$client = new Client(
|
||||
$option,
|
||||
function (array $options = []) {
|
||||
$client = Mockery::mock(GuzzleHttp\Client::class);
|
||||
$response = (new Response())->setStatus(200)->addHeader('content-type', 'application/json')
|
||||
->setBody(new SwooleStream(Json::encode([
|
||||
'configurations' => [
|
||||
'apollo.test-key' => 'after-value',
|
||||
],
|
||||
])));
|
||||
$client->shouldReceive('get')->andReturn($response);
|
||||
return $client;
|
||||
},
|
||||
$config,
|
||||
$logger = Mockery::mock(StdoutLoggerInterface::class)
|
||||
);
|
||||
$res = $client->pull();
|
||||
$this->assertSame(['application' => ['apollo.test-key' => 'after-value']], $res);
|
||||
|
||||
$container->shouldReceive('get')->with(ClientInterface::class)->andReturn($client);
|
||||
$container->shouldReceive('get')->with(StdoutLoggerInterface::class)->andReturn($logger);
|
||||
$logger->shouldReceive('debug')->withAnyArgs()->andReturnNull();
|
||||
$driver = new ApolloDriver($container);
|
||||
$driver->fetchConfig();
|
||||
|
||||
$this->assertSame(['test-key' => 'after-value'], $config->get('apollo'));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user