Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Oct 4, 2024
1 parent eb321ae commit d4a67da
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"Lunar\\Stripe\\StripePaymentsServiceProvider",
"Lunar\\Paypal\\PaypalServiceProvider",
"Lunar\\Meilisearch\\MeilisearchServiceProvider",
"Lunar\\Search\\SearchProvider",
"SearchServiceProvider",
"Lunar\\Admin\\LunarPanelProvider",
"Lunar\\Opayo\\OpayoServiceProvider",
"Lunar\\Shipping\\ShippingServiceProvider",
Expand Down
2 changes: 1 addition & 1 deletion packages/search/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"laravel": {
"providers": [
"Lunar\\Search\\SearchProvider"
"SearchServiceProvider"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Lunar\Search\Contracts\InstantSearchContract;
use Lunar\Search\Contracts\SearchManagerContract;

class SearchProvider extends ServiceProvider
class SearchServiceProvider extends ServiceProvider
{
public function register()
{
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</testsuite>
<testsuite name="search">
<directory suffix="Test.php">tests/search/Unit</directory>
<directory suffix="Test.php">tests/search/Feature</directory>
</testsuite>
<testsuite name="shipping">
<directory suffix="Test.php">tests/shipping/Feature</directory>
Expand Down
88 changes: 88 additions & 0 deletions tests/search/Feature/Engines/TypesenseEngineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

use Typesense\ApiCall;

uses(\Lunar\Tests\Search\TestCase::class)->group('search');
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);

beforeEach(function () {
Mockery::mock(ApiCall::class);
});

test('can get search results', function () {
$engine = \Pest\Laravel\partialMock(Lunar\Search\Engines\TypesenseEngine::class, function (\Mockery\MockInterface $mock) {
$mock->shouldAllowMockingProtectedMethods()
->shouldReceive('getRawResults')
->andReturn(
new \Illuminate\Pagination\LengthAwarePaginator(
items: [
'hits' => [],
'facet_counts' => [],
'request_params' => [
'q' => 'Hello',
],
],
total: 100,
perPage: 50,
currentPage: 1
)
);
$mock->shouldReceive('query')->andReturnSelf();
});

$results = $engine->query('')->get();

expect($results)
->toBeInstanceOf(\Lunar\Search\Data\SearchResults::class)
->and($results->count)
->toBe(100)
->and($results->perPage)
->toBe(50)
->and($results->totalPages)
->toBe(2)
->and($results->links)
->toBeArray()
->and($results->links)
->toHaveCount(4);
});

test('can map search results to spatie data objects', function () {
$engine = \Pest\Laravel\partialMock(Lunar\Search\Engines\TypesenseEngine::class, function (\Mockery\MockInterface $mock) {
$mock->shouldAllowMockingProtectedMethods()
->shouldReceive('getRawResults')
->andReturn(
new \Illuminate\Pagination\LengthAwarePaginator(
items: [
'hits' => [
[
'highlights' => [

],
'document' => [
'id' => 123,
'name' => 'Foobar',
]
]
],
'facet_counts' => [],
'request_params' => [
'q' => '',
],
],
total: 0,
perPage: 50,
currentPage: 1
)
);
$mock->shouldReceive('query')->andReturnSelf();
});

$results = $engine->query('')->get();

expect($results)
->toBeInstanceOf(\Lunar\Search\Data\SearchResults::class)
->and($results->hits[0])
->toBeInstanceOf(\Lunar\Search\Data\SearchHit::class)
->and($results->hits[0]->document['name'])
->toBe('Foobar');
});
6 changes: 6 additions & 0 deletions tests/search/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use Cartalyst\Converter\Laravel\ConverterServiceProvider;
use Kalnoy\Nestedset\NestedSetServiceProvider;
use Laravel\Scout\ScoutServiceProvider;
use Lunar\LunarServiceProvider;
use Lunar\Search\SearchServiceProvider;
use Spatie\Activitylog\ActivitylogServiceProvider;
use Spatie\LaravelBlink\BlinkServiceProvider;
use Spatie\LaravelData\LaravelDataServiceProvider;
use Spatie\MediaLibrary\MediaLibraryServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
Expand All @@ -27,6 +30,9 @@ protected function getPackageProviders($app)
ConverterServiceProvider::class,
NestedSetServiceProvider::class,
BlinkServiceProvider::class,
ScoutServiceProvider::class,
LaravelDataServiceProvider::class,
SearchServiceProvider::class
];
}

Expand Down
19 changes: 19 additions & 0 deletions tests/search/Unit/Engines/AbstractEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,22 @@ public function get(): \Illuminate\Support\Collection

expect($engine->getFilters()['color'])->toEqual('Blue');
});

test('can set and get facets', function () {
$engine = new class extends AbstractEngine
{
public function get(): \Illuminate\Support\Collection
{
return collect();
}
};

$facets = [
'color' => ['Red'],
'size' => 'Small',
];

$engine->setFacets($facets);

expect($engine->getFacets())->toEqual($facets);
});

0 comments on commit d4a67da

Please sign in to comment.