From 10742519d4cc9a3dfa4fbea15f2530ce1f11b1f8 Mon Sep 17 00:00:00 2001 From: Attila Fulop <1162360+fulopattila122@users.noreply.github.com> Date: Fri, 21 Jun 2024 11:14:19 +0300 Subject: [PATCH] Added test case for search in variants as well --- src/Foundation/Search/ProductSearch.php | 5 +++++ src/Foundation/Tests/ProductSearchTest.php | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Foundation/Search/ProductSearch.php b/src/Foundation/Search/ProductSearch.php index 05d28034..4c8e8669 100644 --- a/src/Foundation/Search/ProductSearch.php +++ b/src/Foundation/Search/ProductSearch.php @@ -351,4 +351,9 @@ public function getResults(int $limit = null): Collection { return is_null($limit) ? $this->getSearcher()->search() : $this->getSearcher()->simplePaginate($limit)->search()->getCollection(); } + + public function includeVariants(): self + { + return $this; + } } diff --git a/src/Foundation/Tests/ProductSearchTest.php b/src/Foundation/Tests/ProductSearchTest.php index 166acfd3..3ff251da 100644 --- a/src/Foundation/Tests/ProductSearchTest.php +++ b/src/Foundation/Tests/ProductSearchTest.php @@ -17,6 +17,7 @@ use Illuminate\Database\Eloquent\ModelNotFoundException; use Konekt\Search\Searcher; use Vanilo\Foundation\Models\MasterProduct; +use Vanilo\Foundation\Models\MasterProductVariant; use Vanilo\Foundation\Models\Product; use Vanilo\Foundation\Models\Taxon; use Vanilo\Foundation\Search\ProductSearch; @@ -691,4 +692,25 @@ public function it_can_be_extended_using_macros() { $finder = new ProductSearch(); } + + /** @test */ + public function it_can_optionally_include_variants() + { + factory(Product::class, 7)->create([ + 'state' => ProductState::ACTIVE, + ]); + $master = factory(MasterProduct::class)->create([ + 'state' => ProductState::ACTIVE, + ]); + factory(MasterProductVariant::class, 3)->create([ + 'state' => ProductState::ACTIVE, + 'master_product_id' => $master->id, + ]); + + $this->assertCount(8, (new ProductSearch())->getResults()); + + $finder = new ProductSearch(); + $finder->includeVariants(); + $this->assertCount(11, $finder->getResults()); + } }