Skip to content

Commit

Permalink
Merge pull request #333 from driesvints/unify-test-namespaces
Browse files Browse the repository at this point in the history
[6.0] Unify test namespaces
  • Loading branch information
driesvints authored Dec 6, 2018
2 parents f470bbb + 828d9fb commit a05a238
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 76 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
"Laravel\\Scout\\Tests\\": "tests/"
}
},
"extra": {
Expand Down
16 changes: 0 additions & 16 deletions tests/AbstractTestCase.php

This file was deleted.

52 changes: 29 additions & 23 deletions tests/AlgoliaEngineTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
<?php

namespace Tests;
namespace Laravel\Scout\Tests;

use Mockery;
use Mockery as m;
use Laravel\Scout\Builder;
use Tests\Fixtures\EmptyTestModel;
use PHPUnit\Framework\TestCase;
use Laravel\Scout\Engines\AlgoliaEngine;
use Tests\Fixtures\AlgoliaEngineTestModel;
use Illuminate\Database\Eloquent\Collection;
use Tests\Fixtures\AlgoliaEngineTestCustomKeyModel;
use Laravel\Scout\Tests\Fixtures\EmptyTestModel;
use Laravel\Scout\Tests\Fixtures\AlgoliaEngineTestModel;
use Laravel\Scout\Tests\Fixtures\AlgoliaEngineTestCustomKeyModel;

class AlgoliaEngineTest extends AbstractTestCase
class AlgoliaEngineTest extends TestCase
{
public function tearDown()
{
m::close();
}

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

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

$engine = new AlgoliaEngine($client);
Expand All @@ -37,8 +43,8 @@ public function test_delete_removes_objects_to_index()

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

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

$model = Mockery::mock('StdClass');
$model = m::mock('StdClass');
$model->shouldReceive('newQuery')->andReturn($model);
$model->shouldReceive('getKeyName')->andReturn('id');
$model->shouldReceive('getScoutModelsByIds')->andReturn(Collection::make([new AlgoliaEngineTestModel]));

$builder = Mockery::mock(Builder::class);
$builder = m::mock(Builder::class);

$results = $engine->map($builder, ['nbHits' => 1, 'hits' => [
['objectID' => 1, 'id' => 1],
Expand All @@ -70,8 +76,8 @@ public function test_map_correctly_maps_results_to_models()

public function test_a_model_is_indexed_with_a_custom_algolia_key()
{
$client = Mockery::mock('AlgoliaSearch\Client');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = Mockery::mock('StdClass'));
$client = m::mock('AlgoliaSearch\Client');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock('StdClass'));
$index->shouldReceive('addObjects')->with([[
'id' => 1,
'objectID' => 'my-algolia-key.1',
Expand All @@ -83,8 +89,8 @@ 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 = Mockery::mock('AlgoliaSearch\Client');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = Mockery::mock('StdClass'));
$client = m::mock('AlgoliaSearch\Client');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock('StdClass'));
$index->shouldReceive('deleteObjects')->with(['my-algolia-key.1']);

$engine = new AlgoliaEngine($client);
Expand All @@ -93,8 +99,8 @@ public function test_a_model_is_removed_with_a_custom_algolia_key()

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

$engine = new AlgoliaEngine($client);
Expand All @@ -103,8 +109,8 @@ public function test_flush_a_model()

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

$engine = new AlgoliaEngine($client);
Expand Down
26 changes: 16 additions & 10 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<?php

namespace Tests;
namespace Laravel\Scout\Tests;

use Mockery;
use StdClass;
use stdClass;
use Mockery as m;
use Laravel\Scout\Builder;
use PHPUnit\Framework\TestCase;
use Illuminate\Pagination\Paginator;
use Illuminate\Database\Eloquent\Collection;

class BuilderTest extends AbstractTestCase
class BuilderTest extends TestCase
{
public function tearDown()
{
m::close();
}

public function test_pagination_correctly_handles_paginated_results()
{
Paginator::currentPageResolver(function () {
Expand All @@ -19,12 +25,12 @@ public function test_pagination_correctly_handles_paginated_results()
return 'http://localhost/foo';
});

$builder = new Builder($model = Mockery::mock(), 'zonda');
$builder = new Builder($model = m::mock(), 'zonda');
$model->shouldReceive('getPerPage')->andReturn(15);
$model->shouldReceive('searchableUsing')->andReturn($engine = Mockery::mock());
$model->shouldReceive('searchableUsing')->andReturn($engine = m::mock());

$engine->shouldReceive('paginate');
$engine->shouldReceive('map')->andReturn(Collection::make([new StdClass]));
$engine->shouldReceive('map')->andReturn(Collection::make([new stdClass]));
$engine->shouldReceive('getTotalCount');

$builder->paginate();
Expand All @@ -36,22 +42,22 @@ public function test_macroable()
return 'bar';
});

$builder = new Builder($model = Mockery::mock(), 'zonda');
$builder = new Builder($model = m::mock(), 'zonda');
$this->assertEquals(
'bar', $builder->foo()
);
}

public function test_hard_delete_doesnt_set_wheres()
{
$builder = new Builder($model = Mockery::mock(), 'zonda', null, false);
$builder = new Builder($model = m::mock(), 'zonda', null, false);

$this->assertArrayNotHasKey('__soft_deleted', $builder->wheres);
}

public function test_soft_delete_sets_wheres()
{
$builder = new Builder($model = Mockery::mock(), 'zonda', null, true);
$builder = new Builder($model = m::mock(), 'zonda', null, true);

$this->assertEquals(0, $builder->wheres['__soft_deleted']);
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Fixtures/AlgoliaEngineTestCustomKeyModel.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace Tests\Fixtures;
namespace Laravel\Scout\Tests\Fixtures;

class AlgoliaEngineTestCustomKeyModel extends TestModel
{
public function getScoutKey() {
public function getScoutKey()
{
return 'my-algolia-key.'.$this->getKey();
}
}
2 changes: 1 addition & 1 deletion tests/Fixtures/AlgoliaEngineTestModel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Fixtures;
namespace Laravel\Scout\Tests\Fixtures;

class AlgoliaEngineTestModel extends TestModel
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/EmptyTestModel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Fixtures;
namespace Laravel\Scout\Tests\Fixtures;

class EmptyTestModel extends TestModel
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/SearchableTestModel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Fixtures;
namespace Laravel\Scout\Tests\Fixtures;

use Laravel\Scout\Searchable;

Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestModel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Fixtures;
namespace Laravel\Scout\Tests\Fixtures;

use Illuminate\Database\Eloquent\Model;

Expand Down
14 changes: 10 additions & 4 deletions tests/MakeSearchableTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
<?php

namespace Tests;
namespace Laravel\Scout\Tests;

use Mockery;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Laravel\Scout\Jobs\MakeSearchable;
use Illuminate\Database\Eloquent\Collection;

class MakeSearchableTest extends AbstractTestCase
class MakeSearchableTest extends TestCase
{
public function tearDown()
{
m::close();
}

public function test_handle_passes_the_collection_to_engine()
{
$job = new MakeSearchable($collection = Collection::make([
$model = Mockery::mock(),
$model = m::mock(),
]));

$model->shouldReceive('searchableUsing->update')->with($collection);
Expand Down
22 changes: 14 additions & 8 deletions tests/ModelObserverTest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
<?php

namespace Tests;
namespace Laravel\Scout\Tests;

use Mockery;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Laravel\Scout\ModelObserver;

class ModelObserverTest extends AbstractTestCase
class ModelObserverTest extends TestCase
{
public function tearDown()
{
m::close();
}

public function test_saved_handler_makes_model_searchable()
{
$observer = new ModelObserver;
$model = Mockery::mock();
$model = m::mock();
$model->shouldReceive('shouldBeSearchable')->andReturn(true);
$model->shouldReceive('searchable');
$observer->saved($model);
Expand All @@ -19,7 +25,7 @@ public function test_saved_handler_makes_model_searchable()
public function test_saved_handler_doesnt_make_model_searchable_when_disabled()
{
$observer = new ModelObserver;
$model = Mockery::mock();
$model = m::mock();
$observer->disableSyncingFor(get_class($model));
$model->shouldReceive('searchable')->never();
$observer->saved($model);
Expand All @@ -28,7 +34,7 @@ public function test_saved_handler_doesnt_make_model_searchable_when_disabled()
public function test_saved_handler_makes_model_unsearchable_when_disabled_per_model_rule()
{
$observer = new ModelObserver;
$model = Mockery::mock();
$model = m::mock();
$model->shouldReceive('shouldBeSearchable')->andReturn(false);
$model->shouldReceive('searchable')->never();
$model->shouldReceive('unsearchable');
Expand All @@ -38,15 +44,15 @@ public function test_saved_handler_makes_model_unsearchable_when_disabled_per_mo
public function test_deleted_handler_makes_model_unsearchable()
{
$observer = new ModelObserver;
$model = Mockery::mock();
$model = m::mock();
$model->shouldReceive('unsearchable');
$observer->deleted($model);
}

public function test_restored_handler_makes_model_searchable()
{
$observer = new ModelObserver;
$model = Mockery::mock();
$model = m::mock();
$model->shouldReceive('shouldBeSearchable')->andReturn(true);
$model->shouldReceive('searchable');
$observer->restored($model);
Expand Down
Loading

0 comments on commit a05a238

Please sign in to comment.