Skip to content

Commit bfb43f8

Browse files
authored
Merge pull request #51 from typesense/tests
Add tests
2 parents f8105a5 + d4197e6 commit bfb43f8

File tree

11 files changed

+537
-4
lines changed

11 files changed

+537
-4
lines changed

.github/workflows/pr.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Pull Request
2+
3+
on:
4+
pull_request:
5+
branches: [master]
6+
7+
jobs:
8+
tests:
9+
runs-on: ubuntu-latest
10+
timeout-minutes: 15
11+
strategy:
12+
matrix:
13+
node-version: [16.x]
14+
typesense-version: [0.24.0]
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v2
18+
with:
19+
submodules: 'recursive'
20+
21+
- name: Start Typesense
22+
uses: jirevwe/[email protected]
23+
with:
24+
typesense-version: ${{ matrix.typesense-version }}
25+
typesense-port: 8108
26+
typesense-api-key: xyz
27+
28+
- name: Composer Validate
29+
run: composer validate
30+
31+
- name: Cache Composer Packages
32+
id: composer-cache
33+
uses: actions/cache@v2
34+
with:
35+
path: vendor
36+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
37+
restore-keys: |
38+
${{ runner.os }}-php-
39+
40+
- name: Cache dependencies
41+
uses: actions/cache@v2
42+
with:
43+
path: |
44+
~/.npm
45+
key: ${{ runner.os }}-node-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
46+
47+
- name: Install Dependencies
48+
run: |
49+
composer update --prefer-dist --no-interaction --no-progress
50+
51+
- name: Run Tests
52+
run: vendor/bin/phpunit tests

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.tmp
33
/composer.lock
44
vendor
5+
.phpunit.result.cache

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ $searchRequests = [
201201
]
202202
];
203203

204-
Todo::searchMulti($searchRequests)->paginateRaw();
204+
Todo::search('')->searchMulti($searchRequests)->paginateRaw();
205205
```
206206

207207
### Generate Scoped Search Key

composer.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"minimum-stability": "stable",
2626
"autoload": {
2727
"psr-4": {
28-
"Typesense\\LaravelTypesense\\": "src/"
28+
"Typesense\\LaravelTypesense\\": "src/",
29+
"Typesense\\LaravelTypesense\\Tests\\": "tests/"
2930
}
3031
},
3132
"extra": {
@@ -47,18 +48,24 @@
4748
"illuminate/pagination": "^7.0|^8.0|^9.0|^10.0",
4849
"illuminate/queue": "^7.0|^8.0|^9.0|^10.0",
4950
"illuminate/support": "^7.0|^8.0|^9.0|^10.0",
50-
"typesense/typesense-php": "^4.0"
51+
"typesense/typesense-php": "^4.0",
52+
"symfony/http-client": "^5.4"
5153
},
5254
"config": {
5355
"platform": {
5456
"php": "8.0"
57+
},
58+
"allow-plugins": {
59+
"pestphp/pest-plugin": true,
60+
"php-http/discovery": true
5561
}
5662
},
5763
"suggest": {
5864
"typesense/typesense-php": "Required to use the Typesense php client."
5965
},
6066
"require-dev": {
6167
"phpunit/phpunit": "^8.0|^9.0",
62-
"mockery/mockery": "^1.3"
68+
"mockery/mockery": "^1.3",
69+
"orchestra/testbench": "^6.17|^7.0|^8.0"
6370
}
6471
}

config/scout.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Default Search Engine
8+
|--------------------------------------------------------------------------
9+
|
10+
| This option controls the default search connection that gets used while
11+
| using Laravel Scout. This connection is used when syncing all models
12+
| to the search service. You should adjust this based on your needs.
13+
|
14+
| Supported: "algolia", "meilisearch", "database", "collection", "null"
15+
|
16+
*/
17+
18+
'driver' => env('SCOUT_DRIVER', 'typesense'),
19+
20+
/*
21+
|--------------------------------------------------------------------------
22+
| Index Prefix
23+
|--------------------------------------------------------------------------
24+
|
25+
| Here you may specify a prefix that will be applied to all search index
26+
| names used by Scout. This prefix may be useful if you have multiple
27+
| "tenants" or applications sharing the same search infrastructure.
28+
|
29+
*/
30+
31+
'prefix' => env('SCOUT_PREFIX', ''),
32+
33+
/*
34+
|--------------------------------------------------------------------------
35+
| Queue Data Syncing
36+
|--------------------------------------------------------------------------
37+
|
38+
| This option allows you to control if the operations that sync your data
39+
| with your search engines are queued. When this is set to "true" then
40+
| all automatic data syncing will get queued for better performance.
41+
|
42+
*/
43+
44+
'queue' => env('SCOUT_QUEUE', false),
45+
46+
/*
47+
|--------------------------------------------------------------------------
48+
| Database Transactions
49+
|--------------------------------------------------------------------------
50+
|
51+
| This configuration option determines if your data will only be synced
52+
| with your search indexes after every open database transaction has
53+
| been committed, thus preventing any discarded data from syncing.
54+
|
55+
*/
56+
57+
'after_commit' => false,
58+
59+
/*
60+
|--------------------------------------------------------------------------
61+
| Chunk Sizes
62+
|--------------------------------------------------------------------------
63+
|
64+
| These options allow you to control the maximum chunk size when you are
65+
| mass importing data into the search engine. This allows you to fine
66+
| tune each of these chunk sizes based on the power of the servers.
67+
|
68+
*/
69+
70+
'chunk' => [
71+
'searchable' => 500,
72+
'unsearchable' => 500,
73+
],
74+
75+
/*
76+
|--------------------------------------------------------------------------
77+
| Soft Deletes
78+
|--------------------------------------------------------------------------
79+
|
80+
| This option allows to control whether to keep soft deleted records in
81+
| the search indexes. Maintaining soft deleted records can be useful
82+
| if your application still needs to search for the records later.
83+
|
84+
*/
85+
86+
'soft_delete' => false,
87+
88+
/*
89+
|--------------------------------------------------------------------------
90+
| Identify User
91+
|--------------------------------------------------------------------------
92+
|
93+
| This option allows you to control whether to notify the search engine
94+
| of the user performing the search. This is sometimes useful if the
95+
| engine supports any analytics based on this application's users.
96+
|
97+
| Supported engines: "algolia"
98+
|
99+
*/
100+
101+
'identify' => env('SCOUT_IDENTIFY', false),
102+
103+
/*
104+
|--------------------------------------------------------------------------
105+
| Algolia Configuration
106+
|--------------------------------------------------------------------------
107+
|
108+
| Here you may configure your Algolia settings. Algolia is a cloud hosted
109+
| search engine which works great with Scout out of the box. Just plug
110+
| in your application ID and admin API key to get started searching.
111+
|
112+
*/
113+
114+
'algolia' => [
115+
'id' => env('ALGOLIA_APP_ID', ''),
116+
'secret' => env('ALGOLIA_SECRET', ''),
117+
],
118+
119+
/*
120+
|--------------------------------------------------------------------------
121+
| MeiliSearch Configuration
122+
|--------------------------------------------------------------------------
123+
|
124+
| Here you may configure your MeiliSearch settings. MeiliSearch is an open
125+
| source search engine with minimal configuration. Below, you can state
126+
| the host and key information for your own MeiliSearch installation.
127+
|
128+
| See: https://docs.meilisearch.com/guides/advanced_guides/configuration.html
129+
|
130+
*/
131+
132+
'meilisearch' => [
133+
'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
134+
'key' => env('MEILISEARCH_KEY', null),
135+
'index-settings' => [
136+
// 'users' => [
137+
// 'filterableAttributes'=> ['id', 'name', 'email'],
138+
// ],
139+
],
140+
],
141+
142+
'typesense' => [
143+
'api_key' => 'xyz',
144+
'nodes' => [
145+
[
146+
'host' => 'localhost',
147+
'port' => '8108',
148+
'path' => '',
149+
'protocol' => 'http',
150+
],
151+
],
152+
'nearest_node' => [
153+
'host' => 'localhost',
154+
'port' => '8108',
155+
'path' => '',
156+
'protocol' => 'http',
157+
],
158+
'connection_timeout_seconds' => 2,
159+
'healthcheck_interval_seconds' => 30,
160+
'num_retries' => 3,
161+
'retry_interval_seconds' => 1,
162+
],
163+
];

phpunit.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
>
7+
<testsuites>
8+
<testsuite name="Feature">
9+
<directory suffix="Test.php">./tests/Feature</directory>
10+
</testsuite>
11+
</testsuites>
12+
<coverage processUncoveredFiles="true">
13+
<include>
14+
<directory suffix=".php">./app</directory>
15+
<directory suffix=".php">./src</directory>
16+
</include>
17+
</coverage>
18+
<php>
19+
<env name="APP_ENV" value="testing"/>
20+
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
21+
<env name="DB_CONNECTION" value="testing"/>
22+
</php>
23+
</phpunit>

tests/Feature/MultiSearchTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Typesense\LaravelTypesense\Tests\Feature;
4+
5+
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Typesense\LaravelTypesense\Tests\Fixtures\SearchableUserModel;
7+
use Typesense\LaravelTypesense\Tests\TestCase;
8+
9+
class MultiSearchTest extends TestCase
10+
{
11+
use RefreshDatabase;
12+
13+
protected function defineDatabaseMigrations()
14+
{
15+
$this->setUpFaker();
16+
$this->loadLaravelMigrations();
17+
18+
SearchableUserModel::create([
19+
'name' => 'Laravel Typsense',
20+
'email' => '[email protected]',
21+
'password' => bcrypt('password'),
22+
]);
23+
SearchableUserModel::create([
24+
'name' => 'Laravel Typsense',
25+
'email' => '[email protected]',
26+
'password' => bcrypt('password'),
27+
]);
28+
SearchableUserModel::create([
29+
'name' => 'Laravel Typsense',
30+
'email' => '[email protected]',
31+
'password' => bcrypt('password'),
32+
]);
33+
}
34+
35+
public function testSearchByEmail()
36+
{
37+
$searchRequests = [
38+
[
39+
'collection' => 'users',
40+
'q' => 'Laravel Typsense'
41+
],
42+
[
43+
'collection' => 'users',
44+
45+
]
46+
];
47+
48+
$response = SearchableUserModel::search('')->searchMulti($searchRequests)->paginateRaw();
49+
50+
$this->assertCount(2, $response->items()['results']);
51+
$this->assertEquals(3, $response->items()['results'][0]['found']);
52+
$this->assertEquals("[email protected]", $response->items()['results'][0]['hits'][0]['document']['email']);
53+
54+
}
55+
56+
public function testSearchByName()
57+
{
58+
$searchRequests = [
59+
[
60+
'collection' => 'users',
61+
'q' => 'Laravel Typsense'
62+
],
63+
[
64+
'collection' => 'users',
65+
66+
]
67+
];
68+
69+
$response = SearchableUserModel::search('')->searchMulti($searchRequests)->paginateRaw();
70+
71+
$this->assertCount(2, $response->items()['results']);
72+
$this->assertEquals(1, $response->items()['results'][1]['found']);
73+
$this->assertEquals("[email protected]", $response->items()['results'][1]['hits'][0]['document']['email']);
74+
}
75+
76+
public function testSearchByWrongQueryParams()
77+
{
78+
$searchRequests = [
79+
[
80+
'collection' => 'users',
81+
'q' => 'Wrong Params'
82+
],
83+
[
84+
'collection' => 'users',
85+
86+
]
87+
];
88+
89+
$response = SearchableUserModel::search('')->searchMulti($searchRequests)->paginateRaw();
90+
$this->assertEquals(0, $response->items()['results'][0]['found']);
91+
$this->assertEquals(0, $response->items()['results'][1]['found']);
92+
}
93+
}

0 commit comments

Comments
 (0)