FulltextTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database\Postgres;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. /**
  7. * @requires extension pdo_pgsql
  8. * @requires OS Linux|Darwin
  9. */
  10. class FulltextTest extends PostgresTestCase
  11. {
  12. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  13. {
  14. Schema::create('articles', function (Blueprint $table) {
  15. $table->id('id');
  16. $table->string('title', 200);
  17. $table->text('body');
  18. $table->fulltext(['title', 'body']);
  19. });
  20. }
  21. protected function destroyDatabaseMigrations()
  22. {
  23. Schema::drop('articles');
  24. }
  25. protected function setUp(): void
  26. {
  27. parent::setUp();
  28. DB::table('articles')->insert([
  29. ['title' => 'PostgreSQL Tutorial', 'body' => 'DBMS stands for DataBase ...'],
  30. ['title' => 'How To Use PostgreSQL Well', 'body' => 'After you went through a ...'],
  31. ['title' => 'Optimizing PostgreSQL', 'body' => 'In this tutorial, we show ...'],
  32. ['title' => '1001 PostgreSQL Tricks', 'body' => '1. Never run mysqld as root. 2. ...'],
  33. ['title' => 'PostgreSQL vs. YourSQL', 'body' => 'In the following database comparison ...'],
  34. ['title' => 'PostgreSQL Security', 'body' => 'When configured properly, PostgreSQL ...'],
  35. ]);
  36. }
  37. public function testWhereFulltext()
  38. {
  39. $articles = DB::table('articles')->whereFulltext(['title', 'body'], 'database')->orderBy('id')->get();
  40. $this->assertCount(2, $articles);
  41. $this->assertSame('PostgreSQL Tutorial', $articles[0]->title);
  42. $this->assertSame('PostgreSQL vs. YourSQL', $articles[1]->title);
  43. }
  44. public function testWhereFulltextWithWebsearch()
  45. {
  46. $articles = DB::table('articles')->whereFulltext(['title', 'body'], '+PostgreSQL -YourSQL', ['mode' => 'websearch'])->get();
  47. $this->assertCount(5, $articles);
  48. }
  49. public function testWhereFulltextWithPlain()
  50. {
  51. $articles = DB::table('articles')->whereFulltext(['title', 'body'], 'PostgreSQL tutorial', ['mode' => 'plain'])->get();
  52. $this->assertCount(2, $articles);
  53. }
  54. public function testWhereFulltextWithPhrase()
  55. {
  56. $articles = DB::table('articles')->whereFulltext(['title', 'body'], 'PostgreSQL tutorial', ['mode' => 'phrase'])->get();
  57. $this->assertCount(1, $articles);
  58. }
  59. }