-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSelectTest.php
executable file
·242 lines (196 loc) · 5.96 KB
/
SelectTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
use Faker\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Schema;
use LaravelEnso\Select\Traits\OptionsBuilder;
use Tests\TestCase;
class SelectTest extends TestCase
{
use OptionsBuilder;
private $testModel;
private $faker;
protected $queryAttributes = ['email', 'relation.name'];
protected $resource = null;
protected $appends = null;
public function setUp(): void
{
parent::setUp();
$this->faker = Factory::create();
$this->createTestModelTable();
$this->createRelationTable();
$this->testModel = $this->createTestModel();
$this->createRelation();
}
/** @test */
public function can_get_options_without_filters()
{
$response = $this->requestResponse();
$this->assertCount(SelectTestModel::count(), $response);
$this->assertTrue($this->whithinResponse($response));
}
/** @test */
public function can_get_empty_options_with_restricting_query()
{
$response = $this->requestResponse(['query' => 'NO_VALUE']);
$this->assertCount(0, $response);
}
/** @test */
public function can_get_empty_options_with_restricting_params()
{
$response = $this->requestResponse(['params' => ['id' => 0]]);
$this->assertCount(0, $response);
}
/** @test */
public function can_get_empty_options_with_restricting_pivot_params()
{
$response = $this->requestResponse([
'pivotParams' => ['relation' => ['id' => 0]],
]);
$this->assertCount(0, $response);
}
/** @test */
public function can_get_selected_options_with_restricting_filter()
{
$response = $this->requestResponse([
'value' => $this->testModel->id,
'query' => 'NO_VALUE',
]);
$this->assertCount(1, $response);
$this->assertTrue($this->whithinResponse($response));
}
/** @test */
public function can_get_filtered_options()
{
$response = $this->requestResponse([
'query' => $this->testModel->email,
]);
$this->assertTrue($this->whithinResponse($response));
}
/** @test */
public function can_get_filtered_on_nested_attrs_options()
{
$response = $this->requestResponse([
'query' => $this->testModel->relation->name,
]);
$this->assertTrue($this->whithinResponse($response));
}
/** @test */
public function can_get_options_with_param()
{
$response = $this->requestResponse([
'params' => ['email' => $this->testModel->email],
]);
$this->assertTrue($this->whithinResponse($response));
}
/** @test */
public function can_get_options_with_pivot_params()
{
$response = $this->requestResponse([
'pivotParams' => ['relation' => ['name' => $this->testModel->relation->name]],
]);
$this->assertTrue($this->whithinResponse($response));
}
/** @test */
public function can_paginate()
{
$paginate = 0;
$response = $this->requestResponse(['paginate' => $paginate]);
$this->assertCount($paginate, $response);
}
/** @test */
public function can_use_resource()
{
$this->resource = SelectTestResource::class;
$response = $this->requestResponse();
$this->assertCount(SelectTestModel::count(), $response);
$this->assertTrue(
$response->first()->resolve()['resource'] === 'resource'
);
}
/** @test */
public function can_use_accessors()
{
$this->appends = ['custom'];
$response = $this->requestResponse();
$this->assertCount(SelectTestModel::count(), $response);
$this->assertTrue(
$response->first()->toArray()['custom'] === $this->testModel->custom
);
}
private function whithinResponse($response)
{
return $response->pluck('email')
->contains($this->testModel->email);
}
private function requestResponse(array $params = [])
{
$request = new Request();
Collection::wrap($params)->each(fn ($value, $key) => $request->merge([
$key => is_array($value) ? json_encode($value) : $value,
]));
return new Collection(
$this->__invoke($request)->toResponse($request)
);
}
public function query()
{
return SelectTestModel::query();
}
private function createTestModel()
{
return SelectTestModel::create([
'email' => $this->faker->email,
]);
}
private function createRelation()
{
return SelectRelation::create([
'name' => $this->faker->name,
'parent_id' => $this->testModel->id,
]);
}
private function createTestModelTable()
{
Schema::create('select_test_models', function ($table) {
$table->increments('id');
$table->string('email');
$table->timestamps();
});
}
private function createRelationTable()
{
Schema::create('select_relations', function ($table) {
$table->increments('id');
$table->integer('parent_id');
$table->foreign('parent_id')->references('id')->on('select_test_models');
$table->string('name');
$table->timestamps();
});
}
}
class SelectTestModel extends Model
{
protected $fillable = ['email'];
public function relation()
{
return $this->hasOne(SelectRelation::class, 'parent_id');
}
public function getCustomAttribute()
{
return 'custom';
}
}
class SelectRelation extends Model
{
protected $fillable = ['name', 'parent_id'];
}
class SelectTestResource extends JsonResource
{
public function toArray($request)
{
return ['resource' => 'resource'];
}
}