Skip to content

Commit 3da32d2

Browse files
committed
wip
1 parent e93864e commit 3da32d2

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

src/QueryCompiler.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct(
2424
public function compile(array $modelQueries): QueryBuilder
2525
{
2626
$queries = collect($modelQueries);
27-
27+
2828
/** @var QueryBuilder $firstQuery */
2929
$firstQuery = $queries->shift()->toBase();
3030

@@ -41,7 +41,7 @@ public function compile(array $modelQueries): QueryBuilder
4141
*/
4242
private function needsUnionWrapper(QueryBuilder $query): bool
4343
{
44-
return $this->hasMultipleModels()
44+
return $this->hasMultipleModels()
4545
&& !$this->grammar->supportsUnionOrdering();
4646
}
4747

@@ -101,7 +101,7 @@ private function applyModelOrdering(QueryBuilder $query): void
101101
$modelOrderKeys = $this->models->map(function ($modelToSearchThrough) {
102102
return $this->grammar->wrap($modelToSearchThrough->getModelKey('model_order'));
103103
})->toArray();
104-
104+
105105
$modelCoalesceExpr = $this->grammar->coalesce($modelOrderKeys);
106106

107107
$query->orderByRaw($modelCoalesceExpr . ' ' . $this->getOrderDirection());
@@ -129,7 +129,7 @@ private function applyStandardOrdering(QueryBuilder $query): void
129129
$orderKeys = $this->models->map(function ($modelToSearchThrough) {
130130
return $this->grammar->wrap($modelToSearchThrough->getModelKey('order'));
131131
})->toArray();
132-
132+
133133
$coalesceExpr = $this->grammar->coalesce($orderKeys);
134134

135135
$query->orderByRaw($coalesceExpr . ' ' . $this->getOrderDirection());

tests/SearchTest.php

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Carbon;
88
use Illuminate\Support\Collection;
99
use Illuminate\Support\Facades\DB;
10+
use Illuminate\Support\Facades\Schema;
1011
use ProtoneMedia\LaravelCrossEloquentSearch\Exceptions\OrderByRelevanceException;
1112
use ProtoneMedia\LaravelCrossEloquentSearch\Search;
1213
use ProtoneMedia\LaravelCrossEloquentSearch\Searcher;
@@ -77,30 +78,48 @@ public function it_can_count_the_results()
7778
/** @test */
7879
public function it_respects_table_prefixes()
7980
{
80-
// Create a new in-memory database with prefix for testing
8181
$connection = DB::connection();
8282
$originalPrefix = $connection->getTablePrefix();
8383

84-
// Create tables first, then set prefix and recreate
85-
$connection->setTablePrefix('prefix_');
86-
87-
// Create the prefixed tables manually
88-
include_once __DIR__ . '/create_tables.php';
89-
(new \CreateTables)->up();
90-
91-
$postA = Post::create(['title' => 'foo']);
92-
$postB = Post::create(['title' => 'bar']);
93-
$videoA = Video::create(['title' => 'foo']);
94-
$videoB = Video::create(['title' => 'bar', 'subtitle' => 'foo']);
95-
96-
$count = Search::add(Post::class, 'title')
97-
->add(Video::class, ['title', 'subtitle'])
98-
->count('foo');
99-
100-
$this->assertEquals(3, $count);
101-
102-
// Reset prefix and clean up
103-
$connection->setTablePrefix($originalPrefix);
84+
try {
85+
// Drop existing tables first
86+
Schema::dropIfExists('posts');
87+
Schema::dropIfExists('videos');
88+
Schema::dropIfExists('comments');
89+
Schema::dropIfExists('blogs');
90+
Schema::dropIfExists('pages');
91+
92+
// Set prefix and create prefixed tables
93+
$connection->setTablePrefix('prefix_');
94+
95+
include_once __DIR__ . '/create_tables.php';
96+
(new \CreateTables)->up();
97+
98+
$postA = Post::create(['title' => 'foo']);
99+
$postB = Post::create(['title' => 'bar']);
100+
$videoA = Video::create(['title' => 'foo']);
101+
$videoB = Video::create(['title' => 'bar', 'subtitle' => 'foo']);
102+
103+
$count = Search::add(Post::class, 'title')
104+
->add(Video::class, ['title', 'subtitle'])
105+
->count('foo');
106+
107+
$this->assertEquals(3, $count);
108+
} finally {
109+
// Always reset prefix and recreate original tables
110+
$connection->setTablePrefix($originalPrefix);
111+
112+
// Drop prefixed tables
113+
Schema::dropIfExists('prefix_posts');
114+
Schema::dropIfExists('prefix_videos');
115+
Schema::dropIfExists('prefix_comments');
116+
Schema::dropIfExists('prefix_blogs');
117+
Schema::dropIfExists('prefix_pages');
118+
119+
// Recreate original tables for other tests
120+
include_once __DIR__ . '/create_tables.php';
121+
(new \CreateTables)->up();
122+
}
104123
}
105124

106125
/** @test */

0 commit comments

Comments
 (0)