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 ec08c3a
Show file tree
Hide file tree
Showing 2 changed files with 72 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
71 changes: 71 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Elastica\ClientConfiguration;
use Elastica\Exception\InvalidException;
use Elastica\Test\Base as BaseTest;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

/**
* @group unit
Expand Down Expand Up @@ -154,4 +156,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($message, array $context = [])

Check failure on line 168 in tests/ClientTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Return type mixed of method class@anonymous/tests/ClientTest.php:167::emergency() is not covariant with return type void of method Psr\Log\LoggerInterface::emergency().
{
}

public function alert($message, array $context = [])

Check failure on line 172 in tests/ClientTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Return type mixed of method class@anonymous/tests/ClientTest.php:167::alert() is not covariant with return type void of method Psr\Log\LoggerInterface::alert().
{
}

public function critical($message, array $context = [])

Check failure on line 176 in tests/ClientTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Return type mixed of method class@anonymous/tests/ClientTest.php:167::critical() is not covariant with return type void of method Psr\Log\LoggerInterface::critical().
{
}

public function error($message, array $context = [])

Check failure on line 180 in tests/ClientTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Return type mixed of method class@anonymous/tests/ClientTest.php:167::error() is not covariant with return type void of method Psr\Log\LoggerInterface::error().
{
}

public function warning($message, array $context = [])

Check failure on line 184 in tests/ClientTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Return type mixed of method class@anonymous/tests/ClientTest.php:167::warning() is not covariant with return type void of method Psr\Log\LoggerInterface::warning().
{
}

public function notice($message, array $context = [])

Check failure on line 188 in tests/ClientTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Return type mixed of method class@anonymous/tests/ClientTest.php:167::notice() is not covariant with return type void of method Psr\Log\LoggerInterface::notice().
{
}

public function info($message, array $context = [])

Check failure on line 192 in tests/ClientTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Return type mixed of method class@anonymous/tests/ClientTest.php:167::info() is not covariant with return type void of method Psr\Log\LoggerInterface::info().
{
}

public function debug($message, array $context = [])

Check failure on line 196 in tests/ClientTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Return type mixed of method class@anonymous/tests/ClientTest.php:167::debug() is not covariant with return type void of method Psr\Log\LoggerInterface::debug().
{
}

public function log($level, $message, array $context = [])

Check failure on line 200 in tests/ClientTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Return type mixed of method class@anonymous/tests/ClientTest.php:167::log() is not covariant with return type void of method Psr\Log\LoggerInterface::log().
{
}
}
);

self::assertNotInstanceOf(NullLogger::class, $client->getLogger());

Check failure on line 206 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 ec08c3a

Please sign in to comment.