From 080fb4f840319a74ae5ac00334365bef28925777 Mon Sep 17 00:00:00 2001 From: Adam Engebretson Date: Thu, 5 Feb 2015 10:39:05 -0600 Subject: [PATCH] Moving Namespace Signed-off-by: Adam Engebretson --- .gitattributes | 7 + .gitignore | 6 + .scrutinizer.yml | 55 +++ .travis.yml | 13 + CONTRIBUTING.md | 3 + LICENSE.txt | 21 + composer.json | 55 +-- phpunit.php | 28 ++ phpunit.xml | 26 ++ readme.md | 235 ++++++++++ FormBuilder.php => src/FormBuilder.php | 6 +- FormFacade.php => src/FormFacade.php | 4 +- HtmlBuilder.php => src/HtmlBuilder.php | 2 +- HtmlFacade.php => src/HtmlFacade.php | 4 +- .../HtmlServiceProvider.php | 6 +- helpers.php => src/helpers.php | 0 tests/FormBuilderTest.php | 440 ++++++++++++++++++ 17 files changed, 874 insertions(+), 37 deletions(-) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .scrutinizer.yml create mode 100755 .travis.yml create mode 100755 CONTRIBUTING.md create mode 100644 LICENSE.txt create mode 100755 phpunit.php create mode 100755 phpunit.xml create mode 100644 readme.md rename FormBuilder.php => src/FormBuilder.php (99%) rename FormFacade.php => src/FormFacade.php (76%) rename HtmlBuilder.php => src/HtmlBuilder.php (99%) rename HtmlFacade.php => src/HtmlFacade.php (76%) rename HtmlServiceProvider.php => src/HtmlServiceProvider.php (87%) rename helpers.php => src/helpers.php (100%) create mode 100644 tests/FormBuilderTest.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..b7afee8 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,7 @@ +/tests export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.scrutinizer.yml export-ignore +.travis.yml export-ignore +phpunit.php export-ignore +phpunit.xml export-ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ac9c2d --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/vendor +composer.phar +composer.lock +.DS_Store +Thumbs.db +.idea diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..6c33c7b --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,55 @@ +filter: + excluded_paths: [tests/*] + +checks: + php: + code_rating: true + duplication: true + variable_existence: true + useless_calls: true + use_statement_alias_conflict: true + unused_variables: true + unused_properties: true + unused_parameters: true + unused_methods: true + unreachable_code: true + sql_injection_vulnerabilities: true + security_vulnerabilities: true + precedence_mistakes: true + precedence_in_conditions: true + parameter_non_unique: true + no_property_on_interface: true + no_non_implemented_abstract_methods: true + deprecated_code_usage: true + closure_use_not_conflicting: true + closure_use_modifiable: true + avoid_useless_overridden_methods: true + avoid_conflicting_incrementers: true + assignment_of_null_return: true + avoid_usage_of_logical_operators: true + ensure_lower_case_builtin_functions: true + foreach_traversable: true + function_in_camel_caps: true + instanceof_class_exists: true + lowercase_basic_constants: true + lowercase_php_keywords: true + missing_arguments: true + no_commented_out_code: true + no_duplicate_arguments: true + no_else_if_statements: true + no_space_between_concatenation_operator: true + no_space_inside_cast_operator: true + no_trailing_whitespace: true + no_underscore_prefix_in_properties: true + no_unnecessary_if: true + no_unnecessary_function_call_in_for_loop: true + non_commented_empty_catch_block: true + php5_style_constructor: true + parameters_in_camelcaps: true + prefer_while_loop_over_for_loop: true + properties_in_camelcaps: true + require_scope_for_methods: true + require_scope_for_properties: true + spacing_around_conditional_operators: true + spacing_around_non_conditional_operators: true + spacing_of_function_arguments: true diff --git a/.travis.yml b/.travis.yml new file mode 100755 index 0000000..cd31330 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: php + +php: + - 5.4 + - 5.5 + - 5.6 + - hhvm + +sudo: false + +install: travis_retry composer install --no-interaction --prefer-source + +script: vendor/bin/phpunit diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100755 index 0000000..2cbf825 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,3 @@ +# Laravel Contribution Guide + +Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](http://laravel.com/docs/contributions). Please review the entire guide before sending a pull request. diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..40cd6a3 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/composer.json b/composer.json index d26646b..32ce351 100755 --- a/composer.json +++ b/composer.json @@ -1,30 +1,33 @@ { - "name": "illuminate/html", - "license": "MIT", - "authors": [ - { - "name": "Taylor Otwell", - "email": "taylorotwell@gmail.com" - } - ], - "require": { - "php": ">=5.4.0", - "illuminate/http": "~5.0", - "illuminate/session": "~5.0", - "illuminate/support": "~5.0" + "name": "illuminate/html", + "license": "MIT", + "authors": [ + { + "name": "Adam Engebretson", + "email": "adam@laravelcollective.com" }, - "autoload": { - "psr-4": { - "Illuminate\\Html\\": "" - }, - "files": [ - "helpers.php" - ] + { + "name": "Taylor Otwell", + "email": "taylorotwell@gmail.com" + } + ], + "require": { + "php": ">=5.4.0", + "illuminate/http": "~5.0", + "illuminate/routing": "~5.0", + "illuminate/session": "~5.0", + "illuminate/support": "~5.0" + }, + "require-dev": { + "mockery/mockery": "~0.9", + "phpunit/phpunit": "~4.0" + }, + "autoload": { + "psr-4": { + "Collective\\Html\\": "src/" }, - "extra": { - "branch-alias": { - "dev-master": "5.0-dev" - } - }, - "minimum-stability": "dev" + "files": [ + "src/helpers.php" + ] + } } diff --git a/phpunit.php b/phpunit.php new file mode 100755 index 0000000..25c90c8 --- /dev/null +++ b/phpunit.php @@ -0,0 +1,28 @@ + + + + + ./tests + + + + + ./src + + + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..e7d3a03 --- /dev/null +++ b/readme.md @@ -0,0 +1,235 @@ +# Forms & HTML + +[![Build Status](https://travis-ci.org/LaravelCollective/html.svg)](https://travis-ci.org/LaravelCollective/html) +[![Total Downloads](https://poser.pugx.org/LaravelCollective/html/downloads.svg)](https://packagist.org/packages/laravelcollective/html) +[![Latest Stable Version](https://poser.pugx.org/LaravelCollective/html/v/stable.svg)](https://packagist.org/packages/laravelcollective/html) +[![Latest Unstable Version](https://poser.pugx.org/LaravelCollective/html/v/unstable.svg)](https://packagist.org/packages/laravelcollective/html) +[![License](https://poser.pugx.org/LaravelCollective/html/license.svg)](https://packagist.org/packages/laravelcollective/html) + +- [Installation](#installation) +- [Opening A Form](#opening-a-form) +- [CSRF Protection](#csrf-protection) +- [Form Model Binding](#form-model-binding) +- [Labels](#labels) +- [Text, Text Area, Password & Hidden Fields](#text) +- [Checkboxes and Radio Buttons](#checkboxes-and-radio-buttons) +- [File Input](#file-input) +- [Number Input](#number) +- [Drop-Down Lists](#drop-down-lists) +- [Buttons](#buttons) +- [Custom Macros](#custom-macros) +- [Generating URLs](#generating-urls) + + +## Installation + +Begin by installing this package through Composer. Edit your project's `composer.json` file to require `laravelcollective/html`. + + "require": { + "laravelcollective/html": "~5.0" + } + +Next, update Composer from the Terminal: + + composer update + +Finally, add your new provider to the `providers` array of `config/app.php`: + +```php + 'providers' => [ + // ... + 'Collective\Html\HtmlServiceProvider', + // ... + ]; +``` + + +## Opening A Form + +#### Opening A Form + + {{ Form::open(array('url' => 'foo/bar')) }} + // + {{ Form::close() }} + +By default, a `POST` method will be assumed; however, you are free to specify another method: + + echo Form::open(array('url' => 'foo/bar', 'method' => 'put')) + +> **Note:** Since HTML forms only support `POST` and `GET`, `PUT` and `DELETE` methods will be spoofed by automatically adding a `_method` hidden field to your form. + +You may also open forms that point to named routes or controller actions: + + echo Form::open(array('route' => 'route.name')) + + echo Form::open(array('action' => 'Controller@method')) + +You may pass in route parameters as well: + + echo Form::open(array('route' => array('route.name', $user->id))) + + echo Form::open(array('action' => array('Controller@method', $user->id))) + +If your form is going to accept file uploads, add a `files` option to your array: + + echo Form::open(array('url' => 'foo/bar', 'files' => true)) + + +## CSRF Protection + +#### Adding The CSRF Token To A Form + +Laravel provides an easy method of protecting your application from cross-site request forgeries. First, a random token is placed in your user's session. If you use the `Form::open` method with `POST`, `PUT` or `DELETE` the CSRF token will be added to your forms as a hidden field automatically. Alternatively, if you wish to generate the HTML for the hidden CSRF field, you may use the `token` method: + + echo Form::token(); + +#### Attaching The CSRF Filter To A Route + + Route::post('profile', array('before' => 'csrf', function() + { + // + })); + + +## Form Model Binding + +#### Opening A Model Form + +Often, you will want to populate a form based on the contents of a model. To do so, use the `Form::model` method: + + echo Form::model($user, array('route' => array('user.update', $user->id))) + +Now, when you generate a form element, like a text input, the model's value matching the field's name will automatically be set as the field value. So, for example, for a text input named `email`, the user model's `email` attribute would be set as the value. However, there's more! If there is an item in the Session flash data matching the input name, that will take precedence over the model's value. So, the priority looks like this: + +1. Session Flash Data (Old Input) +2. Explicitly Passed Value +3. Model Attribute Data + +This allows you to quickly build forms that not only bind to model values, but easily re-populate if there is a validation error on the server! + +> **Note:** When using `Form::model`, be sure to close your form with `Form::close`! + + +## Labels + +#### Generating A Label Element + + echo Form::label('email', 'E-Mail Address'); + +#### Specifying Extra HTML Attributes + + echo Form::label('email', 'E-Mail Address', array('class' => 'awesome')); + +> **Note:** After creating a label, any form element you create with a name matching the label name will automatically receive an ID matching the label name as well. + + +## Text, Text Area, Password & Hidden Fields + +#### Generating A Text Input + + echo Form::text('username'); + +#### Specifying A Default Value + + echo Form::text('email', 'example@gmail.com'); + +> **Note:** The *hidden* and *textarea* methods have the same signature as the *text* method. + +#### Generating A Password Input + + echo Form::password('password'); + +#### Generating Other Inputs + + echo Form::email($name, $value = null, $attributes = array()); + echo Form::file($name, $attributes = array()); + + +## Checkboxes and Radio Buttons + +#### Generating A Checkbox Or Radio Input + + echo Form::checkbox('name', 'value'); + + echo Form::radio('name', 'value'); + +#### Generating A Checkbox Or Radio Input That Is Checked + + echo Form::checkbox('name', 'value', true); + + echo Form::radio('name', 'value', true); + + +## Number + +#### Generating A Number Input + + echo Form::number('name', 'value'); + + +## File Input + +#### Generating A File Input + + echo Form::file('image'); + +> **Note:** The form must have been opened with the `files` option set to `true`. + + +## Drop-Down Lists + +#### Generating A Drop-Down List + + echo Form::select('size', array('L' => 'Large', 'S' => 'Small')); + +#### Generating A Drop-Down List With Selected Default + + echo Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S'); + +#### Generating A Grouped List + + echo Form::select('animal', array( + 'Cats' => array('leopard' => 'Leopard'), + 'Dogs' => array('spaniel' => 'Spaniel'), + )); + +#### Generating A Drop-Down List With A Range + + echo Form::selectRange('number', 10, 20); + +#### Generating A List With Month Names + + echo Form::selectMonth('month'); + + +## Buttons + +#### Generating A Submit Button + + echo Form::submit('Click Me!'); + +> **Note:** Need to create a button element? Try the *button* method. It has the same signature as *submit*. + + +## Custom Macros + +#### Registering A Form Macro + +It's easy to define your own custom Form class helpers called "macros". Here's how it works. First, simply register the macro with a given name and a Closure: + + Form::macro('myField', function() + { + return ''; + }); + +Now you can call your macro using its name: + +#### Calling A Custom Form Macro + + echo Form::myField(); + + + +##Generating URLs + +For more information on generating URL's, check out the documentation on [helpers](/docs/helpers#urls). \ No newline at end of file diff --git a/FormBuilder.php b/src/FormBuilder.php similarity index 99% rename from FormBuilder.php rename to src/FormBuilder.php index daadb0e..5be6283 100755 --- a/FormBuilder.php +++ b/src/FormBuilder.php @@ -1,4 +1,4 @@ -registerFormBuilder(); - $this->app->alias('html', 'Illuminate\Html\HtmlBuilder'); - $this->app->alias('form', 'Illuminate\Html\FormBuilder'); + $this->app->alias('html', 'Collective\Html\HtmlBuilder'); + $this->app->alias('form', 'Collective\Html\FormBuilder'); } /** diff --git a/helpers.php b/src/helpers.php similarity index 100% rename from helpers.php rename to src/helpers.php diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php new file mode 100644 index 0000000..01354c8 --- /dev/null +++ b/tests/FormBuilderTest.php @@ -0,0 +1,440 @@ +urlGenerator = new UrlGenerator(new RouteCollection, Request::create('/foo', 'GET')); + $this->htmlBuilder = new HtmlBuilder($this->urlGenerator); + $this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, 'abc'); + } + + + /** + * Destroy the test environment. + */ + public function tearDown() + { + m::close(); + } + + + public function testOpeningForm() + { + $form1 = $this->formBuilder->open(['method' => 'GET']); + $form2 = $this->formBuilder->open(['method' => 'POST', 'class' => 'form', 'id' => 'id-form']); + $form3 = $this->formBuilder->open(['method' => 'GET', 'accept-charset' => 'UTF-16']); + $form4 = $this->formBuilder->open(['method' => 'GET', 'accept-charset' => 'UTF-16', 'files' => true]); + $form5 = $this->formBuilder->open(['method' => 'PUT']); + + + $this->assertEquals('
', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + $this->assertEquals('', $form5); + } + + + public function testClosingForm() + { + $this->assertEquals('
', $this->formBuilder->close()); + } + + + public function testFormLabel() + { + $form1 = $this->formBuilder->label('foo', 'Foobar'); + $form2 = $this->formBuilder->label('foo', 'Foobar', ['class' => 'control-label']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + } + + + public function testFormInput() + { + $form1 = $this->formBuilder->input('text', 'foo'); + $form2 = $this->formBuilder->input('text', 'foo', 'foobar'); + $form3 = $this->formBuilder->input('date', 'foobar', null, ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + } + + + public function testPasswordsNotFilled() + { + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + + $session->shouldReceive('getOldInput')->never(); + + $form1 = $this->formBuilder->password('password'); + + $this->assertEquals('', $form1); + } + + + public function testFilesNotFilled() + { + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + + $session->shouldReceive('getOldInput')->never(); + + $form = $this->formBuilder->file('img'); + + $this->assertEquals('', $form); + } + + + public function testFormText() + { + $form1 = $this->formBuilder->input('text', 'foo'); + $form2 = $this->formBuilder->text('foo'); + $form3 = $this->formBuilder->text('foo', 'foobar'); + $form4 = $this->formBuilder->text('foo', null, ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals($form1, $form2); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + } + + + public function testFormTextRepopulation() + { + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + $this->setModel($model = ['relation' => ['key' => 'attribute'], 'other' => 'val']); + + $session->shouldReceive('getOldInput')->twice()->with('name_with_dots')->andReturn('some value'); + $input = $this->formBuilder->text('name.with.dots', 'default value'); + $this->assertEquals('', $input); + + $session->shouldReceive('getOldInput')->once()->with('text.key.sub')->andReturn(null); + $input = $this->formBuilder->text('text[key][sub]', 'default value'); + $this->assertEquals('', $input); + + $session->shouldReceive('getOldInput')->with('relation.key')->andReturn(null); + $input1 = $this->formBuilder->text('relation[key]'); + + $this->setModel($model, false); + $input2 = $this->formBuilder->text('relation[key]'); + + $this->assertEquals('', $input1); + $this->assertEquals($input1, $input2); + } + + + public function testFormPassword() + { + $form1 = $this->formBuilder->password('foo'); + $form2 = $this->formBuilder->password('foo', ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + } + + + public function testFormHidden() + { + $form1 = $this->formBuilder->hidden('foo'); + $form2 = $this->formBuilder->hidden('foo', 'foobar'); + $form3 = $this->formBuilder->hidden('foo', null, ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + } + + + public function testFormEmail() + { + $form1 = $this->formBuilder->email('foo'); + $form2 = $this->formBuilder->email('foo', 'foobar'); + $form3 = $this->formBuilder->email('foo', null, ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + } + + + public function testFormFile() + { + $form1 = $this->formBuilder->file('foo'); + $form2 = $this->formBuilder->file('foo', ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + } + + + public function testFormTextarea() + { + $form1 = $this->formBuilder->textarea('foo'); + $form2 = $this->formBuilder->textarea('foo', 'foobar'); + $form3 = $this->formBuilder->textarea('foo', null, ['class' => 'span2']); + $form4 = $this->formBuilder->textarea('foo', null, ['size' => '60x15']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + } + + + public function testSelect() + { + $select = $this->formBuilder->select( + 'size', + ['L' => 'Large', 'S' => 'Small'] + ); + $this->assertEquals($select, ''); + + + + $select = $this->formBuilder->select( + 'size', + ['L' => 'Large', 'S' => 'Small'], + 'L' + ); + $this->assertEquals($select, ''); + + + + $select = $this->formBuilder->select( + 'size', + ['L' => 'Large', 'S' => 'Small'], + null, + ['class' => 'class-name', 'id' => 'select-id'] + ); + $this->assertEquals($select, ''); + + + + $this->formBuilder->label('select-name-id'); + $select = $this->formBuilder->select( + 'select-name-id', + [], + null, + ['name' => 'select-name'] + ); + $this->assertEquals($select, ''); + } + + + public function testFormSelectRepopulation() + { + $list = ['L' => 'Large', 'M' => 'Medium', 'S' => 'Small']; + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + $this->setModel($model = ['size' => ['key' => 'S'], 'other' => 'val']); + + $session->shouldReceive('getOldInput')->twice()->with('size')->andReturn('M'); + $select = $this->formBuilder->select('size', $list, 'S'); + $this->assertEquals($select, ''); + + $session->shouldReceive('getOldInput')->twice()->with('size.multi')->andReturn(['L', 'S']); + $select = $this->formBuilder->select('size[multi][]', $list, 'M', ['multiple' => 'multiple']); + $this->assertEquals($select, ''); + + $session->shouldReceive('getOldInput')->once()->with('size.key')->andReturn(null); + $select = $this->formBuilder->select('size[key]', $list); + $this->assertEquals($select, ''); + } + + + public function testFormSelectYear() + { + $select1 = $this->formBuilder->selectYear('year', 2000, 2020); + $select2 = $this->formBuilder->selectYear('year', 2000, 2020, null, ['id' => 'foo']); + $select3 = $this->formBuilder->selectYear('year', 2000, 2020, '2000'); + + $this->assertContains('', $select2); + $this->assertContains('', $range); + $this->assertContains('', $range); + } + + + public function testFormSelectMonth() + { + $month1 = $this->formBuilder->selectMonth('month'); + $month2 = $this->formBuilder->selectMonth('month', '1'); + $month3 = $this->formBuilder->selectMonth('month', null, ['id' => 'foo']); + + $this->assertContains('', $month2); + $this->assertContains('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + } + + + public function testFormCheckboxRepopulation() + { + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + $session->shouldReceive('getOldInput')->withNoArgs()->andReturn([1]); + + $session->shouldReceive('getOldInput')->once()->with('check')->andReturn(null); + $check = $this->formBuilder->checkbox('check', 1, true); + $this->assertEquals('', $check); + + $session->shouldReceive('getOldInput')->with('check.key')->andReturn('yes'); + $check = $this->formBuilder->checkbox('check[key]', 'yes'); + $this->assertEquals('', $check); + + $session->shouldReceive('getOldInput')->with('multicheck')->andReturn([1, 3]); + $check1 = $this->formBuilder->checkbox('multicheck[]', 1); + $check2 = $this->formBuilder->checkbox('multicheck[]', 2, true); + $check3 = $this->formBuilder->checkbox('multicheck[]', 3); + + $this->assertEquals('', $check1); + $this->assertEquals('', $check2); + $this->assertEquals('', $check3); + } + + + public function testFormCheckboxWithoutSession() + { + $form1 = $this->formBuilder->checkbox('foo'); + $form2 = $this->formBuilder->checkbox('foo', 'foobar', true); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + } + + + public function testFormRadio() + { + $form1 = $this->formBuilder->input('radio', 'foo'); + $form2 = $this->formBuilder->radio('foo'); + $form3 = $this->formBuilder->radio('foo', 'foobar', true); + $form4 = $this->formBuilder->radio('foo', 'foobar', false, ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + } + + + public function testFormRadioRepopulation() + { + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + + $session->shouldReceive('getOldInput')->with('radio')->andReturn(1); + + $radio1 = $this->formBuilder->radio('radio', 1); + $radio2 = $this->formBuilder->radio('radio', 2, true); + + $this->assertEquals('', $radio1); + $this->assertEquals('', $radio2); + } + + + public function testFormSubmit() + { + $form1 = $this->formBuilder->submit('foo'); + $form2 = $this->formBuilder->submit('foo', ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + } + + + public function testFormButton() + { + $form1 = $this->formBuilder->button('foo'); + $form2 = $this->formBuilder->button('foo', ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + } + + + public function testResetInput() + { + $resetInput = $this->formBuilder->reset('foo'); + $this->assertEquals('', $resetInput); + } + + + public function testImageInput() + { + $url = 'http://laravel.com/'; + $image = $this->formBuilder->image($url); + + $this->assertEquals('', $image); + } + + protected function setModel(array $data, $object = true) + { + if ($object) $data = new FormBuilderModelStub($data); + + $this->formBuilder->model($data, ['method' => 'GET']); + } +} + +class FormBuilderModelStub { + + protected $data; + + public function __construct(array $data = []) + { + foreach ($data as $key => $val) + { + if (is_array($val)) $val = new self($val); + + $this->data[$key] = $val; + } + } + + + public function __get($key) + { + return $this->data[$key]; + } + + + public function __isset($key) + { + return isset($this->data[$key]); + } +} \ No newline at end of file