Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Enhancement/get all errors #100

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/AdamWathan/Form/ErrorStore/ErrorStoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

interface ErrorStoreInterface
{
public function hasErrors();

public function hasError($key);

public function getErrors();

public function getError($key);
}
4 changes: 2 additions & 2 deletions src/AdamWathan/Form/ErrorStore/IlluminateErrorStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 14 additions & 0 deletions src/AdamWathan/Form/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ public function token()
return $token;
}

public function hasErrors()
{
return $this->errorStore->hasErrors();
}

public function hasError($name)
{
if (! isset($this->errorStore)) {
Expand All @@ -200,6 +205,15 @@ public function hasError($name)
return $this->errorStore->hasError($name);
}

public function getErrors()
{
if(! $this->hasErrors()){
return null;
}

return $this->errorStore->getErrors()->all();
}

public function getError($name, $format = null)
{
if (! isset($this->errorStore)) {
Expand Down
42 changes: 42 additions & 0 deletions tests/FormBuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use AdamWathan\Form\FormBuilder;
use Illuminate\Support\MessageBag;

class FormBuilderTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -182,6 +183,23 @@ public function testCanCheckForErrorMessage()
$this->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');
Expand All @@ -195,6 +213,30 @@ 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('hasErrors')->andReturn(true);
$errorStore->shouldReceive('getErrors')->andReturn($errors);

$this->form->setErrorStore($errorStore);

$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);
}

public function testCanRetrieveFormattedErrorMessage()
{
$errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface');
Expand Down
13 changes: 13 additions & 0 deletions tests/IlluminateErrorStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}