FulltextTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database\MySql;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. /**
  7. * @requires extension pdo_mysql
  8. * @requires OS Linux|Darwin
  9. */
  10. class FulltextTest extends MySqlTestCase
  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' => 'MySQL Tutorial', 'body' => 'DBMS stands for DataBase ...'],
  30. ['title' => 'How To Use MySQL Well', 'body' => 'After you went through a ...'],
  31. ['title' => 'Optimizing MySQL', 'body' => 'In this tutorial, we show ...'],
  32. ['title' => '1001 MySQL Tricks', 'body' => '1. Never run mysqld as root. 2. ...'],
  33. ['title' => 'MySQL vs. YourSQL', 'body' => 'In the following database comparison ...'],
  34. ['title' => 'MySQL Security', 'body' => 'When configured properly, MySQL ...'],
  35. ]);
  36. }
  37. /** @link https://dev.mysql.com/doc/refman/8.0/en/fulltext-natural-language.html */
  38. public function testWhereFulltext()
  39. {
  40. $articles = DB::table('articles')->whereFulltext(['title', 'body'], 'database')->get();
  41. $this->assertCount(2, $articles);
  42. $this->assertSame('MySQL Tutorial', $articles[0]->title);
  43. $this->assertSame('MySQL vs. YourSQL', $articles[1]->title);
  44. }
  45. /** @link https://dev.mysql.com/doc/refman/8.0/en/fulltext-boolean.html */
  46. public function testWhereFulltextWithBooleanMode()
  47. {
  48. $articles = DB::table('articles')->whereFulltext(['title', 'body'], '+MySQL -YourSQL', ['mode' => 'boolean'])->get();
  49. $this->assertCount(5, $articles);
  50. }
  51. /** @link https://dev.mysql.com/doc/refman/8.0/en/fulltext-query-expansion.html */
  52. public function testWhereFulltextWithExpandedQuery()
  53. {
  54. $articles = DB::table('articles')->whereFulltext(['title', 'body'], 'database', ['expanded' => true])->get();
  55. $this->assertCount(6, $articles);
  56. }
  57. }