From d4a67dac6fe6cf14b86d10f0ad5da82d867af4c0 Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Fri, 4 Oct 2024 10:55:46 +0100 Subject: [PATCH] test --- composer.json | 2 +- packages/search/composer.json | 2 +- ...Provider.php => SearchServiceProvider.php} | 2 +- phpunit.xml | 1 + .../Feature/Engines/TypesenseEngineTest.php | 88 +++++++++++++++++++ tests/search/TestCase.php | 6 ++ .../Unit/Engines/AbstractEngineTest.php | 19 ++++ 7 files changed, 117 insertions(+), 3 deletions(-) rename packages/search/src/{SearchProvider.php => SearchServiceProvider.php} (91%) create mode 100644 tests/search/Feature/Engines/TypesenseEngineTest.php diff --git a/composer.json b/composer.json index 6398457ee..afb19bebe 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/packages/search/composer.json b/packages/search/composer.json index 6ff8dca51..dc970032e 100644 --- a/packages/search/composer.json +++ b/packages/search/composer.json @@ -30,7 +30,7 @@ }, "laravel": { "providers": [ - "Lunar\\Search\\SearchProvider" + "SearchServiceProvider" ] } }, diff --git a/packages/search/src/SearchProvider.php b/packages/search/src/SearchServiceProvider.php similarity index 91% rename from packages/search/src/SearchProvider.php rename to packages/search/src/SearchServiceProvider.php index 8be8c20d6..05bd2ae71 100644 --- a/packages/search/src/SearchProvider.php +++ b/packages/search/src/SearchServiceProvider.php @@ -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() { diff --git a/phpunit.xml b/phpunit.xml index e43064aa0..1fc132aac 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -23,6 +23,7 @@ tests/search/Unit + tests/search/Feature tests/shipping/Feature diff --git a/tests/search/Feature/Engines/TypesenseEngineTest.php b/tests/search/Feature/Engines/TypesenseEngineTest.php new file mode 100644 index 000000000..e758895f4 --- /dev/null +++ b/tests/search/Feature/Engines/TypesenseEngineTest.php @@ -0,0 +1,88 @@ +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'); +}); diff --git a/tests/search/TestCase.php b/tests/search/TestCase.php index e51cf3cf9..f2b0dc1bd 100644 --- a/tests/search/TestCase.php +++ b/tests/search/TestCase.php @@ -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 @@ -27,6 +30,9 @@ protected function getPackageProviders($app) ConverterServiceProvider::class, NestedSetServiceProvider::class, BlinkServiceProvider::class, + ScoutServiceProvider::class, + LaravelDataServiceProvider::class, + SearchServiceProvider::class ]; } diff --git a/tests/search/Unit/Engines/AbstractEngineTest.php b/tests/search/Unit/Engines/AbstractEngineTest.php index f022af824..ec74945dc 100644 --- a/tests/search/Unit/Engines/AbstractEngineTest.php +++ b/tests/search/Unit/Engines/AbstractEngineTest.php @@ -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); +});