|
| 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