diff --git a/phpunit.xml b/phpunit.xml index c2b7dc2dd..989295b4d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -9,6 +9,7 @@ ./src/circuit-breaker/tests ./src/command/tests ./src/config/tests + ./src/config-apollo/tests ./src/config-center/tests ./src/config-nacos/tests ./src/config-zookeeper/tests @@ -86,6 +87,7 @@ ./src/circuit-breaker/src ./src/command/src ./src/config/src + ./src/config-apollo/src ./src/config-center/src ./src/config-nacos/src ./src/constants/src diff --git a/src/config-apollo/tests/ClientTest.php b/src/config-apollo/tests/ClientTest.php index 75ad977f3..f3a77d9b8 100644 --- a/src/config-apollo/tests/ClientTest.php +++ b/src/config-apollo/tests/ClientTest.php @@ -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')); } }