From 6b5afdf807b63915b89e599321be191ebfc455d9 Mon Sep 17 00:00:00 2001 From: Oleg Zhulnev Date: Sat, 27 Jan 2024 22:54:54 +0300 Subject: [PATCH] Add more tests --- src/Client.php | 2 +- tests/ClientTest.php | 72 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 031fbc245..a68c481a8 100644 --- a/src/Client.php +++ b/src/Client.php @@ -637,7 +637,7 @@ protected function _buildTransport(array $config): Transport // API key if (!empty($config['api_key'])) { if (!empty($config['username'])) { - throw new InvalidException('You cannot use APIKey and Basic Authentication together'); + throw new InvalidException('You cannot use APIKey and Basic Authentication together.'); } $transport->setHeader('Authorization', \sprintf('ApiKey %s', $config['api_key'])); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 2c717b062..6ca3f7300 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -6,6 +6,9 @@ use Elastica\ClientConfiguration; use Elastica\Exception\InvalidException; use Elastica\Test\Base as BaseTest; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; +use Stringable; /** * @group unit @@ -154,4 +157,73 @@ public function testClientConnectionWithCloudId(): void $this->assertEquals('4de46ced8d8d459696e544fe5f32b999.eu-central-1.aws.cloud.es.io', $node->getUri()->getHost()); } + + public function testItSetLogger(): void + { + $client = new Client(); + + self::assertInstanceOf(NullLogger::class, $client->getLogger()); + + $client->setLogger( + new class() implements LoggerInterface { + public function emergency(Stringable|string $message, array $context = []): void + { + } + + public function alert(Stringable|string $message, array $context = []): void + { + } + + public function critical(Stringable|string $message, array $context = []): void + { + } + + public function error(Stringable|string $message, array $context = []): void + { + } + + public function warning(Stringable|string $message, array $context = []): void + { + } + + public function notice(Stringable|string $message, array $context = []): void + { + } + + public function info(Stringable|string $message, array $context = []): void + { + } + + public function debug(Stringable|string $message, array $context = []): void + { + } + + public function log($level, Stringable|string $message, array $context = []): void + { + } + } + ); + + self::assertNotInstanceOf(NullLogger::class, $client->getLogger()); + } + + public function testItThrowsAnExceptionWhenApiKeyAndUserNameInConfigAtTheSameTime(): void + { + $this->expectException(InvalidException::class); + $this->expectExceptionMessage('You cannot use APIKey and Basic Authentication together.'); + + new Client([ + 'username' => 'user', + 'api_key' => 'key', + ]); + } + + public function testItSetsAuthorizationHeaderIfApiKeyPassed(): void + { + $apiKey = 'key'; + + $client = new Client(['api_key' => $apiKey]); + + self::assertSame(['Authorization' => \sprintf('ApiKey %s', $apiKey)], $client->getTransport()->getHeaders()); + } }