Skip to content

Commit

Permalink
ClientBuilder: setLogger() and setTracer() only accept \Psr\Log\Logge…
Browse files Browse the repository at this point in the history
…rInterface (#673)

This is a more verbose approach than directly adding typehints, but it
won't break the projects which may be extending ClientBuilder.
  • Loading branch information
mhujer authored and polyfractal committed Dec 5, 2017
1 parent 6d467fa commit 0270c4f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions phpstan.neon
Expand Up @@ -7,3 +7,6 @@ parameters:
# because of \Elasticsearch\Tests\RegisteredNamespaceTest
- '#Call to an undefined method Elasticsearch\\Client::foo\(\)#'
- '#Call to an undefined method Elasticsearch\\Client::bar\(\)#'

# because of \Elasticsearch\Tests\ClientBuilderTest
- '#expects Psr\\Log\\LoggerInterface, Elasticsearch\\Tests\\ClientBuilder\\DummyLogger given.$#'
8 changes: 8 additions & 0 deletions src/Elasticsearch/ClientBuilder.php
Expand Up @@ -307,6 +307,10 @@ public function setHandler($handler)
*/
public function setLogger($logger)
{
if (!$logger instanceof LoggerInterface) {
throw new InvalidArgumentException('$logger must implement \Psr\Log\LoggerInterface!');
}

$this->logger = $logger;

return $this;
Expand All @@ -318,6 +322,10 @@ public function setLogger($logger)
*/
public function setTracer($tracer)
{
if (!$tracer instanceof LoggerInterface) {
throw new InvalidArgumentException('$tracer must implement \Psr\Log\LoggerInterface!');
}

$this->tracer = $tracer;

return $this;
Expand Down
9 changes: 9 additions & 0 deletions tests/Elasticsearch/Tests/ClientBuilder/DummyLogger.php
@@ -0,0 +1,9 @@
<?php
declare(strict_types = 1);

namespace Elasticsearch\Tests\ClientBuilder;

class DummyLogger
{

}
29 changes: 29 additions & 0 deletions tests/Elasticsearch/Tests/ClientBuilderTest.php
@@ -0,0 +1,29 @@
<?php

declare(strict_types = 1);

namespace Elasticsearch\Tests;

use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class ClientBuilderTest extends TestCase
{

public function testClientBuilderThrowsExceptionForIncorrectLoggerClass()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('$logger must implement \Psr\Log\LoggerInterface!');

ClientBuilder::create()->setLogger(new \Elasticsearch\Tests\ClientBuilder\DummyLogger());
}

public function testClientBuilderThrowsExceptionForIncorrectTracerClass()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('$tracer must implement \Psr\Log\LoggerInterface!');

ClientBuilder::create()->setTracer(new \Elasticsearch\Tests\ClientBuilder\DummyLogger());
}
}

0 comments on commit 0270c4f

Please sign in to comment.