@@ -28,9 +28,69 @@ public function test_complex_query_are_wrapped_and_countable()
28
28
);
29
29
30
30
$ this ->assertQueryWrapped (true , $ dataTable ->prepareCountQuery ());
31
+ $ this ->assertQueryHasNoSelect (false , $ dataTable ->prepareCountQuery ());
31
32
$ this ->assertEquals (60 , $ dataTable ->count ());
32
33
}
33
34
35
+ public function test_complex_query_use_select_in_count ()
36
+ {
37
+ /** @var \Yajra\DataTables\QueryDataTable $dataTable */
38
+ $ dataTable = app ('datatables ' )->of (
39
+ DB ::table ('users ' )
40
+ ->select ('users.* ' )
41
+ ->addSelect ([
42
+ 'last_post_id ' => DB ::table ('posts ' )
43
+ ->whereColumn ('posts.user_id ' , 'users.id ' )
44
+ ->orderBy ('created_at ' )
45
+ ->select ('id ' ),
46
+ ])
47
+ ->orderBy (DB ::table ('posts ' )->whereColumn ('posts.user_id ' , 'users.id ' )->orderBy ('created_at ' )->select ('created_at ' )
48
+ )
49
+ );
50
+
51
+ $ this ->assertQueryHasNoSelect (false , $ dataTable ->prepareCountQuery ());
52
+ $ this ->assertEquals (20 , $ dataTable ->count ());
53
+ }
54
+
55
+ public function test_complex_query_can_ignore_select_in_count ()
56
+ {
57
+ /** @var \Yajra\DataTables\QueryDataTable $dataTable */
58
+ $ dataTable = app ('datatables ' )->of (
59
+ DB ::table ('users ' )
60
+ ->select ('users.* ' )
61
+ ->addSelect ([
62
+ 'last_post_id ' => DB ::table ('posts ' )
63
+ ->whereColumn ('posts.user_id ' , 'users.id ' )
64
+ ->orderBy ('created_at ' )
65
+ ->select ('id ' ),
66
+ ])
67
+ ->orderBy (DB ::table ('posts ' )->whereColumn ('posts.user_id ' , 'users.id ' )->orderBy ('created_at ' )->select ('created_at ' )
68
+ )
69
+ )->ignoreSelectsInCountQuery ();
70
+
71
+ $ this ->assertQueryHasNoSelect (true , $ dataTable ->prepareCountQuery ());
72
+ $ this ->assertEquals (20 , $ dataTable ->count ());
73
+ }
74
+
75
+ public function test_simple_queries_with_complexe_select_are_wrapped_without_selects ()
76
+ {
77
+ /** @var \Yajra\DataTables\QueryDataTable $dataTable */
78
+ $ dataTable = app ('datatables ' )->of (
79
+ DB ::table ('users ' )
80
+ ->select ('users.* ' )
81
+ ->addSelect ([
82
+ 'last_post_id ' => DB ::table ('posts ' )
83
+ ->whereColumn ('posts.user_id ' , 'users.id ' )
84
+ ->orderBy ('created_at ' )
85
+ ->select ('id ' ),
86
+ ])
87
+ );
88
+
89
+ $ this ->assertQueryWrapped (true , $ dataTable ->prepareCountQuery ());
90
+ $ this ->assertQueryHasNoSelect (true , $ dataTable ->prepareCountQuery ());
91
+ $ this ->assertEquals (20 , $ dataTable ->count ());
92
+ }
93
+
34
94
public function test_simple_queries_are_not_wrapped_and_countable ()
35
95
{
36
96
/** @var \Yajra\DataTables\QueryDataTable $dataTable */
@@ -42,9 +102,20 @@ public function test_simple_queries_are_not_wrapped_and_countable()
42
102
$ this ->assertEquals (20 , $ dataTable ->count ());
43
103
}
44
104
105
+ public function test_complexe_queries_can_be_wrapped_and_countable ()
106
+ {
107
+ /** @var \Yajra\DataTables\QueryDataTable $dataTable */
108
+ $ dataTable = app ('datatables ' )->of (
109
+ User::with ('posts ' )->select ('users.* ' )
110
+ );
111
+
112
+ $ this ->assertQueryWrapped (false , $ dataTable ->prepareCountQuery ());
113
+ $ this ->assertEquals (20 , $ dataTable ->count ());
114
+ }
115
+
45
116
/**
46
- * @param $expected bool
47
- * @param $query \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
117
+ * @param $expected bool
118
+ * @param $query \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
48
119
* @return void
49
120
*/
50
121
protected function assertQueryWrapped ($ expected , $ query )
@@ -53,4 +124,16 @@ protected function assertQueryWrapped($expected, $query)
53
124
54
125
$ this ->assertSame ($ expected , Str::endsWith ($ sql , 'count_row_table ' ), "' {$ sql }' is not wrapped " );
55
126
}
127
+
128
+ /**
129
+ * @param $expected bool
130
+ * @param $query \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
131
+ * @return void
132
+ */
133
+ public function assertQueryHasNoSelect ($ expected , $ query )
134
+ {
135
+ $ sql = $ query ->toSql ();
136
+
137
+ $ this ->assertSame ($ expected , Str::startsWith ($ sql , 'select * from (select 1 from ' ), "' {$ sql }' is not wrapped " );
138
+ }
56
139
}
0 commit comments