From fe848dc21a5bf60e3d9b326cc516910c08268cbf Mon Sep 17 00:00:00 2001 From: Ricardo Perez Date: Wed, 22 Jun 2016 13:18:30 -0500 Subject: [PATCH 1/2] Add ability to check for and retrieve all errors --- .../Form/ErrorStore/ErrorStoreInterface.php | 4 +++ .../Form/ErrorStore/IlluminateErrorStore.php | 4 +-- src/AdamWathan/Form/FormBuilder.php | 10 ++++++ tests/FormBuilderTest.php | 31 +++++++++++++++++++ tests/IlluminateErrorStoreTest.php | 13 ++++++++ 5 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/AdamWathan/Form/ErrorStore/ErrorStoreInterface.php b/src/AdamWathan/Form/ErrorStore/ErrorStoreInterface.php index b1e0906..2e36e4f 100644 --- a/src/AdamWathan/Form/ErrorStore/ErrorStoreInterface.php +++ b/src/AdamWathan/Form/ErrorStore/ErrorStoreInterface.php @@ -4,7 +4,11 @@ interface ErrorStoreInterface { + public function hasErrors(); + public function hasError($key); + public function getErrors(); + public function getError($key); } diff --git a/src/AdamWathan/Form/ErrorStore/IlluminateErrorStore.php b/src/AdamWathan/Form/ErrorStore/IlluminateErrorStore.php index 00052e2..57ee1d1 100644 --- a/src/AdamWathan/Form/ErrorStore/IlluminateErrorStore.php +++ b/src/AdamWathan/Form/ErrorStore/IlluminateErrorStore.php @@ -35,12 +35,12 @@ public function getError($key) return $this->getErrors()->first($key); } - protected function hasErrors() + public function hasErrors() { return $this->session->has('errors'); } - protected function getErrors() + public function getErrors() { return $this->hasErrors() ? $this->session->get('errors') : null; } diff --git a/src/AdamWathan/Form/FormBuilder.php b/src/AdamWathan/Form/FormBuilder.php index ab97569..7258172 100644 --- a/src/AdamWathan/Form/FormBuilder.php +++ b/src/AdamWathan/Form/FormBuilder.php @@ -191,6 +191,11 @@ public function token() return $token; } + public function hasErrors() + { + return $this->errorStore->hasErrors(); + } + public function hasError($name) { if (! isset($this->errorStore)) { @@ -200,6 +205,11 @@ public function hasError($name) return $this->errorStore->hasError($name); } + public function getErrors() + { + return $this->errorStore->getErrors(); + } + public function getError($name, $format = null) { if (! isset($this->errorStore)) { diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index d32b934..7773c06 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -1,6 +1,7 @@ assertFalse($result); } + public function testCanCheckForErrorMessages() + { + $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); + $errorStore->shouldReceive('hasErrors')->andReturn(true); + + $this->form->setErrorStore($errorStore); + + $this->assertTrue($this->form->hasErrors()); + + $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); + $errorStore->shouldReceive('hasErrors')->andReturn(false); + + $this->form->setErrorStore($errorStore); + + $this->assertFalse($this->form->hasErrors()); + } + public function testCanRetrieveErrorMessage() { $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); @@ -195,6 +213,19 @@ public function testCanRetrieveErrorMessage() $this->assertEquals($expected, $result); } + public function testCanRetrieveErrorMessages() + { + $errors = new MessageBag(['foo.bar' => 'Some error']); + $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); + $errorStore->shouldReceive('getErrors')->andReturn($errors); + + $this->form->setErrorStore($errorStore); + + $expected = $errors; + $result = $this->form->getErrors(); + $this->assertEquals($expected, $result); + } + public function testCanRetrieveFormattedErrorMessage() { $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); diff --git a/tests/IlluminateErrorStoreTest.php b/tests/IlluminateErrorStoreTest.php index 7e5d025..b0269fd 100644 --- a/tests/IlluminateErrorStoreTest.php +++ b/tests/IlluminateErrorStoreTest.php @@ -15,4 +15,17 @@ public function test_it_converts_array_keys_to_dot_notation() $errorStore = new IlluminateErrorStore($session); $this->assertTrue($errorStore->hasError('foo[bar]')); } + + public function test_it_returns_all_errors() + { + $errors = new MessageBag(['foo.bar' => 'Some error']); + $session = Mockery::mock('Illuminate\Session\Store'); + $session->shouldReceive('has')->with('errors')->andReturn(true); + $session->shouldReceive('get')->with('errors')->andReturn($errors); + + $errorStore = new IlluminateErrorStore($session); + $this->assertTrue($errorStore->hasErrors()); + $this->assertEquals($errorStore->getErrors(), $errors); + } + } From 837e7997f488f3e32b983b4e1b35e6bfd124bbdd Mon Sep 17 00:00:00 2001 From: Ricardo Perez Date: Wed, 22 Jun 2016 13:43:31 -0500 Subject: [PATCH 2/2] Update to return array instead of collection --- src/AdamWathan/Form/FormBuilder.php | 6 +++++- tests/FormBuilderTest.php | 13 ++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/AdamWathan/Form/FormBuilder.php b/src/AdamWathan/Form/FormBuilder.php index 7258172..56aba08 100644 --- a/src/AdamWathan/Form/FormBuilder.php +++ b/src/AdamWathan/Form/FormBuilder.php @@ -207,7 +207,11 @@ public function hasError($name) public function getErrors() { - return $this->errorStore->getErrors(); + if(! $this->hasErrors()){ + return null; + } + + return $this->errorStore->getErrors()->all(); } public function getError($name, $format = null) diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 7773c06..b182410 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -217,11 +217,22 @@ public function testCanRetrieveErrorMessages() { $errors = new MessageBag(['foo.bar' => 'Some error']); $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); + $errorStore->shouldReceive('hasErrors')->andReturn(true); $errorStore->shouldReceive('getErrors')->andReturn($errors); $this->form->setErrorStore($errorStore); - $expected = $errors; + $expected = $errors->all(); + $result = $this->form->getErrors(); + $this->assertEquals($expected, $result); + + $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); + $errorStore->shouldReceive('hasErrors')->andReturn(false); + $errorStore->shouldReceive('getErrors')->andReturn(null); + + $this->form->setErrorStore($errorStore); + + $expected = null; $result = $this->form->getErrors(); $this->assertEquals($expected, $result); }