Skip to content

Commit

Permalink
Added ability to log elastic requests to application logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
olsgreen committed May 8, 2020
1 parent a51d1b4 commit 6b4cf67
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/ElasticsearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,40 @@

namespace BoxedCode\Laravel\Scout;

use Laravel\Scout\EngineManager;
use Illuminate\Support\ServiceProvider;
use Elasticsearch\ClientBuilder as ElasticBuilder;
use Illuminate\Support\ServiceProvider;
use Laravel\Scout\EngineManager;
use Psr\Log\LoggerInterface;

class ElasticsearchServiceProvider extends ServiceProvider
{
public function register()
{
// Bind the elastic connection builder to the
// container if it's not already present.
if (!$this->app->bound(ElasticBuilder::class)) {
$this->app->bind(ElasticBuilder::class, function() {
return new ElasticBuilder();
});
}
}

/**
* Bootstrap the application services.
*/
public function boot()
{
app(EngineManager::class)->extend('elasticsearch', function($app) {
$client = ElasticBuilder::create()
->setHosts(config('scout.elasticsearch.hosts'))
->build();
$this->app->make(EngineManager::class)->extend('elasticsearch', function() {
$config = $this->app->make('config');

$builder = $this->app->make(ElasticBuilder::class)
->setHosts($config->get('scout.elasticsearch.hosts'));

if ($config->get('scout.elasticsearch.debug', false)) {
$builder->setLogger($this->app->make(LoggerInterface::class));
}

return new ElasticsearchEngine($client);
return new ElasticsearchEngine($builder->build());
});
}
}
135 changes: 135 additions & 0 deletions tests/ElasticsearchProviderTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Tests;

use BoxedCode\Laravel\Scout\ElasticsearchEngine;
use BoxedCode\Laravel\Scout\ElasticsearchServiceProvider;
use Elasticsearch\ClientBuilder as ElasticBuilder;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Contracts\Container\Container;
use Laravel\Scout\EngineManager;
use Mockery;
use Psr\Log\LoggerInterface;
use \Elasticsearch\Client as ElasticClient;

class ElasticsearchServiceProviderTestCase extends AbstractTestCase
{
public function test_binds_elastic_builder()
{
$app = Mockery::mock(Container::class);

$provider = new ElasticsearchServiceProvider($app);

$app->shouldReceive('bound')
->once()
->with(ElasticBuilder::class)
->andReturnFalse();

$app->shouldReceive('bind')
->once()
->with(ElasticBuilder::class, Mockery::any())
->andReturnNull();

$this->assertNull($provider->register());
}

public function test_honors_currently_bound_elastic_builder()
{
$app = Mockery::mock(Container::class);

$provider = new ElasticsearchServiceProvider($app);

$app->shouldReceive('bound')
->once()
->with(ElasticBuilder::class)
->andReturnTrue();

$this->assertNull($provider->register());
}

protected function setupEngineRegistration()
{
$app = Mockery::mock(Container::class);

$provider = new ElasticsearchServiceProvider($app);

$config = Mockery::mock(Config::class);

$app->shouldReceive('make')
->twice()
->with('config')
->andReturn($config);

$app->shouldReceive('make')
->once()
->with(EngineManager::class)
->andReturn(
$manager = new EngineManager($app)
);

$app->shouldReceive('make')
->once()
->with(ElasticBuilder::class)
->andReturn(
$builder = Mockery::mock(ElasticBuilder::class)
);

$config->shouldReceive('get')
->once()
->with('scout.elasticsearch.hosts')
->andReturn(['127.0.0.1']);

$builder->shouldReceive('setHosts')
->once()
->with(['127.0.0.1'])
->andReturnSelf();

$builder->shouldReceive('build')
->once()
->andReturn(
$elastic = Mockery::mock(ElasticClient::class)
);

return [$app, $config, $provider, $manager, $builder];
}

public function test_engine_registration()
{
[$app, $config, $provider, $manager, $builder] = $this->setupEngineRegistration();

$config->shouldReceive('get')
->once()
->with('scout.elasticsearch.debug', false)
->andReturnFalse();

$provider->boot();

$this->assertInstanceOf(ElasticsearchEngine::class, $manager->engine('elasticsearch'));
}

public function test_engine_registration_with_debug_logger()
{
[$app, $config, $provider, $manager, $builder] = $this->setupEngineRegistration();

$config->shouldReceive('get')
->once()
->with('scout.elasticsearch.debug', false)
->andReturnTrue();

$app->shouldReceive('make')
->once()
->with(LoggerInterface::class)
->andReturn(
$logger = Mockery::mock(LoggerInterface::class)
);

$builder->shouldReceive('setLogger')
->once()
->with($logger)
->andReturnSelf();

$provider->boot();

$this->assertInstanceOf(ElasticsearchEngine::class, $manager->engine('elasticsearch'));
}
}

0 comments on commit 6b4cf67

Please sign in to comment.