Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Test Improvements #880

Merged
merged 41 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
74b4e08
[10.x] Supports PHP 8.4
crynobone Nov 12, 2024
3415d99
wip
crynobone Nov 12, 2024
f612588
wip
crynobone Nov 12, 2024
a0a149f
Apply fixes from StyleCI
StyleCIBot Nov 12, 2024
e0bb35a
wip
crynobone Nov 12, 2024
e79ea24
wip
crynobone Nov 12, 2024
34dfa01
Merge commit 'a0a149fc1c54f02f493d8f9fcbc1d3952e6ca039' into remove-r…
crynobone Nov 12, 2024
1604df0
Apply fixes from StyleCI
StyleCIBot Nov 12, 2024
29d37e7
wip
crynobone Nov 12, 2024
91caed8
Merge remote-tracking branch 'upstream/remove-risky-tests' into remov…
crynobone Nov 12, 2024
dcc115d
Apply fixes from StyleCI
StyleCIBot Nov 12, 2024
d889235
wip
crynobone Nov 12, 2024
b7e2629
wip
crynobone Nov 12, 2024
f0dc53d
Merge remote-tracking branch 'upstream/remove-risky-tests' into remov…
crynobone Nov 12, 2024
aa7cc8d
Apply fixes from StyleCI
StyleCIBot Nov 12, 2024
a877eb2
wip
crynobone Nov 12, 2024
2810c4e
Merge remote-tracking branch 'upstream/remove-risky-tests' into remov…
crynobone Nov 12, 2024
3e66edb
wip
crynobone Nov 12, 2024
0995862
Apply fixes from StyleCI
StyleCIBot Nov 12, 2024
72fce00
wip
crynobone Nov 12, 2024
f0814a1
wip
crynobone Nov 12, 2024
eee865c
Merge remote-tracking branch 'upstream/remove-risky-tests' into remov…
crynobone Nov 12, 2024
9eda2aa
Apply fixes from StyleCI
StyleCIBot Nov 12, 2024
d17351b
wip
crynobone Nov 12, 2024
9e0c8bf
wip
crynobone Nov 12, 2024
0a77b2a
wip
crynobone Nov 12, 2024
396b398
Apply fixes from StyleCI
StyleCIBot Nov 12, 2024
6abff7f
wip
crynobone Nov 12, 2024
3afee1a
wip
crynobone Nov 12, 2024
ae07258
wip
crynobone Nov 12, 2024
e1fc15d
Apply fixes from StyleCI
StyleCIBot Nov 12, 2024
d1a2776
wip
crynobone Nov 12, 2024
e28d2c2
Merge remote-tracking branch 'upstream/remove-risky-tests' into remov…
crynobone Nov 12, 2024
568a895
wip
crynobone Nov 12, 2024
942d235
wip
crynobone Nov 12, 2024
4c049f1
wip
crynobone Nov 12, 2024
3dba26b
wip
crynobone Nov 12, 2024
b36a649
Apply fixes from StyleCI
StyleCIBot Nov 12, 2024
68151b4
wip
crynobone Nov 12, 2024
f1d0009
Merge remote-tracking branch 'upstream/remove-risky-tests' into remov…
crynobone Nov 12, 2024
fec90a3
Merge branch '10.x' into remove-risky-tests
crynobone Nov 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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