Skip to content

Commit 91dca6d

Browse files
committed
test: implement more tests
1 parent d3f48da commit 91dca6d

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

config/jsonapi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
*/
4242
'precision' => null,
4343

44-
'when-has' => false,
4544
/*
4645
|--------------------------------------------------------------------------
4746
| When Has
@@ -68,6 +67,7 @@
6867
| ->whenHas('last_name')
6968
| ->whenHas('first_name'),
7069
*/
70+
'when-has' => false,
7171
],
7272

7373
'relationship' => [

tests/Feature/FeatureTestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ abstract class FeatureTestCase extends TestCase
1313
{
1414
use RefreshDatabase;
1515

16+
protected function defineEnvironment($app)
17+
{
18+
$app['config']['jsonapi'] = include __DIR__ . '/../app/jsonapi.php';
19+
}
20+
1621
protected function defineRoutes($router)
1722
{
1823
include __DIR__ . '/../app/routes.php';

tests/Unit/Resources/Concerns/ConditionallyLoadsAttributesTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Ark4ne\JsonApi\Descriptors\Values\ValueMixed;
88
use Ark4ne\JsonApi\Resources\Concerns\ConditionallyLoadsAttributes;
99
use Ark4ne\JsonApi\Resources\Relationship;
10+
use Illuminate\Database\Eloquent\Model;
1011
use Illuminate\Http\Request;
1112
use Illuminate\Http\Resources\Json\JsonResource;
1213
use Illuminate\Http\Resources\MergeValue;
@@ -114,4 +115,26 @@ public function testApplyWhen()
114115
'missing.5' => RelationMissing::fromRelationship((new Relationship(UserResource::class, fn() => null))),
115116
]), $actual);
116117
}
118+
119+
public function testWhenHas()
120+
{
121+
$resource = new class(['a' => 1]) extends JsonResource {
122+
use ConditionallyLoadsAttributes;
123+
};
124+
$this->assertEquals(1, Reflect::invoke($resource, 'whenHas', 'a'));
125+
$this->assertEquals('abc', Reflect::invoke($resource, 'whenHas', 'a', 'abc'));
126+
$this->assertEquals(new MissingValue, Reflect::invoke($resource, 'whenHas', 'b'));
127+
$this->assertEquals(new MissingValue, Reflect::invoke($resource, 'whenHas', 'b', 'missing'));
128+
$this->assertEquals('missing', Reflect::invoke($resource, 'whenHas', 'b', 'abc', 'missing'));
129+
130+
$resource = new class((object)['a' => 1]) extends JsonResource {
131+
use ConditionallyLoadsAttributes;
132+
};
133+
134+
$this->assertEquals(1, Reflect::invoke($resource, 'whenHas', 'a'));
135+
$this->assertEquals('abc', Reflect::invoke($resource, 'whenHas', 'a', 'abc'));
136+
$this->assertEquals(new MissingValue, Reflect::invoke($resource, 'whenHas', 'b'));
137+
$this->assertEquals(new MissingValue, Reflect::invoke($resource, 'whenHas', 'b', 'abc'));
138+
$this->assertEquals('missing', Reflect::invoke($resource, 'whenHas', 'b', 'abc', 'missing'));
139+
}
117140
}

tests/Unit/Support/ValuesTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,44 @@ public function isMissing()
106106

107107
$this->assertEquals(true, Values::isMissing(new JsonResource(new MissingValue())));
108108
}
109+
110+
111+
/**
112+
* Return a list of data to test hasAttribute method
113+
*/
114+
public function dataAttribute()
115+
{
116+
return [
117+
'array' => [['a' => 1], 'a', true, 1],
118+
'array.empty' => [[], 'a', false, null],
119+
'array.fail' => [['a' => 1], 'b', false, null],
120+
'object' => [(object)['a' => 1], 'a', true, 1],
121+
'object.empty' => [(object)[], 'a', false, null],
122+
'object.fail' => [(object)['a' => 1], 'b', false, null],
123+
'model' => [new class(['a' => 1]) extends \Illuminate\Database\Eloquent\Model {
124+
protected $fillable = ['a'];
125+
}, 'a', true, 1],
126+
'model.empty' => [new class() extends \Illuminate\Database\Eloquent\Model {
127+
}, 'a', false, null],
128+
'model.fail' => [new class(['a' => 1]) extends \Illuminate\Database\Eloquent\Model {
129+
protected $fillable = ['a'];
130+
}, 'b', false, null],
131+
];
132+
}
133+
134+
/**
135+
* @dataProvider dataAttribute
136+
*/
137+
public function testHasAttribute($data, $attribute, $expected)
138+
{
139+
$this->assertEquals($expected, Values::hasAttribute($data, $attribute));
140+
}
141+
142+
/**
143+
* @dataProvider dataAttribute
144+
*/
145+
public function testGetAttribute($data, $attribute, $has, $expected)
146+
{
147+
$this->assertEquals($expected, Values::getAttribute($data, $attribute));
148+
}
109149
}

tests/app/jsonapi.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$config = include __DIR__ . '/../../config/jsonapi.php';
4+
5+
$config['describer']['when-has'] = true;
6+
7+
return $config;

0 commit comments

Comments
 (0)