Skip to content

Commit 880c4b0

Browse files
committed
Added tests for all five existing observers
1 parent 7cd4bdd commit 880c4b0

5 files changed

Lines changed: 828 additions & 7 deletions

File tree

tests/Unit/BelongsToTest.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\NovaInlineRelationship\Tests\Unit;
4+
5+
use Laravel\Nova\Fields\Text;
6+
use Laravel\Nova\Http\Requests\NovaRequest;
7+
use Illuminate\Foundation\Testing\WithFaker;
8+
use Illuminate\Foundation\Testing\RefreshDatabase;
9+
use KirschbaumDevelopment\NovaInlineRelationship\Tests\User;
10+
use KirschbaumDevelopment\NovaInlineRelationship\Tests\TestCase;
11+
use KirschbaumDevelopment\NovaInlineRelationship\Tests\Department;
12+
use KirschbaumDevelopment\NovaInlineRelationship\Tests\Resource\User as UserResource;
13+
14+
class BelongsToTest extends TestCase
15+
{
16+
use WithFaker, RefreshDatabase;
17+
18+
private $department;
19+
20+
/**
21+
* @var UserResource
22+
*/
23+
private $userResource;
24+
25+
private $userModel;
26+
27+
/**
28+
* @before
29+
*/
30+
public function setUp(): void
31+
{
32+
parent::setUp();
33+
$this->userModel = User::make(['name' => 'test']);
34+
Department::create(['title' => 'Employee Department'])->users()->save($this->userModel);
35+
36+
$this->userResource = new UserResource($this->userModel);
37+
$this->setResourceForModel(User::class, UserResource::class);
38+
}
39+
40+
public function testResolveWithRelationship()
41+
{
42+
$inlineField = $this->userResource->resolveFieldForAttribute(new NovaRequest(), 'department');
43+
$this->assertCount(1, $inlineField->value);
44+
45+
tap($inlineField->value->first(), function ($department) {
46+
$this->assertArrayHasKey('title', $department->all());
47+
tap($department->get('title'), function ($title) {
48+
$this->assertEquals(Text::class, $title['component']);
49+
$this->assertEquals('title', $title['attribute']);
50+
tap($title['meta'], function ($meta) {
51+
$this->assertEquals('text-field', $meta['component']);
52+
$this->assertEquals('Employee Department', $meta['value']);
53+
});
54+
});
55+
});
56+
}
57+
58+
public function testFillAttributeForCreate()
59+
{
60+
$request = [
61+
'name' => 'Test',
62+
'department' => [
63+
[
64+
'title' => '123123123',
65+
],
66+
],
67+
];
68+
69+
$this->userModel = new User();
70+
71+
$this->userResource->fill(new NovaRequest($request), $this->userModel);
72+
73+
$this->assertEmpty($this->userModel->department);
74+
75+
$this->userModel->save();
76+
77+
tap($this->userModel->fresh()->department, function ($department) {
78+
$this->assertNotEmpty($department);
79+
$this->assertEquals('123123123', $department->title);
80+
});
81+
}
82+
83+
public function testFillAttributeForUpdate()
84+
{
85+
$id = $this->userModel->fresh()->department->id;
86+
87+
$updateRequest = [
88+
'name' => 'Test 2',
89+
'department' => [
90+
[
91+
'title' => '456456456',
92+
],
93+
],
94+
];
95+
96+
$this->userResource->fillForUpdate(new NovaRequest($updateRequest), $this->userModel);
97+
98+
$this->userModel->save();
99+
100+
tap($this->userModel->fresh()->department, function ($department) use ($id) {
101+
$this->assertEquals('456456456', $department->title);
102+
$this->assertEquals($id, $department->id);
103+
});
104+
}
105+
106+
public function testFillAttributeWillNotDelete()
107+
{
108+
$updateRequest = [
109+
'name' => 'Test 2',
110+
'department' => [
111+
],
112+
];
113+
114+
$this->userResource->fillForUpdate(new NovaRequest($updateRequest), $this->userModel);
115+
116+
$this->assertNotEmpty($this->userModel->department);
117+
118+
$this->userModel->save();
119+
120+
$this->assertNotEmpty($this->userModel->fresh()->department);
121+
}
122+
}

tests/Unit/HasManyTest.php

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\NovaInlineRelationship\Tests\Unit;
4+
5+
use Laravel\Nova\Fields\Currency;
6+
use Laravel\Nova\Http\Requests\NovaRequest;
7+
use Illuminate\Foundation\Testing\WithFaker;
8+
use Illuminate\Foundation\Testing\RefreshDatabase;
9+
use KirschbaumDevelopment\NovaInlineRelationship\Tests\Bill;
10+
use KirschbaumDevelopment\NovaInlineRelationship\Tests\Employee;
11+
use KirschbaumDevelopment\NovaInlineRelationship\Tests\TestCase;
12+
use KirschbaumDevelopment\NovaInlineRelationship\Tests\Resource\EmployeeHasMany;
13+
14+
class HasManyTest extends TestCase
15+
{
16+
use WithFaker, RefreshDatabase;
17+
18+
/**
19+
* @before
20+
*/
21+
public function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->employeeResource = new EmployeeHasMany($this->employeeModel);
26+
$this->setResourceForModel(Employee::class, EmployeeHasMany::class);
27+
}
28+
29+
public function testResolveEmpty()
30+
{
31+
$inlineField = $this->employeeResource->resolveFieldForAttribute(new NovaRequest(), 'bills');
32+
33+
$this->assertEmpty($inlineField->value);
34+
}
35+
36+
public function testResolveWithRelationship()
37+
{
38+
$this->employeeModel->bills()->save(Bill::make(['amount' => '100']));
39+
$this->employeeModel->bills()->save(Bill::make(['amount' => '200']));
40+
41+
$inlineField = $this->employeeResource->resolveFieldForAttribute(new NovaRequest(), 'bills');
42+
43+
$this->assertCount(2, $inlineField->value);
44+
45+
$inlineField->value->each(function ($bill) {
46+
$this->assertArrayHasKey('amount', $bill->all());
47+
tap($bill->get('amount'), function ($phone) {
48+
$this->assertEquals(Currency::class, $phone['component']);
49+
$this->assertEquals('amount', $phone['attribute']);
50+
$this->assertEquals('number', $phone['options']['type']);
51+
tap($phone['meta'], function ($meta) {
52+
$this->assertEquals('text-field', $meta['component']);
53+
});
54+
});
55+
});
56+
}
57+
58+
public function testFillAttributeForCreate()
59+
{
60+
$request = [
61+
'name' => 'Test',
62+
'bills' => [
63+
[
64+
'amount' => '100',
65+
],
66+
],
67+
];
68+
69+
$newEmployee = new Employee();
70+
$this->employeeResource->fill(new NovaRequest($request), $newEmployee);
71+
72+
$this->assertEmpty($newEmployee->bills);
73+
74+
$newEmployee->save();
75+
76+
tap($newEmployee->fresh()->bills, function ($bills) {
77+
$this->assertCount(1, $bills);
78+
$this->assertEquals('100', $bills->first()->amount);
79+
});
80+
}
81+
82+
public function testFillAttributeForCreateMany()
83+
{
84+
$request = [
85+
'name' => 'New Test',
86+
'bills' => [
87+
[
88+
'amount' => '100',
89+
],
90+
[
91+
'amount' => '200',
92+
],
93+
],
94+
];
95+
96+
$newEmployee = new Employee();
97+
$this->employeeResource->fill(new NovaRequest($request), $newEmployee);
98+
99+
$this->assertEmpty($newEmployee->bills);
100+
101+
$newEmployee->save();
102+
103+
tap($newEmployee->fresh()->bills, function ($bills) {
104+
$this->assertCount(2, $bills);
105+
$this->assertEquals('100', $bills->first()->amount);
106+
$this->assertEquals('200', $bills->last()->amount);
107+
});
108+
}
109+
110+
public function testFillAttributeForUpdate()
111+
{
112+
$newEmployee = Employee::create(['name' => 'test']);
113+
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
114+
115+
$request = [
116+
'name' => 'Test',
117+
'bills' => [
118+
[
119+
'amount' => '200',
120+
],
121+
],
122+
];
123+
124+
$this->employeeResource->fillForUpdate(new NovaRequest($request), $newEmployee);
125+
126+
$newEmployee->save();
127+
128+
tap($newEmployee->fresh()->bills, function ($bills) {
129+
$this->assertCount(1, $bills);
130+
$this->assertEquals('200', $bills->first()->amount);
131+
});
132+
}
133+
134+
public function testFillAttributeForUpdateMany()
135+
{
136+
$newEmployee = Employee::create(['name' => 'test']);
137+
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
138+
$newEmployee->bills()->save(Bill::make(['amount' => '200']));
139+
140+
$request = [
141+
'name' => 'Test',
142+
'bills' => [
143+
[
144+
'amount' => '300',
145+
],
146+
[
147+
'amount' => '400',
148+
],
149+
],
150+
];
151+
152+
$this->employeeResource->fillForUpdate(new NovaRequest($request), $newEmployee);
153+
154+
$newEmployee->save();
155+
156+
tap($newEmployee->fresh()->bills, function ($bills) {
157+
$this->assertCount(2, $bills);
158+
$this->assertEquals('300', $bills->first()->amount);
159+
$this->assertEquals('400', $bills->last()->amount);
160+
});
161+
}
162+
163+
public function testFillAttributeForUpdateReverse()
164+
{
165+
$newEmployee = Employee::create(['name' => 'test']);
166+
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
167+
$newEmployee->bills()->save(Bill::make(['amount' => '200']));
168+
169+
$request = [
170+
'name' => 'Test',
171+
'bills' => [
172+
[
173+
'amount' => '200',
174+
],
175+
[
176+
'amount' => '100',
177+
],
178+
],
179+
];
180+
181+
$this->employeeResource->fillForUpdate(new NovaRequest($request), $newEmployee);
182+
183+
$newEmployee->save();
184+
185+
tap($newEmployee->fresh()->bills, function ($bills) {
186+
$this->assertCount(2, $bills);
187+
$this->assertEquals('200', $bills->first()->amount );
188+
$this->assertEquals('100', $bills->last()->amount );
189+
});
190+
}
191+
192+
public function testFillAttributeForAddByUpdate()
193+
{
194+
$newEmployee = Employee::create(['name' => 'test']);
195+
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
196+
197+
$request = [
198+
'name' => 'Test',
199+
'bills' => [
200+
[
201+
'amount' => '300',
202+
],
203+
[
204+
'amount' => '400',
205+
],
206+
],
207+
];
208+
209+
$this->employeeResource->fillForUpdate(new NovaRequest($request), $newEmployee);
210+
211+
$this->assertCount(1, $newEmployee->bills);
212+
213+
$newEmployee->save();
214+
215+
tap($newEmployee->fresh()->bills, function ($bills) {
216+
$this->assertCount(2, $bills);
217+
$this->assertEquals('300', $bills->first()->amount );
218+
$this->assertEquals('400', $bills->last()->amount);
219+
});
220+
}
221+
222+
public function testFillAttributeForDeleteByUpdate()
223+
{
224+
$newEmployee = Employee::create(['name' => 'test']);
225+
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
226+
$newEmployee->bills()->save(Bill::make(['amount' => '200']));
227+
228+
$request = [
229+
'name' => 'Test',
230+
'bills' => [
231+
[
232+
'amount' => '300',
233+
],
234+
],
235+
];
236+
237+
$this->employeeResource->fillForUpdate(new NovaRequest($request), $newEmployee);
238+
239+
$this->assertCount(2, $newEmployee->bills);
240+
241+
$newEmployee->save();
242+
243+
tap($newEmployee->fresh()->bills, function ($bills) {
244+
$this->assertCount(1, $bills);
245+
$this->assertEquals('300', $bills->first()->amount );
246+
});
247+
}
248+
249+
public function testFillAttributeForDeleteOnlyItemByUpdate()
250+
{
251+
$newEmployee = Employee::create(['name' => 'test']);
252+
$newEmployee->bills()->save(Bill::make(['amount' => '100']));
253+
254+
$request = [
255+
'name' => 'Test',
256+
'bills' => [
257+
],
258+
];
259+
260+
$this->employeeResource->fillForUpdate(new NovaRequest($request), $newEmployee);
261+
262+
$this->assertCount(1, $newEmployee->bills);
263+
264+
$newEmployee->save();
265+
266+
tap($newEmployee->fresh()->bills, function ($bills) {
267+
$this->assertEmpty($bills);
268+
});
269+
}
270+
}

0 commit comments

Comments
 (0)