44
55namespace Flow \PostgreSql \Tests \Unit \AST \Nodes ;
66
7- use function Flow \PostgreSql \DSL \sql_parse ;
7+ use function Flow \PostgreSql \DSL \{ col , derived , eq , func , literal , select , sql_parse , star , table , table_func } ;
88
99use Flow \PostgreSql \AST \Nodes \Exception \InvalidFromNodeException ;
10- use Flow \PostgreSql \AST \Nodes \From ;
10+ use Flow \PostgreSql \AST \Nodes \{ From , Tables } ;
1111use Flow \PostgreSql \AST \Nodes \Statement \SelectStatement ;
1212use Flow \PostgreSql \Protobuf \AST \Node ;
1313use PHPUnit \Framework \TestCase ;
@@ -23,7 +23,12 @@ protected function setUp() : void
2323
2424 public function test_accepts_join_expr_node () : void
2525 {
26- $ statement = sql_parse ('SELECT * FROM users JOIN orders ON users.id = orders.user_id ' )->statements ()->first ();
26+ $ statement = sql_parse (
27+ select (star ())
28+ ->from (table ('users ' ))
29+ ->join (table ('orders ' ), eq (col ('id ' , 'users ' ), col ('user_id ' , 'orders ' )))
30+ ->toSql ()
31+ )->statements ()->first ();
2732 self ::assertInstanceOf (SelectStatement::class, $ statement );
2833
2934 $ from = $ statement ->from ();
@@ -32,7 +37,11 @@ public function test_accepts_join_expr_node() : void
3237
3338 public function test_accepts_range_function_node () : void
3439 {
35- $ statement = sql_parse ('SELECT * FROM generate_series(1, 10) ' )->statements ()->first ();
40+ $ statement = sql_parse (
41+ select (star ())
42+ ->from (table_func (func ('generate_series ' , [literal (1 ), literal (10 )])))
43+ ->toSql ()
44+ )->statements ()->first ();
3645 self ::assertInstanceOf (SelectStatement::class, $ statement );
3746
3847 $ from = $ statement ->from ();
@@ -41,7 +50,11 @@ public function test_accepts_range_function_node() : void
4150
4251 public function test_accepts_range_subselect_node () : void
4352 {
44- $ statement = sql_parse ('SELECT * FROM (SELECT 1) AS t ' )->statements ()->first ();
53+ $ statement = sql_parse (
54+ select (star ())
55+ ->from (derived (select (literal (1 )), 't ' ))
56+ ->toSql ()
57+ )->statements ()->first ();
4558 self ::assertInstanceOf (SelectStatement::class, $ statement );
4659
4760 $ from = $ statement ->from ();
@@ -50,7 +63,9 @@ public function test_accepts_range_subselect_node() : void
5063
5164 public function test_accepts_range_var_node () : void
5265 {
53- $ statement = sql_parse ('SELECT * FROM users ' )->statements ()->first ();
66+ $ statement = sql_parse (
67+ select (star ())->from (table ('users ' ))->toSql ()
68+ )->statements ()->first ();
5469 self ::assertInstanceOf (SelectStatement::class, $ statement );
5570
5671 $ from = $ statement ->from ();
@@ -59,7 +74,9 @@ public function test_accepts_range_var_node() : void
5974
6075 public function test_count_returns_number_of_from_nodes () : void
6176 {
62- $ statement = sql_parse ('SELECT * FROM users, orders ' )->statements ()->first ();
77+ $ statement = sql_parse (
78+ select (star ())->from (table ('users ' ), table ('orders ' ))->toSql ()
79+ )->statements ()->first ();
6380 self ::assertInstanceOf (SelectStatement::class, $ statement );
6481
6582 self ::assertCount (2 , $ statement ->from ());
@@ -79,15 +96,21 @@ public function test_empty_nodes_array_creates_empty_from() : void
7996
8097 public function test_has_function_returns_false_for_regular_table () : void
8198 {
82- $ statement = sql_parse ('SELECT * FROM users ' )->statements ()->first ();
99+ $ statement = sql_parse (
100+ select (star ())->from (table ('users ' ))->toSql ()
101+ )->statements ()->first ();
83102 self ::assertInstanceOf (SelectStatement::class, $ statement );
84103
85104 self ::assertFalse ($ statement ->from ()->hasFunction ());
86105 }
87106
88107 public function test_has_function_returns_true_for_function_in_from () : void
89108 {
90- $ statement = sql_parse ('SELECT * FROM generate_series(1, 10) ' )->statements ()->first ();
109+ $ statement = sql_parse (
110+ select (star ())
111+ ->from (table_func (func ('generate_series ' , [literal (1 ), literal (10 )])))
112+ ->toSql ()
113+ )->statements ()->first ();
91114 self ::assertInstanceOf (SelectStatement::class, $ statement );
92115
93116 self ::assertTrue ($ statement ->from ()->hasFunction ());
@@ -101,6 +124,86 @@ public function test_has_function_returns_true_for_unnest_function() : void
101124 self ::assertTrue ($ statement ->from ()->hasFunction ());
102125 }
103126
127+ public function test_tables_returns_empty_collection_for_empty_from () : void
128+ {
129+ $ from = new From ([]);
130+
131+ $ tables = $ from ->tables ();
132+
133+ self ::assertInstanceOf (Tables::class, $ tables );
134+ self ::assertTrue ($ tables ->isEmpty ());
135+ }
136+
137+ public function test_tables_returns_empty_collection_for_function () : void
138+ {
139+ $ statement = sql_parse (
140+ select (star ())
141+ ->from (table_func (func ('generate_series ' , [literal (1 ), literal (10 )])))
142+ ->toSql ()
143+ )->statements ()->first ();
144+ self ::assertInstanceOf (SelectStatement::class, $ statement );
145+
146+ $ tables = $ statement ->from ()->tables ();
147+
148+ self ::assertTrue ($ tables ->isEmpty ());
149+ }
150+
151+ public function test_tables_returns_empty_collection_for_join () : void
152+ {
153+ $ statement = sql_parse (
154+ select (star ())
155+ ->from (table ('users ' ))
156+ ->join (table ('orders ' ), eq (col ('id ' , 'users ' ), col ('user_id ' , 'orders ' )))
157+ ->toSql ()
158+ )->statements ()->first ();
159+ self ::assertInstanceOf (SelectStatement::class, $ statement );
160+
161+ $ tables = $ statement ->from ()->tables ();
162+
163+ self ::assertTrue ($ tables ->isEmpty ());
164+ }
165+
166+ public function test_tables_returns_empty_collection_for_subquery () : void
167+ {
168+ $ statement = sql_parse (
169+ select (star ())
170+ ->from (derived (select (literal (1 )), 't ' ))
171+ ->toSql ()
172+ )->statements ()->first ();
173+ self ::assertInstanceOf (SelectStatement::class, $ statement );
174+
175+ $ tables = $ statement ->from ()->tables ();
176+
177+ self ::assertTrue ($ tables ->isEmpty ());
178+ }
179+
180+ public function test_tables_returns_multiple_tables () : void
181+ {
182+ $ statement = sql_parse (
183+ select (star ())->from (table ('users ' ), table ('orders ' ))->toSql ()
184+ )->statements ()->first ();
185+ self ::assertInstanceOf (SelectStatement::class, $ statement );
186+
187+ $ tables = $ statement ->from ()->tables ();
188+
189+ self ::assertCount (2 , $ tables );
190+ self ::assertSame ('users ' , $ tables ->first ()?->name());
191+ self ::assertSame ('orders ' , $ tables ->last ()?->name());
192+ }
193+
194+ public function test_tables_returns_single_table () : void
195+ {
196+ $ statement = sql_parse (
197+ select (star ())->from (table ('users ' ))->toSql ()
198+ )->statements ()->first ();
199+ self ::assertInstanceOf (SelectStatement::class, $ statement );
200+
201+ $ tables = $ statement ->from ()->tables ();
202+
203+ self ::assertTrue ($ tables ->isSingle ());
204+ self ::assertSame ('users ' , $ tables ->first ()?->name());
205+ }
206+
104207 public function test_throws_exception_for_invalid_node () : void
105208 {
106209 $ invalidNode = new Node ();
0 commit comments