Skip to content

Commit 023cd03

Browse files
committed
wip
1 parent bee6ada commit 023cd03

File tree

2 files changed

+23
-55
lines changed

2 files changed

+23
-55
lines changed

tests/SearchTest.php

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -78,51 +78,18 @@ public function it_can_count_the_results()
7878
/** @test */
7979
public function it_respects_table_prefixes()
8080
{
81-
$connection = DB::connection();
82-
$originalPrefix = $connection->getTablePrefix();
81+
$this->initDatabase('prefix_');
8382

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

12895
/** @test */

tests/TestCase.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,18 @@ public function setUp(): void
2323

2424
$this->app['config']->set('app.key', 'base64:yWa/ByhLC/GUvfToOuaPD7zDwB64qkc/QkaQOrT5IpE=');
2525

26-
$this->setupDatabaseConnections();
27-
28-
$this->artisan('migrate:fresh');
29-
30-
include_once __DIR__ . '/create_tables.php';
31-
32-
(new \CreateTables)->up();
26+
$this->initDatabase();
3327
}
3428

35-
protected function setupDatabaseConnections(): void
29+
protected function initDatabase($prefix = '')
3630
{
31+
$connection = env('DB_CONNECTION', 'sqlite');
32+
3733
// Configure SQLite
3834
$this->app['config']->set('database.connections.sqlite', [
3935
'driver' => 'sqlite',
4036
'database' => ':memory:',
41-
'prefix' => '',
37+
'prefix' => $prefix,
4238
]);
4339

4440
// Configure MySQL
@@ -53,7 +49,7 @@ protected function setupDatabaseConnections(): void
5349
'unix_socket' => env('DB_SOCKET', ''),
5450
'charset' => 'utf8mb4',
5551
'collation' => 'utf8mb4_unicode_ci',
56-
'prefix' => '',
52+
'prefix' => $prefix,
5753
'prefix_indexes' => true,
5854
'strict' => true,
5955
'engine' => null,
@@ -63,10 +59,15 @@ protected function setupDatabaseConnections(): void
6359
]);
6460

6561
// Set default connection based on DB_CONNECTION env var
66-
$connection = env('DB_CONNECTION', 'sqlite');
6762
$this->app['config']->set('database.default', $connection);
6863

6964
DB::purge($connection);
7065
DB::setDefaultConnection($connection);
66+
67+
$this->artisan('migrate:fresh');
68+
69+
include_once __DIR__ . '/create_tables.php';
70+
71+
(new \CreateTables)->up();
7172
}
7273
}

0 commit comments

Comments
 (0)