Skip to content

Commit

Permalink
[10.x] Test Improvements (#880)
Browse files Browse the repository at this point in the history
* [10.x] Supports PHP 8.4

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Apply fixes from StyleCI

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Apply fixes from StyleCI

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Apply fixes from StyleCI

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Apply fixes from StyleCI

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Apply fixes from StyleCI

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Apply fixes from StyleCI

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Apply fixes from StyleCI

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Apply fixes from StyleCI

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Apply fixes from StyleCI

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
crynobone and StyleCIBot authored Nov 13, 2024
1 parent 036efa7 commit 13cb646
Show file tree
Hide file tree
Showing 47 changed files with 1,891 additions and 1,728 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
},
"autoload-dev": {
"psr-4": {
"Laravel\\Scout\\Tests\\": "tests/"
"Laravel\\Scout\\Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/"
}
},
"extra": {
Expand Down
4 changes: 3 additions & 1 deletion testbench.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
providers:
- Workbench\App\Providers\WorkbenchServiceProvider
- Laravel\Scout\ScoutServiceProvider

migrations: true
migrations:
- workbench/database/migrations

workbench:
install: true
86 changes: 14 additions & 72 deletions tests/Feature/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,37 @@
namespace Laravel\Scout\Tests\Feature;

use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Foundation\Auth\User;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Laravel\Scout\EngineManager;
use Laravel\Scout\Engines\MeilisearchEngine;
use Laravel\Scout\Tests\Fixtures\SearchableUserModel;
use Mockery as m;
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
use Orchestra\Testbench\Attributes\WithConfig;
use Orchestra\Testbench\Attributes\WithMigration;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\Factories\UserFactory;
use Orchestra\Testbench\TestCase;
use Workbench\App\Models\SearchableUser;
use Workbench\Database\Factories\SearchableUserFactory;

#[WithConfig('scout.driver', 'database')]
#[WithMigration]
class BuilderTest extends TestCase
{
use LazilyRefreshDatabase, WithFaker, WithLaravelMigrations, WithWorkbench;

protected function defineEnvironment($app)
{
$app->make('config')->set('scout.driver', 'fake');
}
use LazilyRefreshDatabase;
use WithFaker;
use WithWorkbench;

protected function afterRefreshingDatabase()
{
$this->setUpFaker();

UserFactory::new()->count(50)->state(new Sequence(function () {
SearchableUserFactory::new()->count(50)->state(new Sequence(function () {
return ['name' => 'Laravel '.$this->faker()->name()];
}))->create();

UserFactory::new()->times(50)->create();
SearchableUserFactory::new()->times(50)->create();
}

public function test_it_can_paginate_without_custom_query_callback()
{
$this->prepareScoutSearchMockUsing('Laravel');

$paginator = SearchableUserModel::search('Laravel')->paginate();
$paginator = SearchableUser::search('Laravel')->paginate();

$this->assertSame(50, $paginator->total());
$this->assertSame(4, $paginator->lastPage());
Expand All @@ -48,9 +42,7 @@ public function test_it_can_paginate_without_custom_query_callback()

public function test_it_can_paginate_with_custom_query_callback()
{
$this->prepareScoutSearchMockUsing('Laravel');

$paginator = SearchableUserModel::search('Laravel')->query(function ($builder) {
$paginator = SearchableUser::search('Laravel')->query(function ($builder) {
return $builder->where('id', '<', 11);
})->paginate();

Expand All @@ -61,60 +53,10 @@ public function test_it_can_paginate_with_custom_query_callback()

public function test_it_can_paginate_raw_without_custom_query_callback()
{
$this->prepareScoutSearchMockUsing('Laravel');

$paginator = SearchableUserModel::search('Laravel')->paginateRaw();
$paginator = SearchableUser::search('Laravel')->paginateRaw();

$this->assertSame(50, $paginator->total());
$this->assertSame(4, $paginator->lastPage());
$this->assertSame(15, $paginator->perPage());
}

public function test_it_can_paginate_raw_with_custom_query_callback()
{
$this->prepareScoutSearchMockUsing('Laravel');

$paginator = SearchableUserModel::search('Laravel')->query(function ($builder) {
return $builder->where('id', '<', 11);
})->paginateRaw();

$this->assertSame(10, $paginator->total());
$this->assertSame(1, $paginator->lastPage());
$this->assertSame(15, $paginator->perPage());
}

protected function prepareScoutSearchMockUsing($searchQuery)
{
$engine = m::mock('Meilisearch\Client');
$indexes = m::mock('Meilisearch\Endpoints\Indexes');

$manager = $this->app->make(EngineManager::class);
$manager->extend('fake', function () use ($engine) {
return new MeilisearchEngine($engine);
});

$query = User::where('name', 'like', $searchQuery.'%');

$hitsPerPage = 15;
$page = 1;
$totalPages = intval($query->count() / $hitsPerPage);

$engine->shouldReceive('index')->with('users')->andReturn($indexes);
$indexes->shouldReceive('rawSearch')
->with($searchQuery, ['hitsPerPage' => $hitsPerPage, 'page' => $page])
->andReturn([
'query' => $searchQuery,
'hits' => $query->get()->transform(function ($result) {
return [
'id' => $result->getKey(),
'name' => $result->name,
];
})->toArray(),
'hitsPerPage' => $hitsPerPage,
'page' => $page,
'totalHits' => $query->count(),
'totalPages' => $totalPages > 0 ? $totalPages : 0,
'processingTimeMs' => 1,
]);
}
}
104 changes: 68 additions & 36 deletions tests/Feature/CollectionEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Laravel\Scout\Tests\Fixtures\SearchableModelWithUnloadedValue;
use Laravel\Scout\Tests\Fixtures\SearchableUserModel;
use Laravel\Scout\Tests\Fixtures\SearchableUserModelWithCustomCreatedAt;
use Laravel\Scout\Tests\Fixtures\SearchableUserModelWithCustomSearchableData;
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
use Illuminate\Support\Collection;
use Orchestra\Testbench\Attributes\WithConfig;
use Orchestra\Testbench\Attributes\WithMigration;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\Factories\UserFactory;
use Orchestra\Testbench\TestCase;
use Workbench\App\Models\SearchableUser;

#[WithConfig('scout.driver', 'collection')]
#[WithMigration]
class CollectionEngineTest extends TestCase
{
use LazilyRefreshDatabase, WithLaravelMigrations, WithWorkbench;

protected function defineEnvironment($app)
{
$app->make('config')->set('scout.driver', 'collection');
}
use LazilyRefreshDatabase;
use WithWorkbench;

protected function afterRefreshingDatabase()
{
Expand All @@ -39,113 +36,113 @@ protected function afterRefreshingDatabase()

public function test_it_can_retrieve_results_with_empty_search()
{
$models = SearchableUserModel::search()->get();
$models = SearchableUser::search()->get();

$this->assertCount(2, $models);
}

public function test_it_can_retrieve_results()
{
$models = SearchableUserModel::search('Taylor')->where('email', '[email protected]')->get();
$models = SearchableUser::search('Taylor')->where('email', '[email protected]')->get();
$this->assertCount(1, $models);
$this->assertEquals(1, $models[0]->id);

$models = SearchableUserModel::search('Taylor')->query(function ($query) {
$models = SearchableUser::search('Taylor')->query(function ($query) {
$query->where('email', 'like', '[email protected]');
})->get();

$this->assertCount(1, $models);
$this->assertEquals(1, $models[0]->id);

$models = SearchableUserModel::search('Abigail')->where('email', '[email protected]')->get();
$models = SearchableUser::search('Abigail')->where('email', '[email protected]')->get();
$this->assertCount(1, $models);
$this->assertEquals(2, $models[0]->id);

$models = SearchableUserModel::search('Taylor')->where('email', '[email protected]')->get();
$models = SearchableUser::search('Taylor')->where('email', '[email protected]')->get();
$this->assertCount(0, $models);

$models = SearchableUserModel::search('Taylor')->where('email', '[email protected]')->get();
$models = SearchableUser::search('Taylor')->where('email', '[email protected]')->get();
$this->assertCount(1, $models);

$models = SearchableUserModel::search('otwell')->get();
$models = SearchableUser::search('otwell')->get();
$this->assertCount(2, $models);

$models = SearchableUserModel::search('laravel')->get();
$models = SearchableUser::search('laravel')->get();
$this->assertCount(2, $models);

$models = SearchableUserModel::search('foo')->get();
$models = SearchableUser::search('foo')->get();
$this->assertCount(0, $models);

$models = SearchableUserModel::search('Abigail')->where('email', '[email protected]')->get();
$models = SearchableUser::search('Abigail')->where('email', '[email protected]')->get();
$this->assertCount(0, $models);
}

public function test_it_can_retrieve_results_matching_to_custom_searchable_data()
{
$models = SearchableUserModelWithCustomSearchableData::search('rolyaT')->get();
$models = SearchableUserWithCustomSearchableData::search('rolyaT')->get();
$this->assertCount(1, $models);
}

public function test_it_can_paginate_results()
{
$models = SearchableUserModel::search('Taylor')->where('email', '[email protected]')->paginate();
$models = SearchableUser::search('Taylor')->where('email', '[email protected]')->paginate();
$this->assertCount(1, $models);

$models = SearchableUserModel::search('Taylor')->where('email', '[email protected]')->paginate();
$models = SearchableUser::search('Taylor')->where('email', '[email protected]')->paginate();
$this->assertCount(0, $models);

$models = SearchableUserModel::search('Taylor')->where('email', '[email protected]')->paginate();
$models = SearchableUser::search('Taylor')->where('email', '[email protected]')->paginate();
$this->assertCount(1, $models);

$models = SearchableUserModel::search('laravel')->paginate();
$models = SearchableUser::search('laravel')->paginate();
$this->assertCount(2, $models);

$dummyQuery = function ($query) {
$query->where('name', '!=', 'Dummy');
};
$models = SearchableUserModel::search('laravel')->query($dummyQuery)->orderBy('name')->paginate(1, 'page', 1);
$models = SearchableUser::search('laravel')->query($dummyQuery)->orderBy('name')->paginate(1, 'page', 1);
$this->assertCount(1, $models);
$this->assertEquals('Abigail Otwell', $models[0]->name);

$models = SearchableUserModel::search('laravel')->query($dummyQuery)->orderBy('name')->paginate(1, 'page', 2);
$models = SearchableUser::search('laravel')->query($dummyQuery)->orderBy('name')->paginate(1, 'page', 2);
$this->assertCount(1, $models);
$this->assertEquals('Taylor Otwell', $models[0]->name);
}

public function test_limit_is_applied()
{
$models = SearchableUserModel::search('laravel')->get();
$models = SearchableUser::search('laravel')->get();
$this->assertCount(2, $models);

$models = SearchableUserModel::search('laravel')->take(1)->get();
$models = SearchableUser::search('laravel')->take(1)->get();
$this->assertCount(1, $models);
}

public function test_it_can_order_results()
{
$models = SearchableUserModel::search('laravel')->orderBy('name', 'asc')->paginate(1, 'page', 1);
$models = SearchableUser::search('laravel')->orderBy('name', 'asc')->paginate(1, 'page', 1);
$this->assertCount(1, $models);
$this->assertEquals('Abigail Otwell', $models[0]->name);

$models = SearchableUserModel::search('laravel')->orderBy('name', 'desc')->paginate(1, 'page', 1);
$models = SearchableUser::search('laravel')->orderBy('name', 'desc')->paginate(1, 'page', 1);
$this->assertCount(1, $models);
$this->assertEquals('Taylor Otwell', $models[0]->name);
}

public function test_it_can_order_by_latest_and_oldest()
{
$models = SearchableUserModel::search('laravel')->latest()->paginate(1, 'page', 1);
$models = SearchableUser::search('laravel')->latest()->paginate(1, 'page', 1);
$this->assertCount(1, $models);
$this->assertEquals('Abigail Otwell', $models[0]->name);

$models = SearchableUserModel::search('laravel')->oldest()->paginate(1, 'page', 1);
$models = SearchableUser::search('laravel')->oldest()->paginate(1, 'page', 1);
$this->assertCount(1, $models);
$this->assertEquals('Taylor Otwell', $models[0]->name);
}

public function test_it_can_order_by_custom_model_created_at_timestamp()
{
$query = SearchableUserModelWithCustomCreatedAt::search()->latest();
$query = SearchableUserWithCustomCreatedAt::search()->latest();

$this->assertCount(1, $query->orders);
$this->assertEquals('created', $query->orders[0]['column']);
Expand All @@ -155,8 +152,43 @@ public function test_it_calls_make_searchable_using_before_searching()
{
Model::preventAccessingMissingAttributes(true);

$models = SearchableModelWithUnloadedValue::search('loaded')->get();
$models = SearchableUserWithUnloadedValue::search('loaded')->get();

$this->assertCount(2, $models);
}
}

class SearchableUserWithCustomCreatedAt extends SearchableUser
{
public const CREATED_AT = 'created';
}

class SearchableUserWithCustomSearchableData extends SearchableUser
{
/** {@inheritDoc} */
public function toSearchableArray(): array
{
return [
'reversed_name' => strrev($this->name),
];
}
}

class SearchableUserWithUnloadedValue extends SearchableUser
{
/** {@inheritDoc} */
public function toSearchableArray()
{
return [
'value' => $this->unloadedValue,
];
}

/** {@inheritDoc} */
public function makeSearchableUsing(Collection $models)
{
return $models->each(
fn ($model) => $model->unloadedValue = 'loaded',
);
}
}
Loading

0 comments on commit 13cb646

Please sign in to comment.