Skip to content

Commit

Permalink
Merge pull request #349 from mvdnbrk/upgrade-algolia-api-client-v2
Browse files Browse the repository at this point in the history
[7.0] Upgrade Algolia API client to v2
  • Loading branch information
taylorotwell committed Jan 26, 2019
2 parents 20cba0e + 7dc92a5 commit 4f6470e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"illuminate/support": "~5.4"
},
"require-dev": {
"algolia/algoliasearch-client-php": "^1.10",
"algolia/algoliasearch-client-php": "^2.2",
"mockery/mockery": "~1.0",
"phpunit/phpunit": "~6.0"
},
Expand All @@ -52,7 +52,7 @@
}
},
"suggest": {
"algolia/algoliasearch-client-php": "Required to use the Algolia engine (^1.10)."
"algolia/algoliasearch-client-php": "Required to use the Algolia engine (^2.2)."
},
"config": {
"sort-packages": true
Expand Down
7 changes: 2 additions & 5 deletions src/EngineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace Laravel\Scout;

use Illuminate\Support\Manager;
use AlgoliaSearch\Client as Algolia;
use Laravel\Scout\Engines\NullEngine;
use Laravel\Scout\Engines\AlgoliaEngine;
use AlgoliaSearch\Version as AlgoliaUserAgent;
use Algolia\AlgoliaSearch\SearchClient as Algolia;

class EngineManager extends Manager
{
Expand All @@ -28,9 +27,7 @@ public function engine($name = null)
*/
public function createAlgoliaDriver()
{
AlgoliaUserAgent::addSuffixUserAgentSegment('Laravel Scout', '3.0.10');

return new AlgoliaEngine(new Algolia(
return new AlgoliaEngine(Algolia::create(
config('scout.algolia.id'), config('scout.algolia.secret')
));
}
Expand Down
12 changes: 6 additions & 6 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
namespace Laravel\Scout\Engines;

use Laravel\Scout\Builder;
use AlgoliaSearch\Client as Algolia;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\SoftDeletes;
use Algolia\AlgoliaSearch\SearchClient as Algolia;

class AlgoliaEngine extends Engine
{
/**
* The Algolia client.
*
* @var \AlgoliaSearch\Client
* @var \Algolia\AlgoliaSearch\SearchClient
*/
protected $algolia;

/**
* Create a new engine instance.
*
* @param \AlgoliaSearch\Client $algolia
* @param \Algolia\AlgoliaSearch\SearchClient $algolia
* @return void
*/
public function __construct(Algolia $algolia)
Expand All @@ -31,7 +31,7 @@ public function __construct(Algolia $algolia)
* Update the given model in the index.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @throws \AlgoliaSearch\AlgoliaException
* @throws \Algolia\AlgoliaSearch\Exceptions\AlgoliaException
* @return void
*/
public function update($models)
Expand Down Expand Up @@ -59,7 +59,7 @@ public function update($models)
})->filter()->values()->all();

if (! empty($objects)) {
$index->addObjects($objects);
$index->saveObjects($objects);
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ public function flush($model)
{
$index = $this->algolia->initIndex($model->searchableAs());

$index->clearIndex();
$index->clearObjects();
}

/**
Expand Down
24 changes: 12 additions & 12 deletions tests/AlgoliaEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public function tearDown()

public function test_update_adds_objects_to_index()
{
$client = m::mock('AlgoliaSearch\Client');
$client = m::mock('Algolia\AlgoliaSearch\SearchClient');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock('StdClass'));
$index->shouldReceive('addObjects')->with([[
$index->shouldReceive('saveObjects')->with([[
'id' => 1,
'objectID' => 1,
]]);
Expand All @@ -33,7 +33,7 @@ public function test_update_adds_objects_to_index()

public function test_delete_removes_objects_to_index()
{
$client = m::mock('AlgoliaSearch\Client');
$client = m::mock('Algolia\AlgoliaSearch\SearchClient');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock('StdClass'));
$index->shouldReceive('deleteObjects')->with([1]);

Expand All @@ -43,7 +43,7 @@ public function test_delete_removes_objects_to_index()

public function test_search_sends_correct_parameters_to_algolia()
{
$client = m::mock('AlgoliaSearch\Client');
$client = m::mock('Algolia\AlgoliaSearch\SearchClient');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock('StdClass'));
$index->shouldReceive('search')->with('zonda', [
'numericFilters' => ['foo=1'],
Expand All @@ -57,7 +57,7 @@ public function test_search_sends_correct_parameters_to_algolia()

public function test_map_correctly_maps_results_to_models()
{
$client = m::mock('AlgoliaSearch\Client');
$client = m::mock('Algolia\AlgoliaSearch\SearchClient');
$engine = new AlgoliaEngine($client);

$model = m::mock('StdClass');
Expand All @@ -76,9 +76,9 @@ public function test_map_correctly_maps_results_to_models()

public function test_a_model_is_indexed_with_a_custom_algolia_key()
{
$client = m::mock('AlgoliaSearch\Client');
$client = m::mock('Algolia\AlgoliaSearch\SearchClient');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock('StdClass'));
$index->shouldReceive('addObjects')->with([[
$index->shouldReceive('saveObjects')->with([[
'id' => 1,
'objectID' => 'my-algolia-key.1',
]]);
Expand All @@ -89,7 +89,7 @@ public function test_a_model_is_indexed_with_a_custom_algolia_key()

public function test_a_model_is_removed_with_a_custom_algolia_key()
{
$client = m::mock('AlgoliaSearch\Client');
$client = m::mock('Algolia\AlgoliaSearch\SearchClient');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock('StdClass'));
$index->shouldReceive('deleteObjects')->with(['my-algolia-key.1']);

Expand All @@ -99,19 +99,19 @@ public function test_a_model_is_removed_with_a_custom_algolia_key()

public function test_flush_a_model()
{
$client = m::mock('AlgoliaSearch\Client');
$client = m::mock('Algolia\AlgoliaSearch\SearchClient');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock('StdClass'));
$index->shouldReceive('clearIndex');
$index->shouldReceive('clearObjects');

$engine = new AlgoliaEngine($client);
$engine->flush(new AlgoliaEngineTestCustomKeyModel);
}

public function test_update_empty_searchable_array_does_not_add_objects_to_index()
{
$client = m::mock('AlgoliaSearch\Client');
$client = m::mock('Algolia\AlgoliaSearch\SearchClient');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock('StdClass'));
$index->shouldNotReceive('addObjects');
$index->shouldNotReceive('saveObjects');

$engine = new AlgoliaEngine($client);
$engine->update(Collection::make([new EmptyTestModel]));
Expand Down

0 comments on commit 4f6470e

Please sign in to comment.