|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel; |
|
6
|
|
|
use Backpack\CRUD\Tests\config\Models\User; |
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Query |
|
11
|
|
|
*/ |
|
12
|
|
|
class CrudPanelQueryTest extends BaseCrudPanel |
|
13
|
|
|
{ |
|
14
|
|
|
public function setUp(): void |
|
15
|
|
|
{ |
|
16
|
|
|
parent::setUp(); |
|
17
|
|
|
|
|
18
|
|
|
$this->crudPanel->setModel(User::class); |
|
19
|
|
|
} |
|
20
|
|
|
public function testItHasABaseQuery() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->assertEquals(User::query()->toSql(), $this->crudPanel->query->toSql()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testItCanAddAClauseToTheQuery() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->crudPanel->addClause('where', 'id', 1); |
|
28
|
|
|
|
|
29
|
|
|
$this->assertEquals(User::query()->where('id', 1)->toSql(), $this->crudPanel->query->toSql()); |
|
30
|
|
|
$this->assertEquals(User::query()->toSql(), $this->crudPanel->totalQuery->toSql()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testItCanAddAClauseToTheQueryUsingAClosure() |
|
34
|
|
|
{ |
|
35
|
|
|
$closure = function ($query) { |
|
36
|
|
|
$query->where('id', 1); |
|
37
|
|
|
}; |
|
38
|
|
|
|
|
39
|
|
|
$this->crudPanel->addClause($closure); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertEquals(User::query()->where('id', 1)->toSql(), $this->crudPanel->query->toSql()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testItCanAddABaseClauseToTheQuery() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->crudPanel->addBaseClause('where', 'id', 1); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertEquals(User::query()->where('id', 1)->toSql(), $this->crudPanel->query->toSql()); |
|
49
|
|
|
$this->assertEquals(User::query()->where('id', 1)->toSql(), $this->crudPanel->totalQuery->toSql()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testItCanAddABaseClauseToTheQueryUsingAClosure() |
|
53
|
|
|
{ |
|
54
|
|
|
$closure = function ($query) { |
|
55
|
|
|
$query->where('id', 1); |
|
56
|
|
|
}; |
|
57
|
|
|
|
|
58
|
|
|
$this->crudPanel->addBaseClause($closure); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertEquals(User::query()->where('id', 1)->toSql(), $this->crudPanel->totalQuery->toSql()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function itCanAddDefaultOrderToTheyQuery() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->crudPanel->orderBy('id'); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals(User::query()->orderBy('id', 'asc')->toSql(), $this->crudPanel->query->toSql()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testItCanAddDefaultOrderToTheQueryWithDirection() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->crudPanel->orderBy('id', 'desc'); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertEquals(User::query()->orderBy('id', 'desc')->toSql(), $this->crudPanel->query->toSql()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testItDoesNotSetOrderIfOrderIsInRequest() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->setupUserCreateRequest(); |
|
80
|
|
|
|
|
81
|
|
|
$this->crudPanel->getRequest()->merge(['order' => 'id']); |
|
82
|
|
|
|
|
83
|
|
|
$this->crudPanel->orderBy('id', 'desc'); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertEquals(User::query()->toSql(), $this->crudPanel->query->toSql()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testItCanAddAGroupByToTheyQuery() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->crudPanel->groupBy('name'); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertEquals(User::query()->groupBy('name')->toSql(), $this->crudPanel->query->toSql()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testItCanAddALimitToTheQuery() |
|
96
|
|
|
{ |
|
97
|
|
|
$this->crudPanel->limit(5); |
|
98
|
|
|
$this->assertEquals(User::query()->limit(5)->toSql(), $this->crudPanel->query->toSql()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testItCanTakeSomeAmountFromTheQuery() |
|
102
|
|
|
{ |
|
103
|
|
|
$this->crudPanel->take(5); |
|
104
|
|
|
$this->assertEquals(User::query()->take(5)->toSql(), $this->crudPanel->query->toSql()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testItCanSkipSomeAmountFromTheQuery() |
|
108
|
|
|
{ |
|
109
|
|
|
$this->crudPanel->skip(5); |
|
110
|
|
|
$this->assertEquals(User::query()->skip(5)->toSql(), $this->crudPanel->query->toSql()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testItCanApplyCustomOrderLogicFromAColumn() |
|
114
|
|
|
{ |
|
115
|
|
|
$this->crudPanel->addColumn([ |
|
116
|
|
|
'name' => 'name', |
|
117
|
|
|
'type' => 'text', |
|
118
|
|
|
'orderLogic' => function ($query, $column, $direction) { |
|
119
|
|
|
$query->orderBy('name', $direction); |
|
120
|
|
|
}, |
|
121
|
|
|
]); |
|
122
|
|
|
|
|
123
|
|
|
$this->crudPanel->customOrderBy($this->crudPanel->columns()['name'], 'asc'); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertEquals(User::query()->orderBy('name', 'asc')->toSql(), $this->crudPanel->query->toSql()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function testItSkipsCustomOrderLogicIfNotCustomOrderIsSet() |
|
129
|
|
|
{ |
|
130
|
|
|
$this->crudPanel->addColumn([ |
|
131
|
|
|
'name' => 'name', |
|
132
|
|
|
'type' => 'text', |
|
133
|
|
|
]); |
|
134
|
|
|
|
|
135
|
|
|
$this->crudPanel->customOrderBy($this->crudPanel->columns()['name'], 'asc'); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertEquals(User::query()->toSql(), $this->crudPanel->query->toSql()); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function testItSkipsCustomOrderLogicIfOrderLogicIsNotCallable() |
|
141
|
|
|
{ |
|
142
|
|
|
$this->crudPanel->addColumn([ |
|
143
|
|
|
'name' => 'name', |
|
144
|
|
|
'type' => 'text', |
|
145
|
|
|
'orderLogic' => 'not callable', |
|
146
|
|
|
]); |
|
147
|
|
|
|
|
148
|
|
|
$this->crudPanel->customOrderBy($this->crudPanel->columns()['name'], 'asc'); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertEquals(User::query()->toSql(), $this->crudPanel->query->toSql()); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function testItCanApplyAOrderByWithPrefix() |
|
154
|
|
|
{ |
|
155
|
|
|
$this->crudPanel->orderByWithPrefix('name'); |
|
156
|
|
|
|
|
157
|
|
|
$this->assertEquals(User::query()->orderBy('name', 'asc')->toSql(), $this->crudPanel->query->toSql()); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function testItCanApplyAOrderByWithPrefixOnQueriesWithJoins() |
|
161
|
|
|
{ |
|
162
|
|
|
$this->crudPanel->query = User::query()->join('articles', 'articles.user_id', '=', 'users.id'); |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
$this->crudPanel->orderByWithPrefix('name'); |
|
165
|
|
|
|
|
166
|
|
|
$this->assertEquals(User::query()->join('articles', 'articles.user_id', '=', 'users.id')->orderBy('users.name', 'asc')->toSql(), $this->crudPanel->query->toSql()); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function testItDoesNotPerformQueryCountWhenItsDisabled() |
|
170
|
|
|
{ |
|
171
|
|
|
$this->crudPanel->setOperationSetting('showEntryCount', false); |
|
172
|
|
|
|
|
173
|
|
|
$this->assertEquals(0, $this->crudPanel->getTotalQueryCount()); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function testItGetTheQueryCountFromAPreviousCount() |
|
177
|
|
|
{ |
|
178
|
|
|
$this->crudPanel->setOperationSetting('showEntryCount', true); |
|
179
|
|
|
$this->crudPanel->setOperationSetting('totalEntryCount', 5); |
|
180
|
|
|
|
|
181
|
|
|
$this->assertEquals(5, $this->crudPanel->getTotalQueryCount()); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.