From baeec42e6a61f9b1475c82fbf0d6c2f2efe28187 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 6 Dec 2016 14:03:43 -0500 Subject: [PATCH 01/13] Test driving form namespace setter. --- tests/FormOpenTest.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/FormOpenTest.php b/tests/FormOpenTest.php index 1a1739b..7abc057 100644 --- a/tests/FormOpenTest.php +++ b/tests/FormOpenTest.php @@ -117,11 +117,20 @@ public function testCanRenderCsrfToken() $this->assertEquals($expected, $result); } - public function testRenderCustomMethodWithToken() + public function testRenderCustomNamespace() + { + $form = new FormOpen; + $expected = '
'; + $result = $form->namespace('tycho')->render(); + + $this->assertEquals($expected, $result); + } + + public function testRenderCustomMethodWithTokenAndNamespace() { $open = new FormOpen; - $expected = ''; - $result = $open->token('abc123')->delete()->render(); + $expected = ''; + $result = $open->namespace('tycho')->token('abc123')->delete()->render(); $this->assertEquals($expected, $result); } From bbe056c3299a9d00c07e396fcb7ee4a25d96d18a Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 6 Dec 2016 14:04:06 -0500 Subject: [PATCH 02/13] Implementing form namespacing. --- src/AdamWathan/Form/Elements/FormOpen.php | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/AdamWathan/Form/Elements/FormOpen.php b/src/AdamWathan/Form/Elements/FormOpen.php index 73992d2..d8e2904 100644 --- a/src/AdamWathan/Form/Elements/FormOpen.php +++ b/src/AdamWathan/Form/Elements/FormOpen.php @@ -13,6 +13,8 @@ class FormOpen extends Element protected $hiddenMethod; + protected $hiddenNamespace; + public function render() { $tags = [sprintf('', $this->renderAttributes())]; @@ -25,6 +27,10 @@ public function render() $tags[] = $this->hiddenMethod->render(); } + if ($this->hasHiddenNamespace()) { + $tags[] = $this->hiddenNamespace->render(); + } + return implode($tags); } @@ -38,6 +44,11 @@ protected function hasHiddenMethod() return isset($this->hiddenMethod); } + protected function hasHiddenNamespace() + { + return isset($this->hiddenNamespace); + } + public function post() { $this->setMethod('POST'); @@ -109,4 +120,17 @@ public function multipart() { return $this->encodingType('multipart/form-data'); } + + public function namespace($namespace) + { + return $this->setHiddenNamespace($namespace); + } + + protected function setHiddenNamespace($namespace) + { + $this->hiddenNamespace = new Hidden('_namespace'); + $this->hiddenNamespace->value($namespace); + + return $this; + } } From 51a8b7e4911cafbbc0f07ec8639bb8acf14d5194 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 6 Dec 2016 14:31:48 -0500 Subject: [PATCH 03/13] Move namespace method to form builder. --- tests/FormBuilderTest.php | 7 +++++++ tests/FormOpenTest.php | 15 +++------------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 18bb12f..aeabaea 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -26,6 +26,13 @@ public function testFormOpen() $this->assertEquals($expected, $result); } + public function testFormNamespace() + { + $expected = ''; + $result = (string) $this->form->namespace('tycho'); + $this->assertEquals($expected, $result); + } + public function testCanCloseForm() { $expected = ''; diff --git a/tests/FormOpenTest.php b/tests/FormOpenTest.php index 7abc057..1a1739b 100644 --- a/tests/FormOpenTest.php +++ b/tests/FormOpenTest.php @@ -117,20 +117,11 @@ public function testCanRenderCsrfToken() $this->assertEquals($expected, $result); } - public function testRenderCustomNamespace() - { - $form = new FormOpen; - $expected = '
'; - $result = $form->namespace('tycho')->render(); - - $this->assertEquals($expected, $result); - } - - public function testRenderCustomMethodWithTokenAndNamespace() + public function testRenderCustomMethodWithToken() { $open = new FormOpen; - $expected = ''; - $result = $open->namespace('tycho')->token('abc123')->delete()->render(); + $expected = ''; + $result = $open->token('abc123')->delete()->render(); $this->assertEquals($expected, $result); } From 52c25ba84ed042a59c9c9c6b8963e95ee06c204e Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 6 Dec 2016 14:32:16 -0500 Subject: [PATCH 04/13] Make tests pass on new namespace() implementation. --- src/AdamWathan/Form/Elements/FormOpen.php | 24 ----------------------- src/AdamWathan/Form/FormBuilder.php | 12 ++++++++++++ 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/AdamWathan/Form/Elements/FormOpen.php b/src/AdamWathan/Form/Elements/FormOpen.php index d8e2904..73992d2 100644 --- a/src/AdamWathan/Form/Elements/FormOpen.php +++ b/src/AdamWathan/Form/Elements/FormOpen.php @@ -13,8 +13,6 @@ class FormOpen extends Element protected $hiddenMethod; - protected $hiddenNamespace; - public function render() { $tags = [sprintf('', $this->renderAttributes())]; @@ -27,10 +25,6 @@ public function render() $tags[] = $this->hiddenMethod->render(); } - if ($this->hasHiddenNamespace()) { - $tags[] = $this->hiddenNamespace->render(); - } - return implode($tags); } @@ -44,11 +38,6 @@ protected function hasHiddenMethod() return isset($this->hiddenMethod); } - protected function hasHiddenNamespace() - { - return isset($this->hiddenNamespace); - } - public function post() { $this->setMethod('POST'); @@ -120,17 +109,4 @@ public function multipart() { return $this->encodingType('multipart/form-data'); } - - public function namespace($namespace) - { - return $this->setHiddenNamespace($namespace); - } - - protected function setHiddenNamespace($namespace) - { - $this->hiddenNamespace = new Hidden('_namespace'); - $this->hiddenNamespace->value($namespace); - - return $this; - } } diff --git a/src/AdamWathan/Form/FormBuilder.php b/src/AdamWathan/Form/FormBuilder.php index 7b86528..2fa0107 100644 --- a/src/AdamWathan/Form/FormBuilder.php +++ b/src/AdamWathan/Form/FormBuilder.php @@ -30,6 +30,8 @@ class FormBuilder protected $boundData; + protected $namespace; + public function setOldInputProvider(OldInputInterface $oldInputProvider) { $this->oldInput = $oldInputProvider; @@ -231,6 +233,16 @@ public function getError($name, $format = null) return $message; } + public function namespace($namespace) + { + $this->namespace = $namespace; + + $hiddenNamespace = new Hidden('_namespace'); + $hiddenNamespace->value($namespace); + + return $hiddenNamespace; + } + public function bind($data) { $this->boundData = new BoundData($data); From 6ab34b20f893cb11a05031c8f55ee8820cce4f6f Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 6 Dec 2016 15:33:15 -0500 Subject: [PATCH 05/13] Reset namespace on form close(). --- tests/FormBuilderTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index aeabaea..98f61cb 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -40,6 +40,17 @@ public function testCanCloseForm() $this->assertEquals($expected, $result); } + public function testFormCloseResetsNamespace() + { + $this->form->open(); + $this->form->namespace('tycho'); + $this->form->close(); + + $expected = null; + $result = $this->form->getNamespace(); + $this->assertEquals($expected, $result); + } + public function testTextBox() { $expected = ''; From 08a7d921bdf82149223a5d759e76aeccd93b96ed Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 6 Dec 2016 15:34:26 -0500 Subject: [PATCH 06/13] Make tests pass on namespace reset implementation. --- src/AdamWathan/Form/FormBuilder.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/AdamWathan/Form/FormBuilder.php b/src/AdamWathan/Form/FormBuilder.php index 2fa0107..b93f94a 100644 --- a/src/AdamWathan/Form/FormBuilder.php +++ b/src/AdamWathan/Form/FormBuilder.php @@ -66,6 +66,7 @@ protected function hasToken() public function close() { $this->unbindData(); + $this->resetNamespace(); return ''; } @@ -299,6 +300,11 @@ protected function unbindData() $this->boundData = null; } + protected function resetNamespace() + { + $this->namespace = null; + } + public function selectMonth($name) { $options = [ From 33421d15b8a83ccc36b4e1c4bc46ab9dae7e6c53 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 6 Dec 2016 16:00:11 -0500 Subject: [PATCH 07/13] Updating method name for clarity. --- src/AdamWathan/Form/FormBuilder.php | 5 +++++ tests/FormBuilderTest.php | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/AdamWathan/Form/FormBuilder.php b/src/AdamWathan/Form/FormBuilder.php index b93f94a..46236b4 100644 --- a/src/AdamWathan/Form/FormBuilder.php +++ b/src/AdamWathan/Form/FormBuilder.php @@ -244,6 +244,11 @@ public function namespace($namespace) return $hiddenNamespace; } + public function getCurrentNamespace() + { + return $this->namespace; + } + public function bind($data) { $this->boundData = new BoundData($data); diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 98f61cb..a3a6442 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -47,7 +47,7 @@ public function testFormCloseResetsNamespace() $this->form->close(); $expected = null; - $result = $this->form->getNamespace(); + $result = $this->form->getCurrentNamespace(); $this->assertEquals($expected, $result); } From adf7c6db0d530d3b5351cf6a36f8b3e6d28cb5ff Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 6 Dec 2016 16:02:47 -0500 Subject: [PATCH 08/13] Adding namespace old input binding test. --- tests/BindingTest.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/BindingTest.php b/tests/BindingTest.php index d350e83..5cd616a 100644 --- a/tests/BindingTest.php +++ b/tests/BindingTest.php @@ -395,6 +395,29 @@ public function testOldInputTakesPrecedenceOverBinding() $this->assertEquals($expected, $result); } + public function testNamespacedOldInputTakesPrecedenceOverBinding() + { + $oldInput = Mockery::mock('AdamWathan\Form\OldInput\OldInputInterface'); + $oldInput->shouldReceive('hasOldInput')->andReturn(true); + $oldInput->shouldReceive('getOldInput')->with('_namespace')->andReturn('profile'); + $oldInput->shouldReceive('getOldInput')->with('first_name')->andReturn('Jesse'); + $this->form->setOldInputProvider($oldInput); + + $object = $this->getStubObject(); + + $expected = ''; + $this->form->namespace('profile'); + $this->form->bind($object); + $result = (string) $this->form->text('first_name'); + $this->assertEquals($expected, $result); + + $expected = ''; + $this->form->namespace('user'); + $this->form->bind($object); + $result = (string) $this->form->text('first_name'); + $this->assertEquals($expected, $result); + } + public function testExplicitUncheckOnCheckboxTakesPrecedenceOverBinding() { $object = $this->getStubObject(); From 27e1eb55f4404fae5a5d6454aa415baa5b93ae38 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 6 Dec 2016 16:03:24 -0500 Subject: [PATCH 09/13] Making tests pass on namespace binding. --- src/AdamWathan/Form/FormBuilder.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/AdamWathan/Form/FormBuilder.php b/src/AdamWathan/Form/FormBuilder.php index 46236b4..5359d01 100644 --- a/src/AdamWathan/Form/FormBuilder.php +++ b/src/AdamWathan/Form/FormBuilder.php @@ -256,7 +256,7 @@ public function bind($data) public function getValueFor($name) { - if ($this->hasOldInput()) { + if ($this->hasOldInput() && $this->hasMatchingOldNamespace()) { return $this->getOldInput($name); } @@ -291,6 +291,15 @@ protected function getBoundValue($name, $default) return $this->escape($this->boundData->get($name, $default)); } + protected function hasMatchingOldNamespace() + { + if (! $this->getCurrentNamespace()) { + return true; + } + + return $this->getOldInput('_namespace') === $this->getCurrentNamespace(); + } + protected function escape($value) { if (! is_string($value)) { From b349416fc58f587166380094c6ff72787ff46d16 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 6 Dec 2016 16:06:37 -0500 Subject: [PATCH 10/13] Expanding test case. --- tests/BindingTest.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/BindingTest.php b/tests/BindingTest.php index 5cd616a..b1945bc 100644 --- a/tests/BindingTest.php +++ b/tests/BindingTest.php @@ -406,7 +406,6 @@ public function testNamespacedOldInputTakesPrecedenceOverBinding() $object = $this->getStubObject(); $expected = ''; - $this->form->namespace('profile'); $this->form->bind($object); $result = (string) $this->form->text('first_name'); $this->assertEquals($expected, $result); @@ -416,6 +415,12 @@ public function testNamespacedOldInputTakesPrecedenceOverBinding() $this->form->bind($object); $result = (string) $this->form->text('first_name'); $this->assertEquals($expected, $result); + + $expected = ''; + $this->form->namespace('profile'); + $this->form->bind($object); + $result = (string) $this->form->text('first_name'); + $this->assertEquals($expected, $result); } public function testExplicitUncheckOnCheckboxTakesPrecedenceOverBinding() From 1da702822fba20cff1e318db4f8578541b8cda21 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 6 Dec 2016 17:20:01 -0500 Subject: [PATCH 11/13] Cannot use namespace method, because reserved keyword in 5.x --- src/AdamWathan/Form/FormBuilder.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/AdamWathan/Form/FormBuilder.php b/src/AdamWathan/Form/FormBuilder.php index 5359d01..043864c 100644 --- a/src/AdamWathan/Form/FormBuilder.php +++ b/src/AdamWathan/Form/FormBuilder.php @@ -234,16 +234,6 @@ public function getError($name, $format = null) return $message; } - public function namespace($namespace) - { - $this->namespace = $namespace; - - $hiddenNamespace = new Hidden('_namespace'); - $hiddenNamespace->value($namespace); - - return $hiddenNamespace; - } - public function getCurrentNamespace() { return $this->namespace; @@ -291,6 +281,16 @@ protected function getBoundValue($name, $default) return $this->escape($this->boundData->get($name, $default)); } + protected function setNamespace($namespace) + { + $this->namespace = $namespace; + + $hiddenNamespace = new Hidden('_namespace'); + $hiddenNamespace->value($namespace); + + return $hiddenNamespace; + } + protected function hasMatchingOldNamespace() { if (! $this->getCurrentNamespace()) { @@ -338,4 +338,13 @@ public function selectMonth($name) return $this->select($name, $options); } + + public function __call($method, $parameters) + { + if ($method == 'namespace') { + return $this->setNamespace($parameters[0]); + } + + return call_user_func_array([$this, $method], $parameters); + } } From f104278ce6f18cf069a0d62aa94a727dfffc7383 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 6 Dec 2016 19:10:44 -0500 Subject: [PATCH 12/13] Changing implementation to name() on builder, to be used before open(). --- tests/BindingTest.php | 4 ++-- tests/FormBuilderTest.php | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/BindingTest.php b/tests/BindingTest.php index b1945bc..f632208 100644 --- a/tests/BindingTest.php +++ b/tests/BindingTest.php @@ -411,13 +411,13 @@ public function testNamespacedOldInputTakesPrecedenceOverBinding() $this->assertEquals($expected, $result); $expected = ''; - $this->form->namespace('user'); + $this->form->name('user')->open(); $this->form->bind($object); $result = (string) $this->form->text('first_name'); $this->assertEquals($expected, $result); $expected = ''; - $this->form->namespace('profile'); + $this->form->name('profile')->open(); $this->form->bind($object); $result = (string) $this->form->text('first_name'); $this->assertEquals($expected, $result); diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index a3a6442..efe5e88 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -26,10 +26,10 @@ public function testFormOpen() $this->assertEquals($expected, $result); } - public function testFormNamespace() + public function testFormOpenWithNamespace() { - $expected = ''; - $result = (string) $this->form->namespace('tycho'); + $expected = '
'; + $result = (string) $this->form->name('tycho')->open(); $this->assertEquals($expected, $result); } @@ -42,8 +42,7 @@ public function testCanCloseForm() public function testFormCloseResetsNamespace() { - $this->form->open(); - $this->form->namespace('tycho'); + $this->form->name('tycho')->open(); $this->form->close(); $expected = null; From 3b30df46b5917b3393311c37345b066ca2e2842c Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 6 Dec 2016 19:11:13 -0500 Subject: [PATCH 13/13] Making tests pass with new Form::name() implementation. --- src/AdamWathan/Form/Elements/FormOpen.php | 26 +++++++++++++++++++++ src/AdamWathan/Form/FormBuilder.php | 28 +++++++---------------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/AdamWathan/Form/Elements/FormOpen.php b/src/AdamWathan/Form/Elements/FormOpen.php index 73992d2..0bd8528 100644 --- a/src/AdamWathan/Form/Elements/FormOpen.php +++ b/src/AdamWathan/Form/Elements/FormOpen.php @@ -13,6 +13,15 @@ class FormOpen extends Element protected $hiddenMethod; + protected $hiddenNamespace; + + public function __construct($namespace = null) + { + if ($namespace) { + $this->setHiddenNamespace($namespace); + } + } + public function render() { $tags = [sprintf('', $this->renderAttributes())]; @@ -25,6 +34,10 @@ public function render() $tags[] = $this->hiddenMethod->render(); } + if ($this->hasHiddenNamespace()) { + $tags[] = $this->hiddenNamespace->render(); + } + return implode($tags); } @@ -38,6 +51,11 @@ protected function hasHiddenMethod() return isset($this->hiddenMethod); } + protected function hasHiddenNamespace() + { + return isset($this->hiddenNamespace); + } + public function post() { $this->setMethod('POST'); @@ -84,6 +102,14 @@ protected function setHiddenMethod($method) return $this; } + protected function setHiddenNamespace($namespace) + { + $this->hiddenNamespace = new Hidden('_namespace'); + $this->hiddenNamespace->value($namespace); + + return $this; + } + public function setMethod($method) { $this->setAttribute('method', $method); diff --git a/src/AdamWathan/Form/FormBuilder.php b/src/AdamWathan/Form/FormBuilder.php index 043864c..25849db 100644 --- a/src/AdamWathan/Form/FormBuilder.php +++ b/src/AdamWathan/Form/FormBuilder.php @@ -47,9 +47,16 @@ public function setToken($token) $this->csrfToken = $token; } + public function name($namespace) + { + $this->namespace = $namespace; + + return $this; + } + public function open() { - $open = new FormOpen; + $open = new FormOpen($this->namespace); if ($this->hasToken()) { $open->token($this->csrfToken); @@ -281,16 +288,6 @@ protected function getBoundValue($name, $default) return $this->escape($this->boundData->get($name, $default)); } - protected function setNamespace($namespace) - { - $this->namespace = $namespace; - - $hiddenNamespace = new Hidden('_namespace'); - $hiddenNamespace->value($namespace); - - return $hiddenNamespace; - } - protected function hasMatchingOldNamespace() { if (! $this->getCurrentNamespace()) { @@ -338,13 +335,4 @@ public function selectMonth($name) return $this->select($name, $options); } - - public function __call($method, $parameters) - { - if ($method == 'namespace') { - return $this->setNamespace($parameters[0]); - } - - return call_user_func_array([$this, $method], $parameters); - } }