Skip to content

Commit

Permalink
Implicitly open route() and resourceful() calls and bump minimum Lara…
Browse files Browse the repository at this point in the history
…vel version

Addresses #35 and #32
  • Loading branch information
inxilpro committed Sep 16, 2019
1 parent 2f42a0e commit 56e1fa6
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"license": "MIT",
"require": {
"php": ">=7.1",
"illuminate/support": "5.5.*|5.6.*|5.7.*|5.8.*|6.0.*",
"illuminate/view": "5.5.*|5.6.*|5.7.*|5.8.*|6.0.*",
"illuminate/events": "5.5.*|5.6.*|5.7.*|5.8.*|6.0.*",
"illuminate/support": "5.7.*|5.8.*|6.0.*",
"illuminate/view": "5.7.*|5.8.*|6.0.*",
"illuminate/events": "5.7.*|5.8.*|6.0.*",
"ext-json": "*",
"graham-campbell/markdown": "^11.1",
"laravel/framework": "^6.0",
Expand Down
15 changes: 15 additions & 0 deletions src/Aire.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

/**
* @method static \Galahad\Aire\Elements\Form route(string $route_name, $parameters = [], bool $absolute = true)
* @method static \Galahad\Aire\Elements\Form resourceful(\Illuminate\Database\Eloquent\Model $model, $resource_name = null, $prepend_parameters = [])
* @method static \Galahad\Aire\Elements\Label label(string $label)
* @method static \Galahad\Aire\Elements\Button button(string $label = null)
* @method static \Galahad\Aire\Elements\Button submit(string $label = 'Submit')
Expand Down Expand Up @@ -54,6 +55,16 @@ class Aire
*/
protected static $default_theme_config;

/**
* These methods will implicitly open a form and then call it
*
* @var array
*/
protected static $implicit_open = [
'route',
'resourceful',
];

/**
* Global store of element IDs
*
Expand Down Expand Up @@ -321,6 +332,10 @@ public function __call($method_name, $arguments)
{
$form = $this->form ?? $this->form();

if (!$form->isOpened() && in_array($method_name, static::$implicit_open)) {
$form->open();
}

// @codeCoverageIgnoreStart
if (!method_exists($form, $method_name)) {
throw new BadMethodCallException(sprintf(
Expand Down
9 changes: 7 additions & 2 deletions src/Elements/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public function open() : self
*/
public function close() : self
{
if (!$this->opened) {
if (!$this->isOpened()) {
throw new BadMethodCallException('Trying to close a form that hasn\'t been opened.');
}

Expand All @@ -283,6 +283,11 @@ public function close() : self
return $this;
}

public function isOpened() : bool
{
return true === $this->opened;
}

public function openButton() : Button
{
$this->pending_button = new Button($this->aire, $this);
Expand Down Expand Up @@ -456,7 +461,7 @@ public function formRequest(string $class_name) : self

public function render() : string
{
if ($this->opened) {
if ($this->isOpened()) {
return '';
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/RouterIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function test_a_form_action_can_be_set_using_a_route_name()
{
Route::post('foo')->name('foo');

$html = $this->aire()->route('foo')->render();
$html = $this->aire()->route('foo')->close()->render();

$this->assertSelectorAttribute($html, 'form', 'action', route('foo'));
}
Expand All @@ -22,7 +22,7 @@ public function test_a_route_with_a_single_verb_sets_the_form_method()
{
Route::put('foo')->name('foo');

$html = $this->aire()->route('foo')->render();
$html = $this->aire()->route('foo')->close()->render();

$this->assertSelectorAttribute($html, 'form', 'method', 'POST');
$this->assertSelectorAttribute($html, 'input[name="_method"]', 'value', 'PUT');
Expand All @@ -32,7 +32,7 @@ public function test_a_route_with_multiple_verbs_do_not_set_the_form_method()
{
Route::any('foo')->name('foo');

$html = $this->aire()->route('foo')->render();
$html = $this->aire()->route('foo')->close()->render();

$this->assertSelectorDoesNotExist($html, 'input[name="_method"]');
}
Expand All @@ -41,7 +41,7 @@ public function test_a_get_route_removes_hidden_method_field()
{
Route::get('foo')->name('foo');

$html = $this->aire()->route('foo')->render();
$html = $this->aire()->route('foo')->close()->render();

$this->assertSelectorAttribute($html, 'form', 'method', 'GET');
$this->assertSelectorDoesNotExist($html, 'input[name="_method"]');
Expand Down
33 changes: 33 additions & 0 deletions tests/Unit/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Galahad\Aire\Tests\Unit;

use Galahad\Aire\Tests\TestCase;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -91,4 +92,36 @@ public function test_hidden_method_field_is_added_for_delete_forms()
$this->assertSelectorAttribute($form, 'input[name="_method"]', 'value', 'DELETE');
$this->assertSelectorAttribute($form, 'form', 'method', 'POST');
}

public function test_calling_route_implicitly_opens_the_form() : void
{
Route::get('/foo/bar')->name('demo-route');

$form = $this->aire()->form();

$this->assertFalse($form->isOpened());

$this->aire()->route('demo-route');

$this->assertTrue($form->isOpened());

$form->close();
}

public function test_calling_resourceful_implicitly_opens_the_form() : void
{
Route::post('/tests')->name('tests.store');

$form = $this->aire()->form();

$this->assertFalse($form->isOpened());

$this->aire()->resourceful(new class extends Model {
protected $table = 'test';
});

$this->assertTrue($form->isOpened());

$form->close();
}
}

0 comments on commit 56e1fa6

Please sign in to comment.