Fixed apollo test cases (#5944)

This commit is contained in:
李铭昕 2023-07-14 17:10:35 +08:00 committed by GitHub
parent d367f2eb2b
commit 7c8f924545
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 24 deletions

View File

@ -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>

View File

@ -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'));
}
}