Skip to content

Commit 8b69116

Browse files
committed
Updated and added new tests
1 parent 39ebb1c commit 8b69116

5 files changed

Lines changed: 282 additions & 130 deletions

File tree

tests/Unit/BelongsToTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ public function testResolveWithRelationship()
5959
public function testFillAttributeForCreate()
6060
{
6161
$request = [
62-
'values' => [
63-
'name' => 'Test',
64-
'department' => [
65-
[
62+
'name' => 'Test',
63+
'department' => [
64+
[
65+
'values' => [
6666
'title' => '123123123',
6767
],
68+
'modelId' => 0,
6869
],
6970
],
7071
];
@@ -88,12 +89,13 @@ public function testFillAttributeForUpdate()
8889
$id = $this->userModel->fresh()->department->id;
8990

9091
$updateRequest = [
91-
'values' => [
92-
'name' => 'Test 2',
93-
'department' => [
94-
[
92+
'name' => 'Test 2',
93+
'department' => [
94+
[
95+
'values' => [
9596
'title' => '456456456',
9697
],
98+
'modelId' => $id,
9799
],
98100
],
99101
];
@@ -111,10 +113,8 @@ public function testFillAttributeForUpdate()
111113
public function testFillAttributeWillNotDelete()
112114
{
113115
$updateRequest = [
114-
'values' => [
115-
'name' => 'Test 2',
116-
'department' => [
117-
],
116+
'name' => 'Test 2',
117+
'department' => [
118118
],
119119
];
120120

tests/Unit/HasManyTest.php

Lines changed: 127 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public function testResolveWithRelationship()
4545

4646
$inlineField->value->each(function ($bill) {
4747
$this->assertArrayHasKey('amount', $bill->all());
48-
tap($bill->get('amount'), function ($phone) {
49-
$this->assertEquals(Currency::class, $phone['component']);
50-
$this->assertEquals('amount', $phone['attribute']);
51-
$this->assertEquals('number', $phone['options']['type']);
52-
tap($phone['meta'], function ($meta) {
53-
$this->assertEquals('text-field', $meta['component']);
48+
tap($bill->get('amount'), function ($amount) {
49+
$this->assertEquals(Currency::class, $amount['component']);
50+
$this->assertEquals('amount', $amount['attribute']);
51+
$this->assertEquals('number', $amount['options']['type']);
52+
tap($amount['meta'], function ($meta) {
53+
$this->assertEquals('currency-field', $meta['component']);
5454
});
5555
});
5656
});
@@ -59,12 +59,13 @@ public function testResolveWithRelationship()
5959
public function testFillAttributeForCreate()
6060
{
6161
$request = [
62-
'values' => [
63-
'name' => 'Test',
64-
'bills' => [
65-
[
62+
'name' => 'Test',
63+
'bills' => [
64+
[
65+
'values' => [
6666
'amount' => '100',
6767
],
68+
'modelId' => 0,
6869
],
6970
],
7071
];
@@ -85,15 +86,19 @@ public function testFillAttributeForCreate()
8586
public function testFillAttributeForCreateMany()
8687
{
8788
$request = [
88-
'values' => [
89-
'name' => 'New Test',
90-
'bills' => [
91-
[
89+
'name' => 'New Test',
90+
'bills' => [
91+
[
92+
'values' => [
9293
'amount' => '100',
9394
],
94-
[
95+
'modelId' => 0,
96+
],
97+
[
98+
'values' => [
9599
'amount' => '200',
96100
],
101+
'modelId' => 0,
97102
],
98103
],
99104
];
@@ -117,13 +122,16 @@ public function testFillAttributeForUpdate()
117122
$newEmployee = Employee::create(['name' => 'test']);
118123
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
119124

125+
$id = $newEmployee->fresh()->bills->first()->id;
126+
120127
$request = [
121-
'values' => [
122-
'name' => 'Test',
123-
'bills' => [
124-
[
128+
'name' => 'Test',
129+
'bills' => [
130+
[
131+
'values' => [
125132
'amount' => '200',
126133
],
134+
'modelId' => $id,
127135
],
128136
],
129137
];
@@ -144,16 +152,23 @@ public function testFillAttributeForUpdateMany()
144152
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
145153
$newEmployee->bills()->save(Bill::make(['amount' => '200']));
146154

155+
$bill1 = $newEmployee->fresh()->bills->first();
156+
$bill2 = $newEmployee->fresh()->bills->reverse()->first();
157+
147158
$request = [
148-
'values' => [
149-
'name' => 'Test',
150-
'bills' => [
151-
[
159+
'name' => 'Test',
160+
'bills' => [
161+
[
162+
'values' => [
152163
'amount' => '300',
153164
],
154-
[
165+
'modelId' => $bill1->id,
166+
],
167+
[
168+
'values' => [
155169
'amount' => '400',
156170
],
171+
'modelId' => $bill2->id,
157172
],
158173
],
159174
];
@@ -162,10 +177,10 @@ public function testFillAttributeForUpdateMany()
162177

163178
$newEmployee->save();
164179

165-
tap($newEmployee->fresh()->bills, function ($bills) {
180+
tap($newEmployee->fresh()->bills, function ($bills) use ($bill1, $bill2) {
166181
$this->assertCount(2, $bills);
167-
$this->assertEquals('300', $bills->first()->amount);
168-
$this->assertEquals('400', $bills->last()->amount);
182+
$this->assertEquals('300', $bill1->fresh()->amount);
183+
$this->assertEquals('400', $bill2->fresh()->amount);
169184
});
170185
}
171186

@@ -175,16 +190,23 @@ public function testFillAttributeForUpdateReverse()
175190
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
176191
$newEmployee->bills()->save(Bill::make(['amount' => '200']));
177192

193+
$bill1 = $newEmployee->fresh()->bills->first();
194+
$bill2 = $newEmployee->fresh()->bills->reverse()->first();
195+
178196
$request = [
179-
'values' => [
180-
'name' => 'Test',
181-
'bills' => [
182-
[
197+
'name' => 'Test',
198+
'bills' => [
199+
[
200+
'values' => [
183201
'amount' => '200',
184202
],
185-
[
203+
'modelId' => $bill1->id,
204+
],
205+
[
206+
'values' => [
186207
'amount' => '100',
187208
],
209+
'modelId' => $bill2->id,
188210
],
189211
],
190212
];
@@ -193,10 +215,58 @@ public function testFillAttributeForUpdateReverse()
193215

194216
$newEmployee->save();
195217

196-
tap($newEmployee->fresh()->bills, function ($bills) {
218+
tap($newEmployee->fresh()->bills, function ($bills) use ($bill1, $bill2) {
197219
$this->assertCount(2, $bills);
198-
$this->assertEquals('200', $bills->first()->amount);
199-
$this->assertEquals('100', $bills->last()->amount);
220+
tap($bill1->fresh(), function ($bill) {
221+
$this->assertEquals('200', $bill->amount);
222+
});
223+
tap($bill2->fresh(), function ($bill) {
224+
$this->assertEquals('100', $bill->amount);
225+
});
226+
});
227+
}
228+
229+
public function testFillAttributeForUpdateByOrder()
230+
{
231+
$newEmployee = Employee::create(['name' => 'test']);
232+
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
233+
$newEmployee->bills()->save(Bill::make(['amount' => '200']));
234+
235+
$bill1 = $newEmployee->fresh()->bills->first();
236+
$bill2 = $newEmployee->fresh()->bills->reverse()->first();
237+
238+
$request = [
239+
'name' => 'Test',
240+
'bills' => [
241+
[
242+
'values' => [
243+
'amount' => '200',
244+
],
245+
'modelId' => $bill2->id,
246+
],
247+
[
248+
'values' => [
249+
'amount' => '100',
250+
],
251+
'modelId' => $bill1->id,
252+
],
253+
],
254+
];
255+
256+
$this->employeeResource->fillForUpdate(new NovaRequest($request), $newEmployee);
257+
258+
$newEmployee->save();
259+
260+
tap($newEmployee->fresh()->bills, function ($bills) use ($bill1, $bill2) {
261+
$this->assertCount(2, $bills);
262+
tap($bill1->fresh(), function ($bill) {
263+
$this->assertEquals('100', $bill->amount);
264+
$this->assertEquals(1, $bill->weight);
265+
});
266+
tap($bill2->fresh(), function ($bill) {
267+
$this->assertEquals('200', $bill->amount);
268+
$this->assertEquals(0, $bill->weight);
269+
});
200270
});
201271
}
202272

@@ -205,16 +275,22 @@ public function testFillAttributeForAddByUpdate()
205275
$newEmployee = Employee::create(['name' => 'test']);
206276
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
207277

278+
$bill = $newEmployee->fresh()->bills->first();
279+
208280
$request = [
209-
'values' => [
210-
'name' => 'Test',
211-
'bills' => [
212-
[
281+
'name' => 'Test',
282+
'bills' => [
283+
[
284+
'values' => [
213285
'amount' => '300',
214286
],
215-
[
287+
'modelId' => $bill->id,
288+
],
289+
[
290+
'values' => [
216291
'amount' => '400',
217292
],
293+
'modelId' => 0,
218294
],
219295
],
220296
];
@@ -238,13 +314,16 @@ public function testFillAttributeForDeleteByUpdate()
238314
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
239315
$newEmployee->bills()->save(Bill::make(['amount' => '200']));
240316

317+
$bill = $newEmployee->fresh()->bills->first();
318+
241319
$request = [
242-
'values' => [
243-
'name' => 'Test',
244-
'bills' => [
245-
[
320+
'name' => 'Test',
321+
'bills' => [
322+
[
323+
'values' => [
246324
'amount' => '300',
247325
],
326+
'modelId' => $bill->id,
248327
],
249328
],
250329
];
@@ -255,9 +334,9 @@ public function testFillAttributeForDeleteByUpdate()
255334

256335
$newEmployee->save();
257336

258-
tap($newEmployee->fresh()->bills, function ($bills) {
337+
tap($newEmployee->fresh()->bills, function ($bills) use ($bill) {
259338
$this->assertCount(1, $bills);
260-
$this->assertEquals('300', $bills->first()->amount);
339+
$this->assertEquals('300', $bill->fresh()->amount);
261340
});
262341
}
263342

@@ -267,10 +346,8 @@ public function testFillAttributeForDeleteOnlyItemByUpdate()
267346
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
268347

269348
$request = [
270-
'values' => [
271-
'name' => 'Test',
272-
'bills' => [
273-
],
349+
'name' => 'Test',
350+
'bills' => [
274351
],
275352
];
276353

tests/Unit/HasOneTest.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ public function testResolveWithRelationship()
5858
public function testFillAttributeForCreate()
5959
{
6060
$request = [
61-
'values' => [
62-
'name' => 'Test',
63-
'profile' => [
64-
[
61+
'name' => 'Test',
62+
'profile' => [
63+
[
64+
'values' => [
6565
'phone' => '123123123',
6666
],
67+
'modelId' => 0,
6768
],
6869
],
6970
];
@@ -90,12 +91,13 @@ public function testFillAttributeForUpdate()
9091
$id = $newEmployee->fresh()->profile->id;
9192

9293
$updateRequest = [
93-
'values' => [
94-
'name' => 'Test 2',
95-
'profile' => [
96-
[
94+
'name' => 'Test 2',
95+
'profile' => [
96+
[
97+
'values' => [
9798
'phone' => '456456456',
9899
],
100+
'modelId' => $id,
99101
],
100102
],
101103
];
@@ -115,11 +117,11 @@ public function testFillAttributeForDelete()
115117
$newEmployee = Employee::create(['name' => 'Test']);
116118
$newEmployee->profile()->save(Profile::make(['phone' => '123123123']));
117119

120+
$id = $newEmployee->fresh()->profile->id;
121+
118122
$updateRequest = [
119-
'values' => [
120-
'name' => 'Test 2',
121-
'profile' => [
122-
],
123+
'name' => 'Test 2',
124+
'profile' => [
123125
],
124126
];
125127

0 commit comments

Comments
 (0)