Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f6701c7c3 | ||
|
|
3dcf2185a1 | ||
|
|
96a2c98a22 | ||
|
|
9931c3e7de | ||
|
|
fffe7ff53d | ||
|
|
382267a4a2 | ||
|
|
98a12b4217 | ||
|
|
c3215d304a | ||
|
|
f620ae4b9c | ||
|
|
f81e0bbe12 | ||
|
|
8956f00475 | ||
|
|
89a9f40956 | ||
|
|
dc15cf8463 | ||
|
|
1add9b421a | ||
|
|
3f474b0bec | ||
|
|
e152172962 | ||
|
|
c47d34b7de | ||
|
|
16264f1c8d | ||
|
|
45fe4e7988 | ||
|
|
0ba1bb0432 | ||
|
|
abec449ed8 | ||
|
|
eb132aed5c | ||
|
|
9f534cb45b | ||
|
|
71b81b7f96 | ||
|
|
bcf0697d7d | ||
|
|
d79805ade4 | ||
|
|
24ee0aadc0 | ||
|
|
0fc25932db | ||
|
|
92a4833e11 | ||
|
|
f6d9a66624 | ||
|
|
0d1d1fcd5a | ||
|
|
f9a4e185f4 | ||
|
|
4d5ed8a59a | ||
|
|
0625f71fd9 | ||
|
|
a3d42da280 | ||
|
|
d65deb4e77 | ||
|
|
81c9ee7a7b | ||
|
|
068dfb348d | ||
|
|
02c75dff24 | ||
|
|
9dc27a4188 | ||
|
|
2b67d708ce | ||
|
|
bdc56f0239 | ||
|
|
6b68b0856c | ||
|
|
df8acc24bb | ||
|
|
3a312d39ff | ||
|
|
fa2628d311 | ||
|
|
e5011d337c | ||
|
|
0e8e2dbc67 | ||
|
|
466aabedbd | ||
|
|
ef11b7dd2f |
No files matched your search
@@ -3,6 +3,7 @@ language: php
|
|||||||
php:
|
php:
|
||||||
- 5.5
|
- 5.5
|
||||||
- 5.6
|
- 5.6
|
||||||
|
- 7.0
|
||||||
- hhvm
|
- hhvm
|
||||||
|
|
||||||
sudo: false
|
sudo: false
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ trait Componentable
|
|||||||
$default = null;
|
$default = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$data[$variable] = array_get($arguments, $i) ?: $default;
|
$data[$variable] = array_get($arguments, $i, $default);
|
||||||
|
|
||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ trait FormAccessible
|
|||||||
return $this->mutateFormAttribute($key, $value);
|
return $this->mutateFormAttribute($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $value;
|
// No form mutator, let the model resolve this
|
||||||
|
return data_get($this, $key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Executable → Regular
+43
-17
@@ -4,17 +4,16 @@ namespace Collective\Html;
|
|||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use BadMethodCallException;
|
use BadMethodCallException;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\HtmlString;
|
use Illuminate\Support\HtmlString;
|
||||||
use Illuminate\Contracts\View\Factory;
|
use Illuminate\Contracts\View\Factory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
|
||||||
use Illuminate\Session\SessionInterface;
|
use Illuminate\Session\SessionInterface;
|
||||||
use Illuminate\Support\Traits\Macroable;
|
use Illuminate\Support\Traits\Macroable;
|
||||||
use Illuminate\Contracts\Routing\UrlGenerator;
|
use Illuminate\Contracts\Routing\UrlGenerator;
|
||||||
|
|
||||||
class FormBuilder
|
class FormBuilder
|
||||||
{
|
{
|
||||||
|
|
||||||
use Macroable, Componentable {
|
use Macroable, Componentable {
|
||||||
Macroable::__call as macroCall;
|
Macroable::__call as macroCall;
|
||||||
Componentable::__call as componentCall;
|
Componentable::__call as componentCall;
|
||||||
@@ -69,6 +68,8 @@ class FormBuilder
|
|||||||
*/
|
*/
|
||||||
protected $labels = [];
|
protected $labels = [];
|
||||||
|
|
||||||
|
protected $request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The reserved form open attributes.
|
* The reserved form open attributes.
|
||||||
*
|
*
|
||||||
@@ -98,12 +99,13 @@ class FormBuilder
|
|||||||
* @param \Illuminate\Contracts\View\Factory $view
|
* @param \Illuminate\Contracts\View\Factory $view
|
||||||
* @param string $csrfToken
|
* @param string $csrfToken
|
||||||
*/
|
*/
|
||||||
public function __construct(HtmlBuilder $html, UrlGenerator $url, Factory $view, $csrfToken)
|
public function __construct(HtmlBuilder $html, UrlGenerator $url, Factory $view, $csrfToken, Request $request = null)
|
||||||
{
|
{
|
||||||
$this->url = $url;
|
$this->url = $url;
|
||||||
$this->html = $html;
|
$this->html = $html;
|
||||||
$this->view = $view;
|
$this->view = $view;
|
||||||
$this->csrfToken = $csrfToken;
|
$this->csrfToken = $csrfToken;
|
||||||
|
$this->request = $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -211,16 +213,21 @@ class FormBuilder
|
|||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @param array $options
|
* @param array $options
|
||||||
|
* @param bool $escape_html
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Support\HtmlString
|
* @return \Illuminate\Support\HtmlString
|
||||||
*/
|
*/
|
||||||
public function label($name, $value = null, $options = [])
|
public function label($name, $value = null, $options = [], $escape_html = true)
|
||||||
{
|
{
|
||||||
$this->labels[] = $name;
|
$this->labels[] = $name;
|
||||||
|
|
||||||
$options = $this->html->attributes($options);
|
$options = $this->html->attributes($options);
|
||||||
|
|
||||||
$value = e($this->formatLabel($name, $value));
|
$value = $this->formatLabel($name, $value);
|
||||||
|
|
||||||
|
if ($escape_html) {
|
||||||
|
$value = $this->html->entities($value);
|
||||||
|
}
|
||||||
|
|
||||||
return $this->toHtmlString('<label for="' . $name . '"' . $options . '>' . $value . '</label>');
|
return $this->toHtmlString('<label for="' . $name . '"' . $options . '>' . $value . '</label>');
|
||||||
}
|
}
|
||||||
@@ -709,7 +716,7 @@ class FormBuilder
|
|||||||
protected function getSelectedValue($value, $selected)
|
protected function getSelectedValue($value, $selected)
|
||||||
{
|
{
|
||||||
if (is_array($selected)) {
|
if (is_array($selected)) {
|
||||||
return in_array($value, $selected) ? 'selected' : null;
|
return in_array($value, $selected, true) ? 'selected' : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ((string) $value == (string) $selected) ? 'selected' : null;
|
return ((string) $value == (string) $selected) ? 'selected' : null;
|
||||||
@@ -806,11 +813,13 @@ class FormBuilder
|
|||||||
*/
|
*/
|
||||||
protected function getCheckboxCheckedState($name, $value, $checked)
|
protected function getCheckboxCheckedState($name, $value, $checked)
|
||||||
{
|
{
|
||||||
if (isset($this->session) && ! $this->oldInputIsEmpty() && is_null($this->old($name))) {
|
$request = $this->request($name);
|
||||||
|
|
||||||
|
if (isset($this->session) && ! $this->oldInputIsEmpty() && is_null($this->old($name)) && !$request) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->missingOldAndModel($name)) {
|
if ($this->missingOldAndModel($name) && !$request) {
|
||||||
return $checked;
|
return $checked;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -836,7 +845,9 @@ class FormBuilder
|
|||||||
*/
|
*/
|
||||||
protected function getRadioCheckedState($name, $value, $checked)
|
protected function getRadioCheckedState($name, $value, $checked)
|
||||||
{
|
{
|
||||||
if ($this->missingOldAndModel($name)) {
|
$request = $this->request($name);
|
||||||
|
|
||||||
|
if ($this->missingOldAndModel($name) && !$request) {
|
||||||
return $checked;
|
return $checked;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1081,10 +1092,15 @@ class FormBuilder
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! is_null($this->old($name))) {
|
if (! is_null($this->old($name)) && $name != '_method') {
|
||||||
return $this->old($name);
|
return $this->old($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$request = $this->request($name);
|
||||||
|
if (!is_null($request)) {
|
||||||
|
return $request;
|
||||||
|
}
|
||||||
|
|
||||||
if (! is_null($value)) {
|
if (! is_null($value)) {
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
@@ -1094,6 +1110,20 @@ class FormBuilder
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get value from current Request
|
||||||
|
* @param $name
|
||||||
|
* @return array|null|string
|
||||||
|
*/
|
||||||
|
protected function request($name)
|
||||||
|
{
|
||||||
|
if (!isset($this->request)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->request->input($this->transformKey($name));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the model value that should be assigned to the field.
|
* Get the model value that should be assigned to the field.
|
||||||
*
|
*
|
||||||
@@ -1104,7 +1134,7 @@ class FormBuilder
|
|||||||
protected function getModelValueAttribute($name)
|
protected function getModelValueAttribute($name)
|
||||||
{
|
{
|
||||||
if (method_exists($this->model, 'getFormValue')) {
|
if (method_exists($this->model, 'getFormValue')) {
|
||||||
return $this->model->getFormValue($name);
|
return $this->model->getFormValue($this->transformKey($name));
|
||||||
}
|
}
|
||||||
|
|
||||||
return data_get($this->model, $this->transformKey($name));
|
return data_get($this->model, $this->transformKey($name));
|
||||||
@@ -1194,16 +1224,12 @@ class FormBuilder
|
|||||||
*/
|
*/
|
||||||
public function __call($method, $parameters)
|
public function __call($method, $parameters)
|
||||||
{
|
{
|
||||||
try {
|
if (static::hasComponent($method)) {
|
||||||
return $this->componentCall($method, $parameters);
|
return $this->componentCall($method, $parameters);
|
||||||
} catch (BadMethodCallException $e) {
|
|
||||||
//
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (static::hasMacro($method)) {
|
||||||
return $this->macroCall($method, $parameters);
|
return $this->macroCall($method, $parameters);
|
||||||
} catch (BadMethodCallException $e) {
|
|
||||||
//
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new BadMethodCallException("Method {$method} does not exist.");
|
throw new BadMethodCallException("Method {$method} does not exist.");
|
||||||
|
|||||||
+39
-15
@@ -10,7 +10,6 @@ use Illuminate\Contracts\Routing\UrlGenerator;
|
|||||||
|
|
||||||
class HtmlBuilder
|
class HtmlBuilder
|
||||||
{
|
{
|
||||||
|
|
||||||
use Macroable, Componentable {
|
use Macroable, Componentable {
|
||||||
Macroable::__call as macroCall;
|
Macroable::__call as macroCall;
|
||||||
Componentable::__call as componentCall;
|
Componentable::__call as componentCall;
|
||||||
@@ -147,10 +146,11 @@ class HtmlBuilder
|
|||||||
* @param string $title
|
* @param string $title
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param bool $secure
|
* @param bool $secure
|
||||||
|
* @param bool $escape
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Support\HtmlString
|
* @return \Illuminate\Support\HtmlString
|
||||||
*/
|
*/
|
||||||
public function link($url, $title = null, $attributes = [], $secure = null)
|
public function link($url, $title = null, $attributes = [], $secure = null, $escape = true)
|
||||||
{
|
{
|
||||||
$url = $this->url->to($url, [], $secure);
|
$url = $this->url->to($url, [], $secure);
|
||||||
|
|
||||||
@@ -158,7 +158,11 @@ class HtmlBuilder
|
|||||||
$title = $url;
|
$title = $url;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->toHtmlString('<a href="' . $url . '"' . $this->attributes($attributes) . '>' . $this->entities($title) . '</a>');
|
if ($escape) {
|
||||||
|
$title = $this->entities($title);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->toHtmlString('<a href="' . $url . '"' . $this->attributes($attributes) . '>' . $title . '</a>');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -242,18 +246,23 @@ class HtmlBuilder
|
|||||||
* @param string $email
|
* @param string $email
|
||||||
* @param string $title
|
* @param string $title
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
|
* @param bool $escape
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Support\HtmlString
|
* @return \Illuminate\Support\HtmlString
|
||||||
*/
|
*/
|
||||||
public function mailto($email, $title = null, $attributes = [])
|
public function mailto($email, $title = null, $attributes = [], $escape = true)
|
||||||
{
|
{
|
||||||
$email = $this->email($email);
|
$email = $this->email($email);
|
||||||
|
|
||||||
$title = $title ?: $email;
|
$title = $title ?: $email;
|
||||||
|
|
||||||
|
if ($escape) {
|
||||||
|
$title = $this->entities($title);
|
||||||
|
}
|
||||||
|
|
||||||
$email = $this->obfuscate('mailto:') . $email;
|
$email = $this->obfuscate('mailto:') . $email;
|
||||||
|
|
||||||
return $this->toHtmlString('<a href="' . $email . '"' . $this->attributes($attributes) . '>' . $this->entities($title) . '</a>');
|
return $this->toHtmlString('<a href="' . $email . '"' . $this->attributes($attributes) . '>' . $title . '</a>');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -268,6 +277,18 @@ class HtmlBuilder
|
|||||||
return str_replace('@', '@', $this->obfuscate($email));
|
return str_replace('@', '@', $this->obfuscate($email));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates non-breaking space entities based on number supplied.
|
||||||
|
*
|
||||||
|
* @param int $num
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function nbsp($num = 1)
|
||||||
|
{
|
||||||
|
return str_repeat(' ', $num);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate an ordered list of items.
|
* Generate an ordered list of items.
|
||||||
*
|
*
|
||||||
@@ -420,11 +441,18 @@ class HtmlBuilder
|
|||||||
*/
|
*/
|
||||||
protected function attributeElement($key, $value)
|
protected function attributeElement($key, $value)
|
||||||
{
|
{
|
||||||
// For numeric keys we will assume that the key and the value are the same
|
// For numeric keys we will assume that the value is a boolean attribute
|
||||||
// as this will convert HTML attributes such as "required" to a correct
|
// where the presence of the attribute represents a true value and the
|
||||||
// form like required="required" instead of using incorrect numerics.
|
// absence represents a false value.
|
||||||
|
// This will convert HTML attributes such as "required" to a correct
|
||||||
|
// form instead of using incorrect numerics.
|
||||||
if (is_numeric($key)) {
|
if (is_numeric($key)) {
|
||||||
$key = $value;
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Treat boolean attributes as HTML properties
|
||||||
|
if (is_bool($value) && $key != 'value') {
|
||||||
|
return $value ? $key : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! is_null($value)) {
|
if (! is_null($value)) {
|
||||||
@@ -525,16 +553,12 @@ class HtmlBuilder
|
|||||||
*/
|
*/
|
||||||
public function __call($method, $parameters)
|
public function __call($method, $parameters)
|
||||||
{
|
{
|
||||||
try {
|
if (static::hasComponent($method)) {
|
||||||
return $this->componentCall($method, $parameters);
|
return $this->componentCall($method, $parameters);
|
||||||
} catch (BadMethodCallException $e) {
|
|
||||||
//
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (static::hasMacro($method)) {
|
||||||
return $this->macroCall($method, $parameters);
|
return $this->macroCall($method, $parameters);
|
||||||
} catch (BadMethodCallException $e) {
|
|
||||||
//
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new BadMethodCallException("Method {$method} does not exist.");
|
throw new BadMethodCallException("Method {$method} does not exist.");
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ class HtmlServiceProvider extends ServiceProvider
|
|||||||
protected function registerFormBuilder()
|
protected function registerFormBuilder()
|
||||||
{
|
{
|
||||||
$this->app->singleton('form', function ($app) {
|
$this->app->singleton('form', function ($app) {
|
||||||
$form = new FormBuilder($app['html'], $app['url'], $app['view'], $app['session.store']->getToken());
|
$form = new FormBuilder($app['html'], $app['url'], $app['view'], $app['session.store']->getToken(), $app['request']);
|
||||||
|
|
||||||
return $form->setSessionStore($app['session.store']);
|
return $form->setSessionStore($app['session.store']);
|
||||||
});
|
});
|
||||||
|
|||||||
+3
-2
@@ -8,12 +8,13 @@ if (! function_exists('link_to')) {
|
|||||||
* @param string $title
|
* @param string $title
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param bool $secure
|
* @param bool $secure
|
||||||
|
* @param bool $escape
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function link_to($url, $title = null, $attributes = [], $secure = null)
|
function link_to($url, $title = null, $attributes = [], $secure = null, $escape = true)
|
||||||
{
|
{
|
||||||
return app('html')->link($url, $title, $attributes, $secure);
|
return app('html')->link($url, $title, $attributes, $secure, $escape);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ use Mockery as m;
|
|||||||
|
|
||||||
class FormAccessibleTest extends PHPUnit_Framework_TestCase
|
class FormAccessibleTest extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
Capsule::table('models')->truncate();
|
Capsule::table('models')->truncate();
|
||||||
@@ -25,6 +24,11 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->modelData = [
|
$this->modelData = [
|
||||||
'string' => 'abcdefghijklmnop',
|
'string' => 'abcdefghijklmnop',
|
||||||
'email' => 'tj@tjshafer.com',
|
'email' => 'tj@tjshafer.com',
|
||||||
|
'address' => [
|
||||||
|
'street' => 'abcde st'
|
||||||
|
],
|
||||||
|
'array' => [1, 2, 3,],
|
||||||
|
'transform_key' => 'testing testing',
|
||||||
'created_at' => $this->now,
|
'created_at' => $this->now,
|
||||||
'updated_at' => $this->now,
|
'updated_at' => $this->now,
|
||||||
];
|
];
|
||||||
@@ -44,6 +48,37 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($model->getFormValue('created_at'), $this->now->timestamp);
|
$this->assertEquals($model->getFormValue('created_at'), $this->now->timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testItCanGetRelatedValueForForms()
|
||||||
|
{
|
||||||
|
$model = new ModelThatUsesForms($this->modelData);
|
||||||
|
$this->assertEquals($model->getFormValue('address.street'), 'abcde st');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItCanUseGetAccessorValuesWhenThereAreNoFormAccessors()
|
||||||
|
{
|
||||||
|
$model = new ModelThatUsesForms($this->modelData);
|
||||||
|
$this->formBuilder->setModel($model);
|
||||||
|
|
||||||
|
$this->assertEquals($this->formBuilder->getValueAttribute('email'), 'mutated@tjshafer.com');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItReturnsSameResultWithAndWithoutThisFeature()
|
||||||
|
{
|
||||||
|
$modelWithAccessor = new ModelThatUsesForms($this->modelData);
|
||||||
|
$modelWithoutAccessor = new ModelThatDoesntUseForms($this->modelData);
|
||||||
|
|
||||||
|
$this->formBuilder->setModel($modelWithAccessor);
|
||||||
|
$valuesWithAccessor[] = $this->formBuilder->getValueAttribute('array');
|
||||||
|
$valuesWithAccessor[] = $this->formBuilder->getValueAttribute('array[0]');
|
||||||
|
$valuesWithAccessor[] = $this->formBuilder->getValueAttribute('transform.key');
|
||||||
|
$this->formBuilder->setModel($modelWithoutAccessor);
|
||||||
|
$valuesWithoutAccessor[] = $this->formBuilder->getValueAttribute('array');
|
||||||
|
$valuesWithoutAccessor[] = $this->formBuilder->getValueAttribute('array[0]');
|
||||||
|
$valuesWithoutAccessor[] = $this->formBuilder->getValueAttribute('transform.key');
|
||||||
|
|
||||||
|
$this->assertEquals($valuesWithAccessor, $valuesWithoutAccessor);
|
||||||
|
}
|
||||||
|
|
||||||
public function testItCanStillMutateValuesForViews()
|
public function testItCanStillMutateValuesForViews()
|
||||||
{
|
{
|
||||||
$model = new ModelThatUsesForms($this->modelData);
|
$model = new ModelThatUsesForms($this->modelData);
|
||||||
@@ -65,7 +100,6 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
class ModelThatUsesForms extends Model
|
class ModelThatUsesForms extends Model
|
||||||
{
|
{
|
||||||
|
|
||||||
use FormAccessible;
|
use FormAccessible;
|
||||||
|
|
||||||
protected $table = 'models';
|
protected $table = 'models';
|
||||||
@@ -89,11 +123,15 @@ class ModelThatUsesForms extends Model
|
|||||||
{
|
{
|
||||||
return '1 second ago';
|
return '1 second ago';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getEmailAttribute($value)
|
||||||
|
{
|
||||||
|
return 'mutated@tjshafer.com';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ModelThatDoesntUseForms extends Model
|
class ModelThatDoesntUseForms extends Model
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $table = 'models';
|
protected $table = 'models';
|
||||||
|
|
||||||
public function getStringAttribute($value)
|
public function getStringAttribute($value)
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ use Mockery as m;
|
|||||||
|
|
||||||
class FormBuilderTest extends PHPUnit_Framework_TestCase
|
class FormBuilderTest extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var FormBuilder
|
||||||
|
*/
|
||||||
|
protected $formBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup the test environment.
|
* Setup the test environment.
|
||||||
@@ -20,7 +24,20 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET'));
|
$this->urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET'));
|
||||||
$this->viewFactory = m::mock(Factory::class);
|
$this->viewFactory = m::mock(Factory::class);
|
||||||
$this->htmlBuilder = new HtmlBuilder($this->urlGenerator, $this->viewFactory);
|
$this->htmlBuilder = new HtmlBuilder($this->urlGenerator, $this->viewFactory);
|
||||||
$this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, $this->viewFactory, 'abc');
|
|
||||||
|
// prepare request for test with some data
|
||||||
|
$request = Request::create('/foo', 'GET', [
|
||||||
|
"person" => [
|
||||||
|
"name" => "John",
|
||||||
|
"surname" => "Doe"
|
||||||
|
],
|
||||||
|
"aggree" => 1,
|
||||||
|
"checkbox_array" => [1,2,3]
|
||||||
|
]);
|
||||||
|
|
||||||
|
$request = Request::createFromBase($request);
|
||||||
|
|
||||||
|
$this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, $this->viewFactory, 'abc', $request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,6 +48,29 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
m::close();
|
m::close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRequestValue()
|
||||||
|
{
|
||||||
|
$name = $this->formBuilder->text("person[name]");
|
||||||
|
$surname = $this->formBuilder->text("person[surname]");
|
||||||
|
$this->assertEquals('<input name="person[name]" type="text" value="John">', $name);
|
||||||
|
$this->assertEquals('<input name="person[surname]" type="text" value="Doe">', $surname);
|
||||||
|
|
||||||
|
$checked = $this->formBuilder->checkbox("aggree", 1);
|
||||||
|
$unchecked = $this->formBuilder->checkbox("no_value", 1);
|
||||||
|
$this->assertEquals('<input checked="checked" name="aggree" type="checkbox" value="1">', $checked);
|
||||||
|
$this->assertEquals('<input name="no_value" type="checkbox" value="1">', $unchecked);
|
||||||
|
|
||||||
|
$checked_array = $this->formBuilder->checkbox("checkbox_array[]", 1);
|
||||||
|
$unchecked_array = $this->formBuilder->checkbox("checkbox_array[]", 4);
|
||||||
|
$this->assertEquals('<input checked="checked" name="checkbox_array[]" type="checkbox" value="1">', $checked_array);
|
||||||
|
$this->assertEquals('<input name="checkbox_array[]" type="checkbox" value="4">', $unchecked_array);
|
||||||
|
|
||||||
|
$checked = $this->formBuilder->radio("aggree", 1);
|
||||||
|
$unchecked = $this->formBuilder->radio("no_value", 1);
|
||||||
|
$this->assertEquals('<input checked="checked" name="aggree" type="radio" value="1">', $checked);
|
||||||
|
$this->assertEquals('<input name="no_value" type="radio" value="1">', $unchecked);
|
||||||
|
}
|
||||||
|
|
||||||
public function testOpeningForm()
|
public function testOpeningForm()
|
||||||
{
|
{
|
||||||
$form1 = $this->formBuilder->open(['method' => 'GET']);
|
$form1 = $this->formBuilder->open(['method' => 'GET']);
|
||||||
@@ -58,9 +98,11 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$form1 = $this->formBuilder->label('foo', 'Foobar');
|
$form1 = $this->formBuilder->label('foo', 'Foobar');
|
||||||
$form2 = $this->formBuilder->label('foo', 'Foobar', ['class' => 'control-label']);
|
$form2 = $this->formBuilder->label('foo', 'Foobar', ['class' => 'control-label']);
|
||||||
|
$form3 = $this->formBuilder->label('foo', 'Foobar <i>bar</i>', null, false);
|
||||||
|
|
||||||
$this->assertEquals('<label for="foo">Foobar</label>', $form1);
|
$this->assertEquals('<label for="foo">Foobar</label>', $form1);
|
||||||
$this->assertEquals('<label for="foo" class="control-label">Foobar</label>', $form2);
|
$this->assertEquals('<label for="foo" class="control-label">Foobar</label>', $form2);
|
||||||
|
$this->assertEquals('<label for="foo">Foobar <i>bar</i></label>', $form3);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFormInput()
|
public function testFormInput()
|
||||||
@@ -68,10 +110,14 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
$form1 = $this->formBuilder->input('text', 'foo');
|
$form1 = $this->formBuilder->input('text', 'foo');
|
||||||
$form2 = $this->formBuilder->input('text', 'foo', 'foobar');
|
$form2 = $this->formBuilder->input('text', 'foo', 'foobar');
|
||||||
$form3 = $this->formBuilder->input('date', 'foobar', null, ['class' => 'span2']);
|
$form3 = $this->formBuilder->input('date', 'foobar', null, ['class' => 'span2']);
|
||||||
|
$form4 = $this->formBuilder->input('hidden', 'foo', true);
|
||||||
|
$form6 = $this->formBuilder->input('checkbox', 'foo-check', true);
|
||||||
|
|
||||||
$this->assertEquals('<input name="foo" type="text">', $form1);
|
$this->assertEquals('<input name="foo" type="text">', $form1);
|
||||||
$this->assertEquals('<input name="foo" type="text" value="foobar">', $form2);
|
$this->assertEquals('<input name="foo" type="text" value="foobar">', $form2);
|
||||||
$this->assertEquals('<input class="span2" name="foobar" type="date">', $form3);
|
$this->assertEquals('<input class="span2" name="foobar" type="date">', $form3);
|
||||||
|
$this->assertEquals('<input name="foo" type="hidden" value="1">', $form4);
|
||||||
|
$this->assertEquals('<input name="foo-check" type="checkbox" value="1">', $form6);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPasswordsNotFilled()
|
public function testPasswordsNotFilled()
|
||||||
@@ -261,6 +307,16 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($select,
|
$this->assertEquals($select,
|
||||||
'<select name="size"><option value="L" selected="selected">Large</option><option value="S">Small</option></select>');
|
'<select name="size"><option value="L" selected="selected">Large</option><option value="S">Small</option></select>');
|
||||||
|
|
||||||
|
$select = $this->formBuilder->select(
|
||||||
|
'size',
|
||||||
|
['0' => 'All Sizes', 'L' => 'Large', 'M' => 'Medium', 'S' => 'Small'],
|
||||||
|
['M'],
|
||||||
|
['multiple']
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
$select,
|
||||||
|
'<select multiple name="size"><option value="0">All Sizes</option><option value="L">Large</option><option value="M" selected="selected">Medium</option><option value="S">Small</option></select>');
|
||||||
|
|
||||||
$select = $this->formBuilder->select(
|
$select = $this->formBuilder->select(
|
||||||
'size',
|
'size',
|
||||||
['L' => 'Large', 'S' => 'Small'],
|
['L' => 'Large', 'S' => 'Small'],
|
||||||
@@ -278,6 +334,27 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
['name' => 'select-name']
|
['name' => 'select-name']
|
||||||
);
|
);
|
||||||
$this->assertEquals($select, '<select name="select-name" id="select-name-id"></select>');
|
$this->assertEquals($select, '<select name="select-name" id="select-name-id"></select>');
|
||||||
|
|
||||||
|
$select = $this->formBuilder->select(
|
||||||
|
'size',
|
||||||
|
[
|
||||||
|
'Large sizes' => [
|
||||||
|
'L' => 'Large',
|
||||||
|
'XL' => 'Extra Large',
|
||||||
|
],
|
||||||
|
'S' => 'Small',
|
||||||
|
],
|
||||||
|
null,
|
||||||
|
[
|
||||||
|
'class' => 'class-name',
|
||||||
|
'id' => 'select-id',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
$select,
|
||||||
|
'<select class="class-name" id="select-id" name="size"><optgroup label="Large sizes"><option value="L">Large</option><option value="XL">Extra Large</option></optgroup><option value="S">Small</option></select>'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFormSelectRepopulation()
|
public function testFormSelectRepopulation()
|
||||||
@@ -499,6 +576,15 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('<input class="span2" name="foo" type="color">', $form3);
|
$this->assertEquals('<input class="span2" name="foo" type="color">', $form3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testBooleanAttributes()
|
||||||
|
{
|
||||||
|
$input = $this->formBuilder->text('test', null, ['disabled']);
|
||||||
|
$this->assertEquals('<input disabled name="test" type="text">', $input);
|
||||||
|
|
||||||
|
$input = $this->formBuilder->textarea('test', null, ['readonly']);
|
||||||
|
$this->assertEquals('<textarea readonly name="test" cols="50" rows="10"></textarea>', $input);
|
||||||
|
}
|
||||||
|
|
||||||
protected function setModel(array $data, $object = true)
|
protected function setModel(array $data, $object = true)
|
||||||
{
|
{
|
||||||
if ($object) {
|
if ($object) {
|
||||||
@@ -511,7 +597,6 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
class FormBuilderModelStub
|
class FormBuilderModelStub
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $data;
|
protected $data;
|
||||||
|
|
||||||
public function __construct(array $data = [])
|
public function __construct(array $data = [])
|
||||||
|
|||||||
@@ -89,4 +89,41 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertTrue($this->htmlBuilder->hasComponent('tweet'));
|
$this->assertTrue($this->htmlBuilder->hasComponent('tweet'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testLink()
|
||||||
|
{
|
||||||
|
$result1 = $this->htmlBuilder->link("http://www.example.com", "<span>Example.com</span>", ["class" => "example-link"], null, true);
|
||||||
|
|
||||||
|
$result2 = $this->htmlBuilder->link("http://www.example.com", "<span>Example.com</span>", ["class" => "example-link"], null, false);
|
||||||
|
|
||||||
|
$this->assertEquals('<a href="http://www.example.com" class="example-link"><span>Example.com</span></a>', $result1);
|
||||||
|
$this->assertEquals('<a href="http://www.example.com" class="example-link"><span>Example.com</span></a>', $result2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMailto()
|
||||||
|
{
|
||||||
|
$htmlBuilder = m::mock('Collective\Html\HtmlBuilder[obfuscate,email]', [$this->urlGenerator, $this->viewFactory]);
|
||||||
|
$htmlBuilder->shouldReceive('obfuscate', 'email')->andReturnUsing(function () {
|
||||||
|
$args = func_get_args();
|
||||||
|
return $args[0];
|
||||||
|
});
|
||||||
|
|
||||||
|
$result1 = $htmlBuilder->mailto("person@example.com", "<span>First Name Last</span>", ["class" => "example-link"], true);
|
||||||
|
|
||||||
|
$result2 = $htmlBuilder->mailto("person@example.com", "<span>First Name Last</span>", ["class" => "example-link"], false);
|
||||||
|
|
||||||
|
$this->assertEquals('<a href="mailto:person@example.com" class="example-link"><span>First Name Last</span></a>', $result1);
|
||||||
|
$this->assertEquals('<a href="mailto:person@example.com" class="example-link"><span>First Name Last</span></a>', $result2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBooleanAttributes()
|
||||||
|
{
|
||||||
|
$result1 = $this->htmlBuilder->attributes(['my-property' => true]);
|
||||||
|
|
||||||
|
$result2 = $this->htmlBuilder->attributes(['my-property' => false]);
|
||||||
|
|
||||||
|
$this->assertEquals('my-property', trim($result1));
|
||||||
|
|
||||||
|
$this->assertEquals('', trim($result2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user