Skip to content

Commit

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

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

* Update AlgoliaSearchableTest.php

* 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

* wip

* wip

* wip

* wip

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

* Test Improvements.

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

* Update AlgoliaSearchableTest.php

* 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

* wip

* wip

* wip

* 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]>

* wip

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

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Dec 5, 2023
1 parent 13fd42b commit 77d602e
Show file tree
Hide file tree
Showing 11 changed files with 755 additions and 1 deletion.
42 changes: 42 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,45 @@ jobs:
- name: Execute tests
run: vendor/bin/phpunit --verbose

tests-using-meilisearch:
runs-on: ubuntu-22.04

services:
meilisearch:
image: getmeili/meilisearch:latest
ports:
- 7700:7700
env:
MEILI_MASTER_KEY: masterKey
MEILI_NO_ANALYTICS: true

strategy:
fail-fast: true
matrix:
php: ['8.0', 8.1, 8.2, 8.3]

name: PHP ${{ matrix.php }} using Meilisearch

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip
ini-values: error_reporting=E_ALL
tools: composer:v2
coverage: none

- name: Install dependencies
run: |
composer update --prefer-dist --no-interaction --no-progress
- name: Execute tests
run: vendor/bin/phpunit --verbose --group external-network,meilisearch
env:
MEILISEARCH_HOST: "http://localhost:7700"
MEILISEARCH_KEY: 'masterKey'
14 changes: 14 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Integration">
<directory suffix="Test.php">./tests/Integration</directory>
</testsuite>
</testsuites>

<groups>
<exclude>
<group>external-network</group>
</exclude>
</groups>

<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
<env name="DB_CONNECTION" value="testing"/>
<!--
<env name="ALGOLIA_APP_ID" value="(null)"/>
<env name="ALGOLIA_SECRET" value="(null)"/>
<env name="MEILISEARCH_HOST" value="(null)"/>
-->
</php>
</phpunit>
3 changes: 3 additions & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ providers:
- Laravel\Scout\ScoutServiceProvider

migrations: true

workbench:
install: true
5 changes: 4 additions & 1 deletion tests/Feature/MeilisearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class MeilisearchEngineTest extends TestCase

protected function defineEnvironment($app)
{
$app->make('config')->set('scout.driver', 'meilisearch');
$app->make('config')->set([
'scout.driver' => 'meilisearch',
'scout.meilisearch.host' => 'http://localhost:7700',
]);
}

public function test_the_meilisearch_client_can_be_initialized()
Expand Down
24 changes: 24 additions & 0 deletions tests/Fixtures/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Laravel\Scout\Tests\Fixtures;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Scout\Searchable;

class User extends Authenticatable
{
use Searchable;

/**
* Get the indexable data array for the model.
*
* @return array<string, mixed>
*/
public function toSearchableArray(): array
{
return [
'id' => (int) $this->id,
'name' => $this->name,
];
}
}
171 changes: 171 additions & 0 deletions tests/Integration/AlgoliaSearchableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php

namespace Laravel\Scout\Tests\Integration;

use Illuminate\Support\Env;
use Laravel\Scout\Tests\Fixtures\User;

/**
* @group algolia
* @group external-network
*/
class AlgoliaSearchableTest extends TestCase
{
use SearchableTests;

/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function defineEnvironment($app)
{
if (is_null(Env::get('ALGOLIA_APP_ID'))) {
$this->markTestSkipped();
}

$this->defineScoutEnvironment($app);
}

/**
* Define database migrations.
*
* @return void
*/
protected function defineDatabaseMigrations()
{
$this->defineScoutDatabaseMigrations();
}

/**
* Perform any work that should take place once the database has finished refreshing.
*
* @return void
*/
protected function afterRefreshingDatabase()
{
$this->importScoutIndexFrom(User::class);
}

public function test_it_can_use_basic_search()
{
$results = $this->itCanUseBasicSearch();

$this->assertSame([
11 => 'Larry Casper',
1 => 'Laravel Framework',
44 => 'Amos Larson Sr.',
43 => 'Dana Larson Sr.',
42 => 'Dax Larkin',
41 => 'Gudrun Larkin',
40 => 'Otis Larson MD',
39 => 'Linkwood Larkin',
20 => 'Prof. Larry Prosacco DVM',
12 => 'Reta Larkin',
], $results->pluck('name', 'id')->all());
}

public function test_it_can_use_basic_search_with_query_callback()
{
$results = $this->itCanUseBasicSearchWithQueryCallback();

$this->assertSame([
1 => 'Laravel Framework',
44 => 'Amos Larson Sr.',
43 => 'Dana Larson Sr.',
42 => 'Dax Larkin',
41 => 'Gudrun Larkin',
40 => 'Otis Larson MD',
12 => 'Reta Larkin',
], $results->pluck('name', 'id')->all());
}

public function test_it_can_use_basic_search_to_fetch_keys()
{
$results = $this->itCanUseBasicSearchToFetchKeys();

$this->assertSame([
'11',
'1',
'44',
'43',
'42',
'41',
'40',
'39',
'20',
'12',
], $results->all());
}

public function test_it_can_use_basic_search_with_query_callback_to_fetch_keys()
{
$results = $this->itCanUseBasicSearchWithQueryCallbackToFetchKeys();

$this->assertSame([
'11',
'1',
'44',
'43',
'42',
'41',
'40',
'39',
'20',
'12',
], $results->all());
}

public function test_it_return_same_keys_with_query_callback()
{
$this->assertSame(
$this->itCanUseBasicSearchToFetchKeys()->all(),
$this->itCanUseBasicSearchWithQueryCallbackToFetchKeys()->all()
);
}

public function test_it_can_use_paginated_search()
{
[$page1, $page2] = $this->itCanUsePaginatedSearch();

$this->assertSame([
11 => 'Larry Casper',
1 => 'Laravel Framework',
44 => 'Amos Larson Sr.',
43 => 'Dana Larson Sr.',
42 => 'Dax Larkin',
], $page1->pluck('name', 'id')->all());

$this->assertSame([
41 => 'Gudrun Larkin',
40 => 'Otis Larson MD',
39 => 'Linkwood Larkin',
20 => 'Prof. Larry Prosacco DVM',
12 => 'Reta Larkin',
], $page2->pluck('name', 'id')->all());
}

public function test_it_can_use_paginated_search_with_query_callback()
{
[$page1, $page2] = $this->itCanUsePaginatedSearchWithQueryCallback();

$this->assertSame([
1 => 'Laravel Framework',
44 => 'Amos Larson Sr.',
43 => 'Dana Larson Sr.',
42 => 'Dax Larkin',
], $page1->pluck('name', 'id')->all());

$this->assertSame([
41 => 'Gudrun Larkin',
40 => 'Otis Larson MD',
12 => 'Reta Larkin',
], $page2->pluck('name', 'id')->all());
}

protected static function scoutDriver(): string
{
return 'algolia';
}
}
39 changes: 39 additions & 0 deletions tests/Integration/CollectionSearchableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Laravel\Scout\Tests\Integration;

class CollectionSearchableTest extends DatabaseSearchableTest
{
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function defineEnvironment($app)
{
$this->defineScoutEnvironment($app);
}

public function test_it_can_use_basic_search_with_query_callback_to_fetch_keys()
{
$results = $this->itCanUseBasicSearchWithQueryCallbackToFetchKeys();

$this->assertNotSame([
44,
43,
42,
41,
40,
12,
1,
], $results->all());

$this->assertSame($this->itCanUseBasicSearchToFetchKeys()->all(), $results->all());
}

protected static function scoutDriver(): string
{
return 'collection';
}
}
Loading

0 comments on commit 77d602e

Please sign in to comment.