Skip to content

Commit 1caa985

Browse files
committed
feat: introduce tests to cover the new jsonapi functionality
1 parent cc074ba commit 1caa985

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

tests/FieldsTest.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@
8383
expect($query)->toEqual($expected);
8484
});
8585

86+
it('can fetch specific string columns jsonApi Format', function () {
87+
config(['query-builder.convert_field_names_to_snake_case' => true]);
88+
config(['query-builder.convert_relation_table_name_strategy' => 'camelCase']);
89+
90+
$query = createQueryFromFieldRequest('firstName,id')
91+
->allowedFields(['firstName', 'id'])
92+
->toSql();
93+
94+
$expected = TestModel::query()
95+
->select("{$this->modelTableName}.first_name", "{$this->modelTableName}.id")
96+
->toSql();
97+
98+
expect($query)->toEqual($expected);
99+
});
100+
86101
it('wont fetch a specific array column if its not allowed', function () {
87102
$query = createQueryFromFieldRequest(['test_models' => 'random-column'])->toSql();
88103

@@ -200,6 +215,81 @@
200215
$this->assertQueryLogContains('select `name` from `related_models`');
201216
});
202217

218+
it('can fetch only requested string columns from an included model jsonApi format', function () {
219+
config(['query-builder.convert_relation_table_name_strategy' => 'camelCase']);
220+
RelatedModel::create([
221+
'test_model_id' => $this->model->id,
222+
'name' => 'related',
223+
]);
224+
225+
$request = new Request([
226+
'fields' => 'id,relatedModels.name',
227+
'include' => ['relatedModels'],
228+
]);
229+
230+
$queryBuilder = QueryBuilder::for(TestModel::class, $request)
231+
->allowedFields('relatedModels.name', 'id')
232+
->allowedIncludes('relatedModels');
233+
234+
DB::enableQueryLog();
235+
236+
$queryBuilder->first()->relatedModels;
237+
238+
$this->assertQueryLogContains('select `test_models`.`id` from `test_models`');
239+
$this->assertQueryLogContains('select `name` from `related_models`');
240+
});
241+
242+
it('can fetch only requested string columns from an included model jsonApi format with field conversion', function () {
243+
config(['query-builder.convert_field_names_to_snake_case' => true]);
244+
config(['query-builder.convert_relation_table_name_strategy' => 'camelCase']);
245+
246+
RelatedModel::create([
247+
'test_model_id' => $this->model->id,
248+
'name' => 'related',
249+
]);
250+
251+
$request = new Request([
252+
'fields' => 'id,relatedModels.fullName',
253+
'include' => ['relatedModels'],
254+
]);
255+
256+
$queryBuilder = QueryBuilder::for(TestModel::class, $request)
257+
->allowedFields('relatedModels.fullName', 'id')
258+
->allowedIncludes('relatedModels');
259+
260+
DB::enableQueryLog();
261+
262+
$queryBuilder->first()->relatedModels;
263+
264+
$this->assertQueryLogContains('select `test_models`.`id` from `test_models`');
265+
$this->assertQueryLogContains('select `full_name` from `related_models`');
266+
});
267+
268+
it('can fetch only requested string columns from an included model through pivot jsonApi format', function () {
269+
config(['query-builder.convert_relation_table_name_strategy' => 'camelCase']);
270+
271+
$this->model->relatedThroughPivotModels()->create([
272+
'id' => $this->model->id + 1,
273+
'name' => 'Test',
274+
]);
275+
276+
$request = new Request([
277+
'fields' => 'id,relatedThroughPivotModels.name',
278+
'include' => ['relatedThroughPivotModels'],
279+
]);
280+
281+
$queryBuilder = QueryBuilder::for(TestModel::class, $request)
282+
->allowedFields('relatedThroughPivotModels.name', 'id')
283+
->allowedIncludes('relatedThroughPivotModels');
284+
285+
DB::enableQueryLog();
286+
287+
$queryBuilder->first()->relatedThroughPivotModels;
288+
289+
$this->assertQueryLogContains('select `test_models`.`id` from `test_models`');
290+
$this->assertQueryLogContains('select `name`, `pivot_models`.`test_model_id` as `pivot_test_model_id`, `pivot_models`.`related_through_pivot_model_id` as `pivot_related_through_pivot_model_id` from `related_through_pivot_models`');
291+
});
292+
203293
it('can fetch requested array columns from included models up to two levels deep', function () {
204294
RelatedModel::create([
205295
'test_model_id' => $this->model->id,
@@ -224,6 +314,36 @@
224314
expect($result->relatedModels->first()->testModel->toArray())->toEqual(['id' => $this->model->id]);
225315
});
226316

317+
it('can fetch requested array columns from included models up to two levels deep jsonApi mapper', function () {
318+
config(['query-builder.convert_field_names_to_snake_case' => true]);
319+
config(['query-builder.convert_relation_table_name_strategy' => 'camelCase']);
320+
321+
$relatedModel = RelatedModel::create([
322+
'test_model_id' => $this->model->id,
323+
'name' => 'related',
324+
]);
325+
326+
$relatedModel->nestedRelatedModels()->create([
327+
'name' => 'nested related',
328+
]);
329+
330+
$request = new Request([
331+
'fields' => 'id,name,relatedModels.id,relatedModels.name,nestedRelatedModels.id,nestedRelatedModels.name',
332+
'include' => ['nestedRelatedModels', 'relatedModels'],
333+
]);
334+
335+
336+
$queryBuilder = QueryBuilder::for(TestModel::class, $request)
337+
->allowedFields('id', 'name', 'relatedModels.id', 'relatedModels.name', 'nestedRelatedModels.id', 'nestedRelatedModels.name')
338+
->allowedIncludes('relatedModels', 'nestedRelatedModels');
339+
340+
DB::enableQueryLog();
341+
$queryBuilder->first();
342+
343+
$this->assertQueryLogContains('select `test_models`.`id`, `test_models`.`name` from `test_models`');
344+
$this->assertQueryLogContains('select `nested_related_models`.`id`, `nested_related_models`.`name`, `related_models`.`test_model_id` as `laravel_through_key` from `nested_related_models`');
345+
});
346+
227347
it('can fetch requested string columns from included models up to two levels deep', function () {
228348
RelatedModel::create([
229349
'test_model_id' => $this->model->id,

tests/TestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected function setUpDatabase(Application $app)
3737
$table->increments('id');
3838
$table->timestamps();
3939
$table->string('name')->nullable();
40+
$table->string('full_name')->nullable();
4041
$table->double('salary')->nullable();
4142
$table->boolean('is_visible')->default(true);
4243
});
@@ -62,6 +63,7 @@ protected function setUpDatabase(Application $app)
6263
$table->increments('id');
6364
$table->integer('test_model_id');
6465
$table->string('name');
66+
$table->string('full_name')->nullable();
6567
});
6668

6769
$app['db']->connection()->getSchemaBuilder()->create('nested_related_models', function (Blueprint $table) {
@@ -92,7 +94,7 @@ protected function setUpDatabase(Application $app)
9294
protected function getPackageProviders($app)
9395
{
9496
return [
95-
RayServiceProvider::class,
97+
// RayServiceProvider::class,
9698
QueryBuilderServiceProvider::class,
9799
];
98100
}

tests/TestClasses/Models/TestModel.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Relations\BelongsTo;
99
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1010
use Illuminate\Database\Eloquent\Relations\HasMany;
11+
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
1112
use Illuminate\Database\Eloquent\Relations\MorphMany;
1213
use Illuminate\Support\Carbon;
1314

@@ -27,6 +28,18 @@ public function relatedModel(): BelongsTo
2728
return $this->belongsTo(RelatedModel::class);
2829
}
2930

31+
public function nestedRelatedModels(): HasManyThrough
32+
{
33+
return $this->hasManyThrough(
34+
NestedRelatedModel::class, // Target model
35+
RelatedModel::class, // Intermediate model
36+
'test_model_id', // Foreign key on RelatedModel
37+
'related_model_id', // Foreign key on NestedRelatedModel
38+
'id', // Local key on TestModel
39+
'id' // Local key on RelatedModel
40+
);
41+
}
42+
3043
public function otherRelatedModels(): HasMany
3144
{
3245
return $this->hasMany(RelatedModel::class);

0 commit comments

Comments
 (0)