|
| 1 | +<?php |
| 2 | + |
| 3 | +/** @noinspection ReturnTypeCanBeDeclaredInspection */ |
| 4 | + |
| 5 | +namespace Galahad\Aire\Tests\Feature; |
| 6 | + |
| 7 | +use Galahad\Aire\Tests\TestCase; |
| 8 | +use Illuminate\Database\Eloquent\Model; |
| 9 | +use Illuminate\Support\Facades\Route; |
| 10 | +use Illuminate\Support\Facades\URL; |
| 11 | + |
| 12 | +class ResourcefulBindingTest extends TestCase |
| 13 | +{ |
| 14 | + protected function setUp() |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + |
| 18 | + Route::post('/users', function() {})->name('users.store'); |
| 19 | + Route::put('/users/{user}', function() {})->name('users.update'); |
| 20 | + } |
| 21 | + |
| 22 | + public function test_action_and_method_are_inferred_for_unsaved_models() |
| 23 | + { |
| 24 | + $model = new XResourcefulModel(['id' => 1]); |
| 25 | + |
| 26 | + $html = $this->aire()->form()->resourceful($model)->render(); |
| 27 | + |
| 28 | + $this->assertSelectorAttribute($html, 'form', 'action', URL::to('/users')); |
| 29 | + $this->assertSelectorAttribute($html, 'form', 'method', 'POST'); |
| 30 | + $this->assertSelectorDoesNotExist($html, 'input[name="_method"]'); |
| 31 | + } |
| 32 | + |
| 33 | + public function test_action_and_method_are_inferred_for_saved_models() |
| 34 | + { |
| 35 | + $model = new XResourcefulModel(['id' => 1]); |
| 36 | + $model->exists = true; |
| 37 | + |
| 38 | + $html = $this->aire()->form()->resourceful($model)->render(); |
| 39 | + |
| 40 | + $this->assertSelectorAttribute($html, 'form', 'action', URL::to('/users/1')); |
| 41 | + $this->assertSelectorAttribute($html, 'input[name="_method"]', 'value', 'PUT'); |
| 42 | + } |
| 43 | + |
| 44 | + public function test_provided_name_overrides_inferred_name() : void |
| 45 | + { |
| 46 | + Route::post('/foo/bar', function() {})->name('foo.bar.store'); |
| 47 | + |
| 48 | + $model = new XResourcefulModel(['id' => 1]); |
| 49 | + |
| 50 | + $html = $this->aire()->form()->resourceful($model, 'foo.bar')->render(); |
| 51 | + |
| 52 | + $this->assertSelectorAttribute($html, 'form', 'action', URL::to('/foo/bar')); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +class XResourcefulModel extends Model |
| 57 | +{ |
| 58 | + protected $guarded = []; |
| 59 | + |
| 60 | + protected $table = 'users'; |
| 61 | +} |
0 commit comments