|  | 
| 7 | 7 | use Illuminate\Support\Carbon; | 
| 8 | 8 | use Illuminate\Support\Collection; | 
| 9 | 9 | use Illuminate\Support\Facades\DB; | 
|  | 10 | +use Illuminate\Support\Facades\Schema; | 
| 10 | 11 | use ProtoneMedia\LaravelCrossEloquentSearch\Exceptions\OrderByRelevanceException; | 
| 11 | 12 | use ProtoneMedia\LaravelCrossEloquentSearch\Search; | 
| 12 | 13 | use ProtoneMedia\LaravelCrossEloquentSearch\Searcher; | 
| @@ -77,30 +78,48 @@ public function it_can_count_the_results() | 
| 77 | 78 |     /** @test */ | 
| 78 | 79 |     public function it_respects_table_prefixes() | 
| 79 | 80 |     { | 
| 80 |  | -        // Create a new in-memory database with prefix for testing | 
| 81 | 81 |         $connection = DB::connection(); | 
| 82 | 82 |         $originalPrefix = $connection->getTablePrefix(); | 
| 83 | 83 | 
 | 
| 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 | +        } | 
| 104 | 123 |     } | 
| 105 | 124 | 
 | 
| 106 | 125 |     /** @test */ | 
|  | 
0 commit comments