12
12
namespace HyperfTest \Database \PgSQL \Cases ;
13
13
14
14
use Hyperf \Database \Connection ;
15
+ use Hyperf \Database \ConnectionInterface ;
16
+ use Hyperf \Database \PgSQL \Query \Grammars \PostgresGrammar as PostgresQueryGrammar ;
17
+ use Hyperf \Database \PgSQL \Query \Processors \PostgresProcessor ;
15
18
use Hyperf \Database \PgSQL \Schema \Grammars \PostgresGrammar ;
16
19
use Hyperf \Database \PgSQL \Schema \PostgresBuilder ;
20
+ use Hyperf \Database \Query \Builder ;
21
+ use Hyperf \Database \Schema \Blueprint ;
22
+ use Hyperf \Database \Schema \Schema ;
23
+ use Hyperf \DbConnection \Db ;
24
+ use HyperfTest \Database \PgSQL \Stubs \ContainerStub ;
17
25
use Mockery as m ;
18
26
use PHPUnit \Framework \TestCase ;
19
27
@@ -25,6 +33,10 @@ class DatabasePostgresBuilderTest extends TestCase
25
33
{
26
34
protected function tearDown (): void
27
35
{
36
+ if (SWOOLE_MAJOR_VERSION >= 5 ) {
37
+ ContainerStub::getContainer ();
38
+ Schema::dropIfExists ('test_full_text_index ' );
39
+ }
28
40
m::close ();
29
41
}
30
42
@@ -57,8 +69,94 @@ public function testDropDatabaseIfExists()
57
69
$ this ->assertEquals (true , $ builder ->dropDatabaseIfExists ('my_database_a ' ));
58
70
}
59
71
60
- protected function getBuilder ($ connection )
72
+ public function testWhereFullText ()
73
+ {
74
+ $ builder = $ this ->getPostgresBuilderWithProcessor ();
75
+ $ builder ->select ('* ' )->from ('users ' )->whereFullText ('body ' , 'Hello World ' );
76
+ $ this ->assertSame ('select * from "users" where (to_tsvector( \'english \', "body")) @@ plainto_tsquery( \'english \', ?) ' , $ builder ->toSql ());
77
+ $ this ->assertEquals (['Hello World ' ], $ builder ->getBindings ());
78
+
79
+ $ builder = $ this ->getPostgresBuilderWithProcessor ();
80
+ $ builder ->select ('* ' )->from ('users ' )->whereFullText ('body ' , 'Hello World ' , ['language ' => 'simple ' ]);
81
+ $ this ->assertSame ('select * from "users" where (to_tsvector( \'simple \', "body")) @@ plainto_tsquery( \'simple \', ?) ' , $ builder ->toSql ());
82
+ $ this ->assertEquals (['Hello World ' ], $ builder ->getBindings ());
83
+
84
+ $ builder = $ this ->getPostgresBuilderWithProcessor ();
85
+ $ builder ->select ('* ' )->from ('users ' )->whereFullText ('body ' , 'Hello World ' , ['mode ' => 'plain ' ]);
86
+ $ this ->assertSame ('select * from "users" where (to_tsvector( \'english \', "body")) @@ plainto_tsquery( \'english \', ?) ' , $ builder ->toSql ());
87
+ $ this ->assertEquals (['Hello World ' ], $ builder ->getBindings ());
88
+
89
+ $ builder = $ this ->getPostgresBuilderWithProcessor ();
90
+ $ builder ->select ('* ' )->from ('users ' )->whereFullText ('body ' , 'Hello World ' , ['mode ' => 'phrase ' ]);
91
+ $ this ->assertSame ('select * from "users" where (to_tsvector( \'english \', "body")) @@ phraseto_tsquery( \'english \', ?) ' , $ builder ->toSql ());
92
+ $ this ->assertEquals (['Hello World ' ], $ builder ->getBindings ());
93
+
94
+ $ builder = $ this ->getPostgresBuilderWithProcessor ();
95
+ $ builder ->select ('* ' )->from ('users ' )->whereFullText ('body ' , '+Hello -World ' , ['mode ' => 'websearch ' ]);
96
+ $ this ->assertSame ('select * from "users" where (to_tsvector( \'english \', "body")) @@ websearch_to_tsquery( \'english \', ?) ' , $ builder ->toSql ());
97
+ $ this ->assertEquals (['+Hello -World ' ], $ builder ->getBindings ());
98
+
99
+ $ builder = $ this ->getPostgresBuilderWithProcessor ();
100
+ $ builder ->select ('* ' )->from ('users ' )->whereFullText ('body ' , 'Hello World ' , ['language ' => 'simple ' , 'mode ' => 'plain ' ]);
101
+ $ this ->assertSame ('select * from "users" where (to_tsvector( \'simple \', "body")) @@ plainto_tsquery( \'simple \', ?) ' , $ builder ->toSql ());
102
+ $ this ->assertEquals (['Hello World ' ], $ builder ->getBindings ());
103
+
104
+ $ builder = $ this ->getPostgresBuilderWithProcessor ();
105
+ $ builder ->select ('* ' )->from ('users ' )->whereFullText (['body ' , 'title ' ], 'Car Plane ' );
106
+ $ this ->assertSame ('select * from "users" where (to_tsvector( \'english \', "body") || to_tsvector( \'english \', "title")) @@ plainto_tsquery( \'english \', ?) ' , $ builder ->toSql ());
107
+ $ this ->assertEquals (['Car Plane ' ], $ builder ->getBindings ());
108
+ }
109
+
110
+ public function testWhereFullTextForReal ()
111
+ {
112
+ if (SWOOLE_MAJOR_VERSION < 5 ) {
113
+ $ this ->markTestSkipped ('PostgreSql requires Swoole version >= 5.0.0 ' );
114
+ }
115
+
116
+ $ container = ContainerStub::getContainer ();
117
+ $ container ->shouldReceive ('get ' )->with (Db::class)->andReturn (new Db ($ container ));
118
+
119
+ Schema::create ('test_full_text_index ' , function (Blueprint $ table ) {
120
+ $ table ->id ('id ' );
121
+ $ table ->string ('title ' , 200 );
122
+ $ table ->text ('body ' );
123
+ $ table ->fullText (['title ' , 'body ' ]);
124
+ });
125
+
126
+ Db::table ('test_full_text_index ' )->insert ([
127
+ ['title ' => 'PostgreSQL Tutorial ' , 'body ' => 'DBMS stands for DataBase ... ' ],
128
+ ['title ' => 'How To Use PostgreSQL Well ' , 'body ' => 'After you went through a ... ' ],
129
+ ['title ' => 'Optimizing PostgreSQL ' , 'body ' => 'In this tutorial, we show ... ' ],
130
+ ['title ' => '1001 PostgreSQL Tricks ' , 'body ' => '1. Never run mysqld as root. 2. ... ' ],
131
+ ['title ' => 'PostgreSQL vs. YourSQL ' , 'body ' => 'In the following database comparison ... ' ],
132
+ ['title ' => 'PostgreSQL Security ' , 'body ' => 'When configured properly, PostgreSQL ... ' ],
133
+ ]);
134
+
135
+ $ result = Db::table ('test_full_text_index ' )->whereFulltext (['title ' , 'body ' ], 'database ' )->orderBy ('id ' )->get ();
136
+ $ this ->assertCount (2 , $ result );
137
+ $ this ->assertSame ('PostgreSQL Tutorial ' , $ result [0 ]['title ' ]);
138
+ $ this ->assertSame ('PostgreSQL vs. YourSQL ' , $ result [1 ]['title ' ]);
139
+
140
+ $ result = Db::table ('test_full_text_index ' )->whereFulltext (['title ' , 'body ' ], '+PostgreSQL -YourSQL ' , ['mode ' => 'websearch ' ])->get ();
141
+ $ this ->assertCount (5 , $ result );
142
+
143
+ $ result = Db::table ('test_full_text_index ' )->whereFulltext (['title ' , 'body ' ], 'PostgreSQL tutorial ' , ['mode ' => 'plain ' ])->get ();
144
+ $ this ->assertCount (2 , $ result );
145
+
146
+ $ result = Db::table ('test_full_text_index ' )->whereFulltext (['title ' , 'body ' ], 'PostgreSQL tutorial ' , ['mode ' => 'phrase ' ])->get ();
147
+ $ this ->assertCount (1 , $ result );
148
+ }
149
+
150
+ protected function getBuilder ($ connection ): PostgresBuilder
61
151
{
62
152
return new PostgresBuilder ($ connection );
63
153
}
154
+
155
+ protected function getPostgresBuilderWithProcessor (): Builder
156
+ {
157
+ $ grammar = new PostgresQueryGrammar ();
158
+ $ processor = new PostgresProcessor ();
159
+
160
+ return new Builder (m::mock (ConnectionInterface::class), $ grammar , $ processor );
161
+ }
64
162
}
0 commit comments