Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/core/src/Search/CustomerIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public function getFilterableFields(): array
public function makeAllSearchableUsing(Builder $query): Builder
{
return $query->with([
'users',
'users' => fn ($query) => $query->select(
$query->getModel()->qualifyColumn('id'),
$query->getModel()->qualifyColumn('email'),
),
]);
}

Expand Down
32 changes: 27 additions & 5 deletions packages/core/src/Search/OrderIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,33 @@ public function getFilterableFields(): array
public function makeAllSearchableUsing(Builder $query): Builder
{
return $query->with([
'channel',
'transactions',
'productLines',
'addresses',
'tags',
'channel' => fn ($query) => $query->select('id', 'name'),
'transactions' => fn ($query) => $query->select('id', 'order_id', 'reference'),
'productLines' => fn ($query) => $query->select('id', 'order_id', 'description', 'identifier'),
'addresses' => fn ($query) => $query->select(
'id',
'order_id',
'country_id',
'type',
'first_name',
'last_name',
'company_name',
'tax_identifier',
'line_one',
'line_two',
'line_three',
'city',
'state',
'postcode',
'contact_email',
'contact_phone',
)->with([
'country' => fn ($query) => $query->select('id', 'name'),
]),
'tags' => fn ($query) => $query->select(
$query->getModel()->qualifyColumn('id'),
$query->getModel()->qualifyColumn('value'),
),
]);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/Search/ProductIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function makeAllSearchableUsing(Builder $query): Builder
{
return $query->with([
'thumbnail',
'variants',
'productType',
'brand',
'variants' => fn ($query) => $query->select('id', 'sku', 'product_id'),
'productType' => fn ($query) => $query->select('id', 'name'),
'brand' => fn ($query) => $query->select('id', 'name'),
]);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/Search/ProductOptionIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public function getFilterableFields(): array

public function makeAllSearchableUsing(Builder $query): Builder
{
return $query;
return $query->with([
'values' => fn ($query) => $query->select('id', 'product_option_id', 'name'),
]);
}

public function toSearchableArray(Model $model): array
Expand Down
5 changes: 2 additions & 3 deletions tests/core/Unit/Search/BrandIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Lunar\Search\BrandIndexer;
use Lunar\Tests\Core\TestCase;

uses(TestCase::class);
uses(TestCase::class)->group('search', 'indexer');

uses(RefreshDatabase::class);

Expand Down Expand Up @@ -64,5 +64,4 @@
->and($data)->not()->toHaveKey($attributeC->handle)
->and($data)->toHaveKey($attributeD->handle.'_en')
->and($data)->toHaveKey($attributeD->handle.'_dk');

})->group('foo');
});
2 changes: 1 addition & 1 deletion tests/core/Unit/Search/CustomerIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Lunar\Tests\Core\Stubs\User;
use Lunar\Tests\Core\TestCase;

uses(TestCase::class);
uses(TestCase::class)->group('search', 'indexer');

uses(RefreshDatabase::class);

Expand Down
47 changes: 46 additions & 1 deletion tests/core/Unit/Search/OrderIndexerTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?php

use Illuminate\Foundation\Testing\RefreshDatabase;
use Lunar\Models\Country;
use Lunar\Models\Currency;
use Lunar\Models\Order;
use Lunar\Models\OrderAddress;
use Lunar\Models\OrderLine;
use Lunar\Models\Tag;
use Lunar\Models\Transaction;
use Lunar\Search\OrderIndexer;
use Lunar\Tests\Core\TestCase;

uses(TestCase::class);
uses(TestCase::class)->group('search', 'indexer');

uses(RefreshDatabase::class);

Expand All @@ -16,6 +21,10 @@
'default' => true,
]);

$country = Country::factory()->create([
'name' => 'United Kingdom',
]);

$order = Order::factory()->create([
'user_id' => null,
'placed_at' => now(),
Expand All @@ -24,9 +33,45 @@
],
]);

$transaction = Transaction::factory()->create([
'order_id' => $order->id,
]);

$line = OrderLine::factory()->create([
'order_id' => $order->id,
'type' => 'physical',
]);

OrderLine::factory()->create([
'order_id' => $order->id,
'type' => 'shipping',
]);

$address = OrderAddress::factory()->create([
'order_id' => $order->id,
'country_id' => $country->id,
'type' => 'shipping',
]);

$tag = Tag::factory()->create([
'value' => 'vip',
]);

$order->tags()->attach($tag);

$data = app(OrderIndexer::class)->toSearchableArray($order);

expect($data['currency_code'])->toEqual('GBP');
expect($data['channel'])->toEqual($order->channel->name);
expect($data['total'])->toEqual($order->total->value);
expect($data['charges']->pluck('reference')->all())->toBe([$transaction->reference]);
expect($data['lines'])->toEqual([[
'description' => $line->description,
'identifier' => $line->identifier,
]]);
expect($data['shipping_first_name'])->toEqual($address->first_name);
expect($data['shipping_last_name'])->toEqual($address->last_name);
expect($data['shipping_country'])->toEqual($country->name);
expect($data['shipping_fullname'])->toEqual($address->first_name.' '.$address->last_name);
expect($data['tags'])->toBe(['vip']);
});
3 changes: 2 additions & 1 deletion tests/core/Unit/Search/ProductIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Lunar\Search\ProductIndexer;
use Lunar\Tests\Core\TestCase;

uses(TestCase::class);
uses(TestCase::class)->group('search', 'indexer');

uses(RefreshDatabase::class);

Expand Down Expand Up @@ -60,6 +60,7 @@
]);

$data = app(ProductIndexer::class)->toSearchableArray($product);

expect($data)->toHaveKey('id');
expect($data['skus'])->toBe([$variant->sku]);
expect($data['status'])->toEqual($product->status);
Expand Down
13 changes: 11 additions & 2 deletions tests/core/Unit/Search/ProductOptionIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@

use Illuminate\Foundation\Testing\RefreshDatabase;
use Lunar\Models\ProductOption;
use Lunar\Models\ProductOptionValue;
use Lunar\Search\ProductOptionIndexer;
use Lunar\Tests\Core\TestCase;

uses(TestCase::class);
uses(TestCase::class)->group('search', 'indexer');

uses(RefreshDatabase::class);

test('can return correct searchable data', function () {
$productOption = ProductOption::factory()->create();

$value = ProductOptionValue::factory()->create([
'product_option_id' => $productOption->id,
'name' => [
'en' => 'Small',
],
]);

$data = app(ProductOptionIndexer::class)->toSearchableArray($productOption);

expect($data['name_en'])->toEqual($productOption->name->en)
->and($data['label_en'])->toEqual($productOption->label->en);
->and($data['label_en'])->toEqual($productOption->label->en)
->and($data['option_'.$value->id.'_en'])->toEqual('Small');
});
2 changes: 1 addition & 1 deletion tests/core/Unit/Search/ScoutIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Lunar\Search\ScoutIndexer;
use Lunar\Tests\Core\TestCase;

uses(TestCase::class);
uses(TestCase::class)->group('search', 'indexer');

uses(RefreshDatabase::class);

Expand Down