Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sidz committed Jan 27, 2024
1 parent ff9abe7 commit 6b5afdf
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
Expand Down
72 changes: 72 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());

Check failure on line 207 in tests/ClientTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to static method PHPUnit\Framework\Assert::assertNotInstanceOf() with 'Psr\\Log\\NullLogger' and Psr\Log\NullLogger will always evaluate to false.
}

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

0 comments on commit 6b5afdf

Please sign in to comment.