From f306f8e36dbc48e0835847d26d0789e737d50ec8 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Wed, 19 Feb 2020 17:57:57 +0100 Subject: [PATCH] Updating the package --- .editorconfig | 10 +- .gitattributes | 1 - .scrutinizer.yml | 2 +- .travis.yml | 25 - LICENSE.md | 2 +- README.md | 6 +- composer.json | 8 +- helpers.php | 24 +- src/AbstractBuilder.php | 10 +- src/Contracts/FormBuilder.php | 102 +- src/Contracts/HtmlBuilder.php | 110 +- src/FormBuilder.php | 195 ++-- src/Helpers/Obfuscater.php | 10 +- src/HtmlBuilder.php | 135 ++- src/HtmlServiceProvider.php | 15 +- src/Traits/Componentable.php | 14 +- src/Traits/FormAccessible.php | 18 +- tests/FormBuilderTest.php | 995 +++++++++--------- tests/HtmlBuilderTest.php | 70 +- tests/HtmlServiceProviderTest.php | 15 +- tests/Stubs/DummyController.php | 6 +- tests/Stubs/FormBuilderModelStub.php | 6 +- tests/Stubs/ModelThatDoesntUseForms.php | 6 +- tests/Stubs/ModelThatUsesForms.php | 6 +- tests/TestCase.php | 50 +- tests/Traits/FormAccessible.php | 30 +- .../2015_01_01_000001_create_models_table.php | 8 +- 27 files changed, 976 insertions(+), 903 deletions(-) delete mode 100644 .travis.yml diff --git a/.editorconfig b/.editorconfig index 042d009..6537ca4 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,15 +1,15 @@ -# This file is for unifying the coding style for different editors and IDEs -# editorconfig.org - root = true [*] -end_of_line = lf charset = utf-8 +end_of_line = lf +insert_final_newline = true indent_style = space indent_size = 4 trim_trailing_whitespace = true -insert_final_newline = true [*.md] +trim_trailing_whitespace = false + +[*.{yml,yaml}] indent_size = 2 diff --git a/.gitattributes b/.gitattributes index 7ccb1f3..22f739a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -7,6 +7,5 @@ .gitattributes export-ignore .gitignore export-ignore .scrutinizer.yml export-ignore -.travis.yml export-ignore phpunit.xml.dist export-ignore CONTRIBUTING.md export-ignore diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 2880858..a34f2cb 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -23,7 +23,7 @@ checks: tools: external_code_coverage: timeout: 600 - runs: 2 + runs: 6 php_code_sniffer: enabled: true config: diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index d407e33..0000000 --- a/.travis.yml +++ /dev/null @@ -1,25 +0,0 @@ -language: php - -sudo: false - -php: - - 7.2 - - 7.3 - - nightly - -matrix: - allow_failures: - - php: nightly - -before_script: - - travis_retry composer self-update - - travis_retry composer install --prefer-source --no-interaction - -script: - - composer validate - - mkdir -p build/logs - - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover - -after_script: - - if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi - - if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi diff --git a/LICENSE.md b/LICENSE.md index d1301af..c1f09d5 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016-2019 ARCANEDEV - LaravelHtml +Copyright (c) 2016-2020 ARCANEDEV - LaravelHtml Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index cb1e301..d77a169 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Laravel HTML & Forms [![Packagist License][badge_license]](LICENSE.md) [![For Laravel][badge_laravel]][link-github-repo] -[![Travis Status][badge_build]][link-travis] +[![Github Workflow Status][badge_build]][link-github-status] [![Coverage Status][badge_coverage]][link-scrutinizer] [![Scrutinizer Code Quality][badge_quality]][link-scrutinizer] [![SensioLabs Insight][badge_insight]][link-insight] @@ -48,7 +48,7 @@ If you discover any security related issues, please email arcanedev.maroc@gmail. [badge_laravel]: https://img.shields.io/badge/Laravel-5.1%20to%206.x-orange.svg?style=flat-square [badge_license]: https://img.shields.io/packagist/l/arcanedev/laravel-html.svg?style=flat-square -[badge_build]: https://img.shields.io/travis/ARCANEDEV/LaravelHtml.svg?style=flat-square +[badge_build]: https://img.shields.io/github/workflow/status/ARCANEDEV/LaravelHtml/run-tests?style=flat-square [badge_coverage]: https://img.shields.io/scrutinizer/coverage/g/ARCANEDEV/LaravelHtml.svg?style=flat-square [badge_quality]: https://img.shields.io/scrutinizer/g/ARCANEDEV/LaravelHtml.svg?style=flat-square [badge_insight]: https://img.shields.io/sensiolabs/i/dfcc38f2-7302-4c01-90ff-2d6c21782682.svg?style=flat-square @@ -59,9 +59,9 @@ If you discover any security related issues, please email arcanedev.maroc@gmail. [link-author]: https://github.com/arcanedev-maroc [link-github-repo]: https://github.com/ARCANEDEV/LaravelHtml +[link-github-status]: https://github.com/ARCANEDEV/LaravelHtml/actions [link-github-issues]: https://github.com/ARCANEDEV/LaravelHtml/issues [link-contributors]: https://github.com/ARCANEDEV/LaravelHtml/graphs/contributors [link-packagist]: https://packagist.org/packages/arcanedev/laravel-html -[link-travis]: https://travis-ci.org/ARCANEDEV/LaravelHtml [link-scrutinizer]: https://scrutinizer-ci.com/g/ARCANEDEV/LaravelHtml/?branch=master [link-insight]: https://insight.sensiolabs.com/projects/dfcc38f2-7302-4c01-90ff-2d6c21782682 diff --git a/composer.json b/composer.json index 2eab4d1..552448a 100644 --- a/composer.json +++ b/composer.json @@ -15,13 +15,13 @@ "license": "MIT", "require": { "php": ">=7.2.0", - "arcanedev/php-html": "^3.0", - "arcanedev/support": "^5.0" + "arcanedev/php-html": "^3.1", + "arcanedev/support": "^5.1.1" }, "require-dev": { "ext-dom": "*", - "orchestra/testbench": "^4.0", - "mockery/mockery": "^1.0", + "orchestra/testbench": "^4.6", + "mockery/mockery": "^1.3.1", "phpunit/phpcov": "^6.0", "phpunit/phpunit": "^8.0" }, diff --git a/helpers.php b/helpers.php index d178905..887cea9 100644 --- a/helpers.php +++ b/helpers.php @@ -1,7 +1,9 @@ link($url, $title, $attributes, $secure, $escaped); } } @@ -60,8 +59,7 @@ function link_to($url, $title = null, $attributes = [], $secure = null, $escaped * * @return \Illuminate\Support\HtmlString */ - function link_to_asset($url, $title = null, $attributes = [], $secure = null) - { + function link_to_asset(string $url, $title = null, array $attributes = [], $secure = null): HtmlString { return html()->linkAsset($url, $title, $attributes, $secure); } } @@ -78,8 +76,7 @@ function link_to_asset($url, $title = null, $attributes = [], $secure = null) * * @return \Illuminate\Support\HtmlString */ - function link_to_route($name, $title = null, $params = [], $attributes = [], $escaped = true) - { + function link_to_route(string $name, $title = null, array $params = [], array $attributes = [], $escaped = true): HtmlString { return html()->linkRoute($name, $title, $params, $attributes, $escaped); } } @@ -96,8 +93,7 @@ function link_to_route($name, $title = null, $params = [], $attributes = [], $es * * @return \Illuminate\Support\HtmlString */ - function link_to_action($action, $title = null, $params = [], $attributes = [], $escaped = true) - { + function link_to_action(string $action, $title = null, array $params = [], array $attributes = [], $escaped = true): HtmlString { return html()->linkAction($action, $title, $params, $attributes, $escaped); } } diff --git a/src/AbstractBuilder.php b/src/AbstractBuilder.php index 14d8356..988dcf4 100644 --- a/src/AbstractBuilder.php +++ b/src/AbstractBuilder.php @@ -1,4 +1,8 @@ -componentCall($method, $parameters); } catch (BadMethodCallException $e) { - // Continue + return $this->macroCall($method, $parameters); } - - return $this->macroCall($method, $parameters); } } diff --git a/src/Contracts/FormBuilder.php b/src/Contracts/FormBuilder.php index b4aae20..80885e9 100644 --- a/src/Contracts/FormBuilder.php +++ b/src/Contracts/FormBuilder.php @@ -1,6 +1,12 @@ -session; } @@ -128,7 +128,7 @@ public function getSessionStore() * * @param \Illuminate\Contracts\Session\Session $session * - * @return self + * @return $this */ public function setSessionStore(Session $session) { @@ -140,9 +140,9 @@ public function setSessionStore(Session $session) /** * Set the model instance on the form builder. * - * @param \Illuminate\Database\Eloquent\Model|null $model + * @param \Illuminate\Database\Eloquent\Model|mixed|null $model * - * @return self + * @return $this */ public function setModel($model) { @@ -154,7 +154,7 @@ public function setModel($model) /** * Get the model instance on the form builder. * - * @return \Illuminate\Database\Eloquent\Model + * @return \Illuminate\Database\Eloquent\Model|mixed|null */ public function getModel() { @@ -164,17 +164,17 @@ public function getModel() /** * Get the ID attribute for a field name. * - * @param string $name - * @param array $attributes + * @param string|null $name + * @param array $attributes * - * @return string + * @return string|null */ - public function getIdAttribute($name, array $attributes) + public function getIdAttribute($name, array $attributes): ?string { if (array_key_exists('id', $attributes)) return $attributes['id']; - if (in_array($name, $this->labels)) + if ( ! is_null($name) && in_array($name, $this->labels)) return $name; return null; @@ -205,16 +205,16 @@ public function getValueAttribute($name, $value = null) /** * Get the model value that should be assigned to the field. * - * @param string $name - * @param \Illuminate\Database\Eloquent\Model $model + * @param string $name + * @param \Illuminate\Database\Eloquent\Model|mixed|null $model * * @return mixed */ - private function getModelValueAttribute($name, $model = null) + private function getModelValueAttribute(string $name, $model = null) { $model = $model ?: $this->getModel(); - $key = self::transformKey($name); + $key = static::transformKey($name); if (strpos($key, '.') !== false) { $keys = explode('.', $key, 2); @@ -237,11 +237,11 @@ private function getModelValueAttribute($name, $model = null) * * @return mixed */ - public function old($name) + public function old(string $name) { - return ! is_null($this->session) - ? $this->session->getOldInput(self::transformKey($name)) - : null; + $session = $this->getSessionStore(); + + return is_null($session) ? null : $session->getOldInput(static::transformKey($name)); } /** @@ -251,7 +251,7 @@ public function old($name) * * @return string */ - private static function transformKey($key) + private static function transformKey(string $key): string { return str_replace( ['.', '[]', '[', ']'], @@ -265,10 +265,12 @@ private static function transformKey($key) * * @return bool */ - public function oldInputIsEmpty() + public function oldInputIsEmpty(): bool { - return ! is_null($this->session) - && (count($this->session->getOldInput()) === 0); + $session = $this->getSessionStore(); + + return ! is_null($session) + && (count($session->getOldInput()) === 0); } /* ----------------------------------------------------------------- @@ -283,7 +285,7 @@ public function oldInputIsEmpty() * * @return \Illuminate\Support\HtmlString */ - public function open(array $attributes = []) + public function open(array $attributes = []): HtmlString { $method = Str::upper(Arr::pull($attributes, 'method', 'POST')); @@ -314,7 +316,7 @@ public function open(array $attributes = []) * * @return \Illuminate\Support\HtmlString */ - public function model($model, array $attributes = []) + public function model($model, array $attributes = []): HtmlString { return $this->setModel($model)->open($attributes); } @@ -324,7 +326,7 @@ public function model($model, array $attributes = []) * * @return \Illuminate\Support\HtmlString */ - public function close() + public function close(): HtmlString { $this->labels = []; $this->setModel(null); @@ -337,12 +339,13 @@ public function close() * * @return \Arcanedev\Html\Elements\Input */ - public function token() + public function token(): Input { - return $this->hidden( - '_token', - empty($this->csrfToken) ? $this->session->token() : $this->csrfToken - ); + $token = empty($this->csrfToken) + ? $this->getSessionStore()->token() + : $this->csrfToken; + + return $this->hidden('_token', $token); } /** @@ -355,7 +358,7 @@ public function token() * * @return \Arcanedev\Html\Elements\Label */ - public function label($name, $value = null, array $attributes = [], $escaped = true) + public function label(string $name, $value = null, array $attributes = [], $escaped = true): Label { $this->labels[] = $name; @@ -371,21 +374,23 @@ public function label($name, $value = null, array $attributes = [], $escaped = t * Create a form input field. * * @param string $type - * @param string $name + * @param string|null $name * @param string|mixed $value * @param array $attributes * * @return \Arcanedev\Html\Elements\Input */ - public function input($type, $name, $value = null, array $attributes = []) + public function input(string $type, $name, $value = null, array $attributes = []): Input { if ( ! in_array($type, $this->skipValueTypes)) $value = $this->getValueAttribute($name, $value); + $id = $this->getIdAttribute($name, $attributes); + return Input::make() ->type($type) - ->attributeUnless(is_null($name), 'name', $name) - ->attributeUnless(is_null($id = $this->getIdAttribute($name, $attributes)), 'id', $id) + ->attributeIfNotNull($name, 'name', $name) + ->attributeIfNotNull($id, 'id', $id) ->attributeUnless(is_null($value) || empty($value), 'value', $value) ->attributes($attributes); } @@ -399,7 +404,7 @@ public function input($type, $name, $value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - public function text($name, $value = null, array $attributes = []) + public function text(string $name, $value = null, array $attributes = []): Input { return $this->input('text', $name, $value, $attributes); } @@ -412,7 +417,7 @@ public function text($name, $value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - public function password($name, array $attributes = []) + public function password(string $name, array $attributes = []): Input { return $this->input('password', $name, null, $attributes); } @@ -426,7 +431,7 @@ public function password($name, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - public function hidden($name, $value = null, array $attributes = []) + public function hidden(string $name, $value = null, array $attributes = []): Input { return $this->input('hidden', $name, $value, $attributes); } @@ -440,7 +445,7 @@ public function hidden($name, $value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - public function email($name, $value = null, array $attributes = []) + public function email(string $name, $value = null, array $attributes = []): Input { return $this->input('email', $name, $value, $attributes); } @@ -454,7 +459,7 @@ public function email($name, $value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - public function tel($name, $value = null, array $attributes = []) + public function tel(string $name, $value = null, array $attributes = []): Input { return $this->input('tel', $name, $value, $attributes); } @@ -468,7 +473,7 @@ public function tel($name, $value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - public function number($name, $value = null, array $attributes = []) + public function number(string $name, $value = null, array $attributes = []): Input { return $this->input('number', $name, $value, $attributes); } @@ -482,7 +487,7 @@ public function number($name, $value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - public function date($name, $value = null, array $attributes = []) + public function date(string $name, $value = null, array $attributes = []): Input { if ($value instanceof DateTime) $value = $value->format('Y-m-d'); @@ -499,7 +504,7 @@ public function date($name, $value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - public function datetime($name, $value = null, array $attributes = []) + public function datetime(string $name, $value = null, array $attributes = []): Input { if ($value instanceof DateTime) $value = $value->format(DateTime::RFC3339); @@ -516,7 +521,7 @@ public function datetime($name, $value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - public function datetimeLocal($name, $value = null, array $attributes = []) + public function datetimeLocal(string $name, $value = null, array $attributes = []): Input { if ($value instanceof DateTime) $value = $value->format('Y-m-d\TH:i'); @@ -533,7 +538,7 @@ public function datetimeLocal($name, $value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - public function time($name, $value = null, array $attributes = []) + public function time(string $name, $value = null, array $attributes = []): Input { return $this->input('time', $name, $value, $attributes); } @@ -547,7 +552,7 @@ public function time($name, $value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - public function url($name, $value = null, array $attributes = []) + public function url(string $name, $value = null, array $attributes = []): Input { return $this->input('url', $name, $value, $attributes); } @@ -560,7 +565,7 @@ public function url($name, $value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\File */ - public function file($name, array $attributes = []) + public function file(string $name, array $attributes = []): File { return File::make()->name($name)->attributes($attributes); } @@ -574,7 +579,7 @@ public function file($name, array $attributes = []) * * @return \Arcanedev\Html\Elements\Textarea */ - public function textarea($name, $value = null, array $attributes = []) + public function textarea(string $name, $value = null, array $attributes = []): Textarea { $id = $this->getIdAttribute($name, $attributes); $size = Arr::pull($attributes, 'size'); @@ -593,23 +598,23 @@ public function textarea($name, $value = null, array $attributes = []) /** * Create a select box field. * - * @param string $name - * @param array|\Illuminate\Support\Collection $list - * @param string|bool $selected - * @param array $attributes - * @param array $optionsAttributes - * @param array $optgroupsAttributes + * @param string $name + * @param array|\Illuminate\Support\Collection|iterable $list + * @param string|bool $selected + * @param array $attributes + * @param array $optionsAttributes + * @param array $optgroupsAttributes * * @return \Arcanedev\Html\Elements\Select */ public function select( - $name, - $list = [], + string $name, + iterable $list = [], $selected = null, array $attributes = [], array $optionsAttributes = [], array $optgroupsAttributes = [] - ) { + ): Select { return Select::make() ->name($name) ->options($list, $optionsAttributes, $optgroupsAttributes) @@ -629,7 +634,7 @@ public function select( * * @return \Arcanedev\Html\Elements\Select */ - public function selectRange($name, $begin, $end, $selected = null, array $attributes = []) + public function selectRange(string $name, $begin, $end, $selected = null, array $attributes = []): Select { $range = array_combine($range = range($begin, $end), $range); @@ -647,7 +652,7 @@ public function selectRange($name, $begin, $end, $selected = null, array $attrib * * @return \Arcanedev\Html\Elements\Select */ - public function selectYear($name, $begin, $end, $selected = null, array $attributes = []) + public function selectYear(string $name, $begin, $end, $selected = null, array $attributes = []): Select { return $this->selectRange($name, $begin, $end, $selected, $attributes); } @@ -662,7 +667,7 @@ public function selectYear($name, $begin, $end, $selected = null, array $attribu * * @return \Arcanedev\Html\Elements\Select */ - public function selectMonth($name, $selected = null, array $attributes = [], $format = '%B') + public function selectMonth(string $name, $selected = null, array $attributes = [], $format = '%B'): Select { $months = []; @@ -683,7 +688,7 @@ public function selectMonth($name, $selected = null, array $attributes = [], $fo * * @return \Arcanedev\Html\Elements\Input */ - public function checkbox($name, $value = 1, $checked = null, array $attributes = []) + public function checkbox(string $name, $value = 1, $checked = null, array $attributes = []): Input { return $this->checkable('checkbox', $name, $value, $checked, $attributes); } @@ -698,7 +703,7 @@ public function checkbox($name, $value = 1, $checked = null, array $attributes = * * @return \Arcanedev\Html\Elements\Input */ - public function radio($name, $value = null, $checked = null, array $attributes = []) + public function radio(string $name, $value = null, $checked = null, array $attributes = []): Input { return $this->checkable('radio', $name, $value ?: $name, $checked, $attributes); } @@ -711,7 +716,7 @@ public function radio($name, $value = null, $checked = null, array $attributes = * * @return \Arcanedev\Html\Elements\Button */ - public function reset($value, array $attributes = []) + public function reset($value, array $attributes = []): Button { return $this->button($value, array_merge(['type' => 'reset'], $attributes)); } @@ -725,7 +730,7 @@ public function reset($value, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - public function image($url, $name = null, array $attributes = []) + public function image(string $url, $name = null, array $attributes = []): Input { return $this->input('image', $name, null, array_merge($attributes, [ 'src' => $this->url->asset($url), @@ -740,7 +745,7 @@ public function image($url, $name = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Button */ - public function submit($value = null, array $attributes = []) + public function submit($value = null, array $attributes = []): Button { return $this->button($value, array_merge(['type' => 'submit'], $attributes)); } @@ -753,7 +758,7 @@ public function submit($value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Button */ - public function button($value = null, array $attributes = []) + public function button($value = null, array $attributes = []): Button { return Button::make() ->type(Arr::pull($attributes, 'type', 'button')) @@ -770,7 +775,7 @@ public function button($value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - public function color($name, $value = null, array $attributes = []) + public function color(string $name, $value = null, array $attributes = []): Input { return $this->input('color', $name, $value, $attributes); } @@ -791,7 +796,7 @@ public function color($name, $value = null, array $attributes = []) * * @return \Arcanedev\Html\Elements\Input */ - protected function checkable($type, $name, $value, $checked, array $attributes) + protected function checkable(string $type, string $name, $value, $checked, array $attributes): Input { $checked = $this->getCheckedState($type, $name, $value, $checked); @@ -811,7 +816,7 @@ protected function checkable($type, $name, $value, $checked, array $attributes) * * @return bool */ - private function getCheckedState($type, $name, $value, $checked) + private function getCheckedState(string $type, string $name, $value, $checked): bool { switch($type) { case 'checkbox': @@ -834,25 +839,29 @@ private function getCheckedState($type, $name, $value, $checked) * * @return bool */ - private function getCheckboxCheckedState($name, $value, $checked) + private function getCheckboxCheckedState(string $name, $value, $checked): bool { if ( isset($this->session) && ! $this->oldInputIsEmpty() && is_null($this->old($name)) - ) + ) { return false; + } - if ($this->missingOldAndModel($name)) - return $checked; + if ($this->missingOldAndModel($name)) { + return (bool) $checked; + } $posted = $this->getValueAttribute($name, $checked); - if (is_array($posted)) + if (is_array($posted)) { return in_array($value, $posted); + } - if ($posted instanceof Collection) + if ($posted instanceof Collection) { return $posted->contains('id', $value); + } return (bool) $posted; } @@ -866,11 +875,13 @@ private function getCheckboxCheckedState($name, $value, $checked) * * @return bool */ - private function getRadioCheckedState($name, $value, $checked) + private function getRadioCheckedState(string $name, $value, $checked): bool { - return $this->missingOldAndModel($name) - ? $checked - : $this->getValueAttribute($name) === $value; + if ($this->missingOldAndModel($name)) { + return (bool) $checked; + } + + return $this->getValueAttribute($name) === $value; } /** @@ -880,7 +891,7 @@ private function getRadioCheckedState($name, $value, $checked) * * @return bool */ - private function missingOldAndModel($name) + private function missingOldAndModel(string $name): bool { return is_null($this->old($name)) && is_null($this->getModelValueAttribute($name)); @@ -893,7 +904,7 @@ private function missingOldAndModel($name) * * @return string */ - private function getAction(array $attributes) + private function getAction(array $attributes): string { if (isset($attributes['url'])) return $this->getUrlAction($attributes['url']); @@ -914,7 +925,7 @@ private function getAction(array $attributes) * * @return string */ - private function getUrlAction($attribute) + private function getUrlAction($attribute): string { return is_array($attribute) ? $this->url->to($attribute[0], array_slice($attribute, 1)) @@ -928,7 +939,7 @@ private function getUrlAction($attribute) * * @return string */ - private function getRouteAction($attribute) + private function getRouteAction($attribute): string { return is_array($attribute) ? $this->url->route($attribute[0], array_slice($attribute, 1)) @@ -942,7 +953,7 @@ private function getRouteAction($attribute) * * @return string */ - private function getControllerAction($attribute) + private function getControllerAction($attribute): string { return is_array($attribute) ? $this->url->action($attribute[0], array_slice($attribute, 1)) diff --git a/src/Helpers/Obfuscater.php b/src/Helpers/Obfuscater.php index 47a3c95..2b1358e 100644 --- a/src/Helpers/Obfuscater.php +++ b/src/Helpers/Obfuscater.php @@ -1,4 +1,8 @@ -entities($value, false); } /** @@ -77,7 +83,7 @@ public function escape($value) * * @return string */ - public function decode($value) + public function decode(string $value): string { return html_entity_decode($value, ENT_QUOTES, 'UTF-8'); } @@ -91,9 +97,9 @@ public function decode($value) * * @return \Illuminate\Support\HtmlString */ - public function script($url, array $attributes = [], $secure = null) + public function script(string $url, array $attributes = [], ?bool $secure = null): HtmlString { - return Elements\Element::withTag('script') + return Element::withTag('script') ->attribute('src', $this->url->asset($url, $secure)) ->attributes($attributes) ->render(); @@ -108,14 +114,14 @@ public function script($url, array $attributes = [], $secure = null) * * @return \Illuminate\Support\HtmlString */ - public function style($url, array $attributes = [], $secure = null) + public function style(string $url, array $attributes = [], ?bool $secure = null): HtmlString { $attributes = array_merge($attributes, [ 'rel' => 'stylesheet', 'href' => $this->url->asset($url, $secure), ]); - return Elements\Element::withTag('link') + return Element::withTag('link') ->attributes($attributes) ->render(); } @@ -130,9 +136,9 @@ public function style($url, array $attributes = [], $secure = null) * * @return \Illuminate\Support\HtmlString */ - public function image($url, $alt = null, $attributes = [], $secure = null) + public function image(string $url, ?string $alt = null, array $attributes = [], ?bool $secure = null): HtmlString { - return Elements\Img::make() + return Img::make() ->src($this->url->asset($url, $secure)) ->attributeUnless(is_null($alt), 'alt', $alt) ->attributes($attributes) @@ -148,14 +154,14 @@ public function image($url, $alt = null, $attributes = [], $secure = null) * * @return \Illuminate\Support\HtmlString */ - public function favicon($url, array $attributes = [], $secure = null) + public function favicon(string $url, array $attributes = [], ?bool $secure = null): HtmlString { $attributes = array_merge([ 'rel' => 'shortcut icon', 'type' => 'image/x-icon', ], $attributes); - return Elements\Element::withTag('link') + return Element::withTag('link') ->attribute('href', $this->url->asset($url, $secure)) ->attributes($attributes) ->render(); @@ -172,14 +178,14 @@ public function favicon($url, array $attributes = [], $secure = null) * * @return \Illuminate\Support\HtmlString */ - public function link($url, $title = null, array $attributes = [], $secure = null, $escaped = true) + public function link(string $url, ?string $title = null, array $attributes = [], ?bool $secure = null, bool $escaped = true): HtmlString { $url = $this->url->to($url, [], $secure); if (is_null($title) || $title === false) $title = $url; - return Elements\A::make() + return A::make() ->href($this->entities($url)) ->attributes($attributes) ->html($escaped ? $this->entities($title) : $title) @@ -196,7 +202,7 @@ public function link($url, $title = null, array $attributes = [], $secure = null * * @return \Illuminate\Support\HtmlString */ - public function secureLink($url, $title = null, array $attributes = [], $escaped = true) + public function secureLink(string $url, ?string $title = null, array $attributes = [], bool $escaped = true): HtmlString { return $this->link($url, $title, $attributes, true, $escaped); } @@ -211,7 +217,7 @@ public function secureLink($url, $title = null, array $attributes = [], $escaped * * @return \Illuminate\Support\HtmlString */ - public function linkAsset($url, $title = null, array $attributes = [], $secure = null) + public function linkAsset(string $url, ?string $title = null, array $attributes = [], ?bool $secure = null): HtmlString { $url = $this->url->asset($url, $secure); @@ -227,7 +233,7 @@ public function linkAsset($url, $title = null, array $attributes = [], $secure = * * @return \Illuminate\Support\HtmlString */ - public function linkSecureAsset($url, $title = null, array $attributes = []) + public function linkSecureAsset(string $url, ?string $title = null, array $attributes = []): HtmlString { return $this->linkAsset($url, $title, $attributes, true); } @@ -237,14 +243,15 @@ public function linkSecureAsset($url, $title = null, array $attributes = []) * * @param string $name * @param string|null $title - * @param array|mixed $parameters + * @param array $parameters * @param array $attributes * @param bool $escaped * * @return \Illuminate\Support\HtmlString */ - public function linkRoute($name, $title = null, $parameters = [], array $attributes = [], $escaped = true) - { + public function linkRoute( + string $name, ?string $title = null, array $parameters = [], array $attributes = [], bool $escaped = true + ): HtmlString { $url = $this->url->route($name, $parameters); return $this->link($url, $title, $attributes, null, $escaped); @@ -254,15 +261,16 @@ public function linkRoute($name, $title = null, $parameters = [], array $attribu * Generate a HTML link to a controller action. * * @param string $action - * @param string $title - * @param array|mixed $parameters + * @param string|null $title + * @param array $parameters * @param array $attributes * @param bool $escaped * * @return \Illuminate\Support\HtmlString */ - public function linkAction($action, $title = null, $parameters = [], array $attributes = [], $escaped = true) - { + public function linkAction( + string $action, ?string $title = null, array $parameters = [], array $attributes = [], bool $escaped = true + ): HtmlString { $url = $this->url->action($action, $parameters); return $this->link($url, $title, $attributes, null, $escaped); @@ -278,15 +286,15 @@ public function linkAction($action, $title = null, $parameters = [], array $attr * * @return \Illuminate\Support\HtmlString */ - public function mailto($email, $title = null, array $attributes = [], $escaped = true) + public function mailto(string $email, ?string $title = null, array $attributes = [], bool $escaped = true): HtmlString { $email = $this->email($email); $title = $title ?: $email; - return Elements\A::make() + return A::make() ->href($this->obfuscate('mailto:').$email) ->attributes($attributes) - ->html(($escaped ? $this->entities($title) : $title)) + ->html($escaped ? $this->entities($title) : $title) ->render(); } @@ -297,7 +305,7 @@ public function mailto($email, $title = null, array $attributes = [], $escaped = * * @return string */ - public function email($email) + public function email(string $email): string { return str_replace('@', '@', $this->obfuscate($email)); } @@ -305,49 +313,40 @@ public function email($email) /** * Generate an ordered list of items. * - * @param array $items - * @param array $attributes + * @param iterable|array $items + * @param iterable|array $attributes * * @return \Illuminate\Support\HtmlString */ - public function ol(array $items, array $attributes = []) + public function ol($items, $attributes = []): HtmlString { - return Elements\Ol::make() - ->items($items) - ->attributes($attributes) - ->render(); + return Ol::make()->items($items)->attributes($attributes)->render(); } /** * Generate an un-ordered list of items. * - * @param array $items - * @param array $attributes + * @param iterable|array $items + * @param iterable|array $attributes * * @return \Illuminate\Support\HtmlString */ - public function ul(array $items, array $attributes = []) + public function ul($items, $attributes = []): HtmlString { - return Elements\Ul::make() - ->items($items) - ->attributes($attributes) - ->render(); + return Ul::make()->items($items)->attributes($attributes)->render(); } /** * Generate a description list of items. * - * @param array $items - * @param array $attributes + * @param iterable|array $items + * @param iterable|array $attributes * * @return \Illuminate\Support\HtmlString */ - public function dl(array $items, array $attributes = []) + public function dl($items, $attributes = []): HtmlString { - return Elements\Dl::make() - ->items($items) - ->attributes($attributes) - ->render(); + return Dl::make()->items($items)->attributes($attributes)->render(); } /** @@ -357,7 +356,7 @@ public function dl(array $items, array $attributes = []) * * @return string */ - public function nbsp($multiplier = 1) + public function nbsp(int $multiplier = 1): string { return str_repeat(' ', $multiplier); } @@ -369,7 +368,7 @@ public function nbsp($multiplier = 1) * * @return string */ - public function attributes(array $attributes) + public function attributes(array $attributes): string { return Attributes::make($attributes)->render(); } @@ -381,25 +380,25 @@ public function attributes(array $attributes) * * @return string */ - public function obfuscate($value) + public function obfuscate(string $value): string { - return Helpers\Obfuscater::make($value); + return Obfuscater::make($value); } /** * Generate a meta tag. * - * @param string $name - * @param string $content - * @param array $attributes + * @param string|null $name + * @param string $content + * @param array $attributes * * @return \Illuminate\Support\HtmlString */ - public function meta($name, $content, array $attributes = []) + public function meta(?string $name, string $content, array $attributes = []): HtmlString { - return Elements\Meta::make() - ->attributeUnless(is_null($name), 'name', $name) - ->attributeUnless(is_null($content), 'content', $content) + return Meta::make() + ->attributeIfNotNull($name, 'name', $name) + ->attributeIfNotNull($content, 'content', $content) ->attributes($attributes) ->render(); } @@ -407,18 +406,18 @@ public function meta($name, $content, array $attributes = []) /** * Generate a HTML link to an phone number (call). * - * @param string $phone - * @param string $title - * @param array $attributes - * @param bool $escaped + * @param string $phone + * @param string|null $title + * @param array $attributes + * @param bool $escaped * * @return \Illuminate\Support\HtmlString */ - public function tel($phone, $title = null, $attributes = [], $escaped = true) + public function tel(string $phone, ?string $title = null, array $attributes = [], $escaped = true): HtmlString { $title = $title ?: $phone; - return Elements\A::make() + return A::make() ->href("tel:{$phone}") ->attributes($attributes) ->html($escaped ? $this->entities($title) : $title) diff --git a/src/HtmlServiceProvider.php b/src/HtmlServiceProvider.php index 1a7d1b0..ff144b3 100644 --- a/src/HtmlServiceProvider.php +++ b/src/HtmlServiceProvider.php @@ -1,5 +1,10 @@ -singleton(Contracts\HtmlBuilder::class, HtmlBuilder::class); - $this->singleton(Contracts\FormBuilder::class, FormBuilder::class); + $this->singleton(HtmlBuilderContract::class, HtmlBuilder::class); + $this->singleton(FormBuilderContract::class, FormBuilder::class); } /** @@ -35,8 +40,8 @@ public function register(): void public function provides(): array { return [ - Contracts\HtmlBuilder::class, - Contracts\FormBuilder::class, + HtmlBuilderContract::class, + FormBuilderContract::class, ]; } } diff --git a/src/Traits/Componentable.php b/src/Traits/Componentable.php index 1b67757..dd2c409 100644 --- a/src/Traits/Componentable.php +++ b/src/Traits/Componentable.php @@ -1,4 +1,8 @@ -getComponentData($component['signature'], $arguments); @@ -84,7 +88,7 @@ protected function renderComponent($name, array $arguments) * * @return array */ - protected function getComponentData(array $signature, array $arguments) + protected function getComponentData(array $signature, array $arguments): array { $data = []; $i = 0; diff --git a/src/Traits/FormAccessible.php b/src/Traits/FormAccessible.php index de79e25..bdba065 100644 --- a/src/Traits/FormAccessible.php +++ b/src/Traits/FormAccessible.php @@ -1,4 +1,8 @@ -getAttributeFromArray($key); @@ -90,7 +94,7 @@ abstract protected function asDateTime($value); * * @return bool */ - protected function hasFormMutator($key) + protected function hasFormMutator(string $key): bool { $methods = $this->getReflection()->getMethods(ReflectionMethod::IS_PUBLIC); @@ -107,7 +111,7 @@ protected function hasFormMutator($key) * * @return mixed */ - private function mutateFormAttribute($key, $value) + private function mutateFormAttribute(string $key, $value) { return $this->{$this->getMutateFromMethodName($key)}($value); } @@ -119,7 +123,7 @@ private function mutateFormAttribute($key, $value) * * @return string */ - private function getMutateFromMethodName($key) + private function getMutateFromMethodName(string $key): string { return 'form'.Str::studly($key).'Attribute'; } @@ -128,10 +132,8 @@ private function getMutateFromMethodName($key) * Get a ReflectionClass Instance. * * @return \ReflectionClass - * - * @throws \ReflectionException */ - protected function getReflection() + protected function getReflection(): ReflectionClass { if (is_null($this->reflection)) { $this->reflection = new ReflectionClass($this); diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 61d4957..da9aab4 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -1,9 +1,17 @@ -app['session.store']; - $session->put('_token', 'abc'); - - $this->form = new FormBuilder( - $this->html, - $this->urlGenerator, - $session - ); + $this->form = $this->getFormBuilder(); } /** @@ -61,24 +61,24 @@ public function tearDown(): void */ /** @test */ - public function it_can_be_instantiated() + public function it_can_be_instantiated(): void { static::assertInstanceOf(FormBuilder::class, $this->form); static::assertNull($this->form->getModel()); } /** @test */ - public function it_can_be_instantiated_via_helper() + public function it_can_be_instantiated_via_helper(): void { static::assertInstanceOf(FormBuilder::class, form()); static::assertNull(form()->getModel()); } /** @test */ - public function it_can_set_and_get_session() + public function it_can_set_and_get_session(): void { static::assertEquals( - $this->mockSession()->reveal(), + $this->mockSession(), $this->form->getSessionStore() ); } @@ -91,7 +91,7 @@ public function it_can_set_and_get_session() * @param string $expected * @param array $attributes */ - public function it_can_open_form($expected, $attributes) + public function it_can_open_form($expected, $attributes): void { static::assertEquals( $expected, @@ -100,7 +100,7 @@ public function it_can_open_form($expected, $attributes) } /** @test */ - public function it_can_open_form_with_different_actions() + public function it_can_open_form_with_different_actions(): void { $expected = implode('', [ '
', @@ -118,7 +118,7 @@ public function it_can_open_form_with_different_actions() } /** @test */ - public function it_can_open_form_with_route_name_and_parameters() + public function it_can_open_form_with_route_name_and_parameters(): void { static::assertEquals( '', @@ -132,7 +132,7 @@ public function it_can_open_form_with_route_name_and_parameters() } /** @test */ - public function it_can_open_form_with_action_name_and_parameters() + public function it_can_open_form_with_action_name_and_parameters(): void { static::assertEquals( '', @@ -146,7 +146,7 @@ public function it_can_open_form_with_action_name_and_parameters() } /** @test */ - public function it_can_open_form_with_url_and_parameters() + public function it_can_open_form_with_url_and_parameters(): void { static::assertEquals( '', @@ -160,7 +160,7 @@ public function it_can_open_form_with_url_and_parameters() } /** @test */ - public function it_can_close_form() + public function it_can_close_form(): void { static::assertEquals('
', $this->form->close()); } @@ -176,7 +176,7 @@ public function it_can_close_form() * @param array $attributes * @param bool $escaped */ - public function it_can_make_label($expected, $name, $value, $attributes, $escaped = true) + public function it_can_make_label($expected, $name, $value, $attributes, $escaped = true): void { static::assertEquals( $expected, @@ -195,7 +195,7 @@ public function it_can_make_label($expected, $name, $value, $attributes, $escape * @param mixed $value * @param array $attributes */ - public function it_can_make_form_inputs($expected, $type, $name, $value, array $attributes) + public function it_can_make_form_inputs($expected, $type, $name, $value, array $attributes): void { static::assertEquals( $expected, @@ -203,36 +203,6 @@ public function it_can_make_form_inputs($expected, $type, $name, $value, array $ ); } - /** - * @return array - */ - public function provideInputs() - { - return [ - [ - '', - 'text', - 'foo', - null, - [], - ], - [ - '', - 'text', - 'foo', - 'foobar', - [], - ], - [ - '', - 'date', - 'dob', - null, - ['class' => 'form-control'], - ], - ]; - } - /** * @test * @@ -242,34 +212,17 @@ public function provideInputs() * @param string $name * @param array $attributes */ - public function it_can_make_password_inputs($expected, $name, $attributes) + public function it_can_make_password_inputs($expected, $name, $attributes): void { static::assertEquals($expected, $this->form->password($name, $attributes)->toHtml()); } - /** - * @return array - */ - public function providePassword() - { - return [ - [ - '', - 'foo', - [] - ],[ - '', - 'foo', - ['class' => 'form-control'] - ] - ]; - } - /** @test */ - public function it_can_make_not_filled_passwords() + public function it_can_make_not_filled_passwords(): void { - $session = $this->mockSession(); - $session->getOldInput()->shouldNotBeCalled(); + $this->mockSession(function (MockInterface $mock) { + $mock->shouldNotReceive('getOldInput'); + }); static::assertEquals( '', @@ -278,10 +231,11 @@ public function it_can_make_not_filled_passwords() } /** @test */ - public function it_can_make_not_filled_files() + public function it_can_make_not_filled_files(): void { - $session = $this->mockSession(); - $session->getOldInput()->shouldNotBeCalled(); + $this->mockSession(function (MockInterface $mock) { + $mock->shouldNotReceive('getOldInput'); + }); static::assertEquals( '', @@ -299,7 +253,7 @@ public function it_can_make_not_filled_files() * @param mixed $value * @param array $attributes */ - public function it_can_make_text_inputs($expected, $name, $value, $attributes) + public function it_can_make_text_inputs($expected, $name, $value, $attributes): void { static::assertEquals( $expected, @@ -307,40 +261,15 @@ public function it_can_make_text_inputs($expected, $name, $value, $attributes) ); } - /** - * Provider text inputs data - * - * @return array - */ - public function provideTextInputs() - { - return [ - [ - '', - 'foo', - null, - [] - ],[ - '', - 'foo', - 'foobar', - [] - ],[ - '', - 'foo', - null, - ['class' => 'form-control'] - ] - ]; - } - /** @test */ - public function it_can_make_populated_text_inputs() + public function it_can_make_populated_text_inputs(): void { - $session = $this->mockSession(); - $session->getOldInput('name_with_dots') - ->shouldBeCalledTimes(2) - ->willReturn('some value'); + $this->mockSession(function (MockInterface $mock) { + $mock->shouldReceive('getOldInput') + ->with('name_with_dots') + ->andReturn('some value') + ->times(2); + }); $this->setModel($model = [ 'relation' => [ @@ -354,16 +283,23 @@ public function it_can_make_populated_text_inputs() $this->form->text('name.with.dots', 'default value')->toHtml() ); - $session->getOldInput('text.key.sub') - ->shouldBeCalled() - ->willReturn(null); + $this->mockSession(function (MockInterface $mock) { + $mock->shouldReceive('getOldInput') + ->with('text.key.sub') + ->andReturn(null) + ->once(); + }); static::assertEquals( '', $this->form->text('text[key][sub]', 'default value')->toHtml() ); - $session->getOldInput('relation.key')->willReturn(null); + $this->mockSession(function (MockInterface $mock) { + $mock->shouldReceive('getOldInput') + ->with('relation.key') + ->andReturn(null); + }); $textInput = $this->form->text('relation[key]')->toHtml(); @@ -389,7 +325,7 @@ public function it_can_make_populated_text_inputs() * @param string $model * @param string $name */ - public function it_can_make_populated_text_inputs_with_mix_of_arrays_and_objects($expected, $model, $name) + public function it_can_make_populated_text_inputs_with_mix_of_arrays_and_objects($expected, $model, $name): void { $this->form->model($model); @@ -399,30 +335,6 @@ public function it_can_make_populated_text_inputs_with_mix_of_arrays_and_objects ); } - /** - * @return array - */ - public function provideTextArrayAndObjectModel() - { - return [ - [ - '', - [ - 'user' => (object) [ - 'password' => 'apple' - ] - ], - 'user[password]' - ],[ - '', - (object) [ - 'letters' => ['a', 'b', 'c'] - ], - 'letters[1]' - ] - ]; - } - /** * @test * @@ -433,7 +345,7 @@ public function provideTextArrayAndObjectModel() * @param mixed $value * @param array $attributes */ - public function it_can_make_hidden_inputs($expected, $name, $value, $attributes) + public function it_can_make_hidden_inputs($expected, $name, $value, $attributes): void { static::assertEquals( $expected, @@ -441,14 +353,6 @@ public function it_can_make_hidden_inputs($expected, $name, $value, $attributes) ); } - /** - * @return array - */ - public function provideHiddenData() - { - return $this->getInputData('hidden'); - } - /** * @test * @@ -459,7 +363,7 @@ public function provideHiddenData() * @param mixed $value * @param array $attributes */ - public function it_can_make_email_inputs($expected, $name, $value, $attributes) + public function it_can_make_email_inputs($expected, $name, $value, $attributes): void { static::assertEquals($expected, $this->form->email($name, $value, $attributes)->toHtml()); @@ -469,14 +373,6 @@ public function it_can_make_email_inputs($expected, $name, $value, $attributes) ); } - /** - * @return array - */ - public function provideEmailData() - { - return $this->getInputData('email'); - } - /** * @test * @@ -487,19 +383,11 @@ public function provideEmailData() * @param mixed $value * @param array $attributes */ - public function it_can_make_tel_inputs($expected, $name, $value, $attributes) + public function it_can_make_tel_inputs($expected, $name, $value, $attributes): void { static::assertEquals($expected, $this->form->tel($name, $value, $attributes)); } - /** - * @return array - */ - public function provideTelInputs() - { - return $this->getInputData('tel'); - } - /** * @test * @@ -510,19 +398,11 @@ public function provideTelInputs() * @param mixed $value * @param array $attributes */ - public function it_can_make_number_inputs($expected, $name, $value, $attributes) + public function it_can_make_number_inputs($expected, $name, $value, $attributes): void { static::assertEquals($expected, $this->form->number($name, $value, $attributes)); } - /** - * @return array - */ - public function provideNumberInputs() - { - return $this->getInputData('number', 1); - } - /** * @test * @@ -533,7 +413,7 @@ public function provideNumberInputs() * @param mixed $value * @param array $attributes */ - public function it_can_make_date_inputs($expected, $name, $value, $attributes) + public function it_can_make_date_inputs($expected, $name, $value, $attributes): void { static::assertEquals( $expected, @@ -541,21 +421,6 @@ public function it_can_make_date_inputs($expected, $name, $value, $attributes) ); } - /** - * @return array - */ - public function provideDateInputs() - { - return $this->getInputData('date', '2015-02-20', [ - [ - '', - 'foo', - now(), - [] - ] - ]); - } - /** * @test * @@ -566,7 +431,7 @@ public function provideDateInputs() * @param string $value * @param array $attributes */ - public function it_can_make_datetime_input($expected, $name, $value, $attributes) + public function it_can_make_datetime_input($expected, $name, $value, $attributes): void { static::assertEquals( $expected, @@ -574,32 +439,6 @@ public function it_can_make_datetime_input($expected, $name, $value, $attributes ); } - /** - * Provide datetime inputs. - * - * @return array - */ - public function provideDateTimeInputs() - { - date_default_timezone_set('UTC'); - - return [ - [ - '', - 'datetime', new \DateTime('2015-01-01'), [], - ],[ - '', - 'datetime', new \DateTime('2015-01-01 15:01:01'), [], - ],[ - '', - 'datetime', '2015-01-01 15:01:01', [], - ],[ - '', - 'datetime', '2015-01-01 12:00:00', ['class' => 'datetime-picker'], - ] - ]; - } - /** * @test * @@ -610,7 +449,7 @@ public function provideDateTimeInputs() * @param string $value * @param array $attributes */ - public function it_can_make_datetime_locale_input($expected, $name, $value, $attributes) + public function it_can_make_datetime_locale_input($expected, $name, $value, $attributes): void { static::assertEquals( $expected, @@ -618,33 +457,6 @@ public function it_can_make_datetime_locale_input($expected, $name, $value, $att ); } - /** - * Provide datetime locale inputs. - * - * @return array - */ - public function provideDateTimeLocaleInputs() - { - return [ - [ - '', - 'datetime-locale', new \DateTime('2015-01-01'), [], - ], - [ - '', - 'datetime-local', new \DateTime('2015-01-01 15:01:01'), [], - ], - [ - '', - 'datetime-local', '2015-01-01 15:01:01', [], - ], - [ - '', - 'datetime-local', '2015-01-01 12:00:00', ['class' => 'datetime-local-picker'], - ], - ]; - } - /** * @test * @@ -655,7 +467,7 @@ public function provideDateTimeLocaleInputs() * @param string $value * @param array $attributes */ - public function it_can_make_color_input($expected, $name, $value, $attributes) + public function it_can_make_color_input($expected, $name, $value, $attributes): void { static::assertEquals( $expected, @@ -663,29 +475,6 @@ public function it_can_make_color_input($expected, $name, $value, $attributes) ); } - /** - * Provide color inputs. - * - * @return array - */ - public function provideColorInputs() - { - return [ - [ - '', - 'palette', null, [], - ], - [ - '', - 'palette', '#BADA55', [], - ], - [ - '', - 'palette', '#BADA55', ['class' => 'palette-class'], - ], - ]; - } - /** * @test * @@ -696,19 +485,11 @@ public function provideColorInputs() * @param mixed $value * @param array $attributes */ - public function it_can_make_time_inputs($expected, $name, $value, $attributes) + public function it_can_make_time_inputs($expected, $name, $value, $attributes): void { static::assertEquals($expected, $this->form->time($name, $value, $attributes)); } - /** - * @return array - */ - public function provideTimeInputs() - { - return $this->getInputData('time', now()->format('H:i')); - } - /** * @test * @@ -718,7 +499,7 @@ public function provideTimeInputs() * @param string $name * @param array $attributes */ - public function it_can_make_url_inputs($expected, $name, $attributes) + public function it_can_make_url_inputs($expected, $name, $attributes): void { static::assertEquals( $expected, @@ -726,25 +507,6 @@ public function it_can_make_url_inputs($expected, $name, $attributes) ); } - /** - * @return array - */ - public function provideUrlInputs() - { - return [ - [ - '', - 'website', - [], - ], - [ - '', - 'website', - ['class' => 'form-control'], - ], - ]; - } - /** * @test * @@ -754,7 +516,7 @@ public function provideUrlInputs() * @param string $name * @param array $attributes */ - public function it_can_make_files_inputs($expected, $name, $attributes) + public function it_can_make_files_inputs($expected, $name, $attributes): void { static::assertEquals( $expected, @@ -763,35 +525,16 @@ public function it_can_make_files_inputs($expected, $name, $attributes) } /** - * @return array + * @test + * + * @dataProvider provideTextAreaInputs + * + * @param string $expected + * @param string $name + * @param mixed $value + * @param array $attributes */ - public function provideFileInputs() - { - return [ - [ - '', - 'foo', - [], - ], - [ - '', - 'foo', - ['class' => 'form-control'], - ], - ]; - } - - /** - * @test - * - * @dataProvider provideTextAreaInputs - * - * @param string $expected - * @param string $name - * @param mixed $value - * @param array $attributes - */ - public function it_can_make_textarea_inputs($expected, $name, $value, $attributes) + public function it_can_make_textarea_inputs($expected, $name, $value, $attributes): void { static::assertEquals( $expected, @@ -799,34 +542,6 @@ public function it_can_make_textarea_inputs($expected, $name, $value, $attribute ); } - /** - * @return array - */ - public function provideTextAreaInputs() - { - return [ - [ - '', - 'foo', null, [] - ],[ - '', - 'foo', 'foobar', ['cols' => 50, 'rows' => 10] - ],[ - '', - 'foo', null, ['class' => 'form-control'] - ],[ - '', - 'foo', null, ['size' => '60x15'] - ],[ - '', - 'encoded_html', 'Eggs & Sausage', ['size' => '60x60'] - ],[ - '', - 'encoded_html', 'Eggs && Sausage', ['size' => '60x60'] - ], - ]; - } - /** * @test * @@ -838,7 +553,7 @@ public function provideTextAreaInputs() * @param mixed $selected * @param array $attributes */ - public function it_can_make_select_inputs($expected, $name, $list, $selected, array $attributes) + public function it_can_make_select_inputs($expected, $name, $list, $selected, array $attributes): void { static::assertEquals( $expected, @@ -846,79 +561,12 @@ public function it_can_make_select_inputs($expected, $name, $list, $selected, ar ); } - /** - * Provide form select data - * - * @return array - */ - public function provideSelectData() - { - $list = [ - 'L' => 'Large', - 'S' => 'Small', - ]; - $selected = ['L', 'S']; - $attributes = [ - 'name' => 'sizes[]', - 'id' => 'select-id', - 'multiple' => 'multiple', - 'class' => 'class-name', - ]; - - return [ - [ - '', - 'size', $list, null, [], - ], - [ - '', - 'size', $list, 'L', [], - ], - [ - '', - 'size', $list, null, ['id' => 'select-id', 'class' => 'class-name'], - ], - [ - '', - 'sizes', $list, $selected, $attributes, - ], - [ - // Test select with a option Collection - '', - 'sizes', new Collection($list), $selected, $attributes, - ], - [ - // Test selects with a selected Collection - '', - 'sizes', $list, new Collection($selected), $attributes - ], - ]; - } - /** @test */ - public function it_can_make_select_input_with_label() + public function it_can_make_select_input_with_label(): void { static::assertHtmlStringEqualsHtmlString( '', - $this->form->label('select-name-id') + $this->form->label('select-name-id')->toHtml() ); static::assertHtmlStringEqualsHtmlString( @@ -928,12 +576,12 @@ public function it_can_make_select_input_with_label() [], null, ['name' => 'select-name'] - ) + )->toHtml() ); } /** @test */ - public function it_can_make_populated_select_inputs() + public function it_can_make_populated_select_inputs(): void { $list = [ 'L' => 'Large', @@ -946,11 +594,13 @@ public function it_can_make_populated_select_inputs() ]; $this->setModel($model); - $session = $this->mockSession(); - $session->getOldInput('size') - ->shouldBeCalledTimes(2) - ->willReturn('M'); + $this->mockSession(function (MockInterface $mock) { + $mock->shouldReceive('getOldInput') + ->with('size') + ->andReturn('M') + ->times(2); + }); static::assertEquals( implode('', [ @@ -963,9 +613,12 @@ public function it_can_make_populated_select_inputs() $this->form->select('size', $list, 'S') ); - $session->getOldInput('size.multi') - ->shouldBeCalledTimes(2) - ->willReturn(['L', 'S']); + $this->mockSession(function (MockInterface $mock) { + $mock->shouldReceive('getOldInput') + ->withArgs(['size.multi']) + ->times(2) + ->andReturn(['L', 'S']); + }); static::assertEquals( implode('', [ @@ -978,9 +631,11 @@ public function it_can_make_populated_select_inputs() $this->form->select('size[multi][]', $list, 'M', ['multiple' => 'multiple'])->toHtml() ); - $session->getOldInput('size.key') - ->shouldBeCalled() - ->willReturn(null); + $this->mockSession(function (MockInterface $mock) { + $mock->shouldReceive('getOldInput') + ->withArgs(['size.key']) + ->andReturn(null); + }); static::assertEquals( implode('', [ @@ -995,7 +650,7 @@ public function it_can_make_populated_select_inputs() } /** @test */ - public function it_can_make_select_options_with_attributes() + public function it_can_make_select_options_with_attributes(): void { static::assertEquals( '', - $this->form->select('encoded_html', ['no_break_space' => ' ', 'ampersand' => '&', 'lower_than' => '<']) + $this->form->select('encoded_html', ['no_break_space' => ' ', 'ampersand' => '&', 'lower_than' => '<'])->toHtml() ); $list = ['L' => 'Large', 'S' => 'Small']; @@ -1214,7 +868,7 @@ public function it_can_make_select_input_with_nested_options() ); $this->form->setSessionStore( - tap(new Store('name', new \SessionHandler()), function (Store $store) { + tap(new Store('name', new SessionHandler), function (Store $store) { $store->put('_old_input', ['countries' => ['1']]); }) ); @@ -1235,12 +889,12 @@ public function it_can_make_select_input_with_nested_options() } /** @test */ - public function it_can_make_checkbox_inputs() + public function it_can_make_checkbox_inputs(): void { $session = $this->mockSession(); - $session->getOldInput()->willReturn([]); - $session->getOldInput('foo')->willReturn(null); + $session->shouldReceive('getOldInput')->with('foo')->andReturn(null); + $session->shouldReceive('getOldInput')->andReturn([]); static::assertEquals( '', @@ -1264,27 +918,25 @@ public function it_can_make_checkbox_inputs() } /** @test */ - public function it_can_make_populated_checkbox_inputs() + public function it_can_make_populated_checkbox_inputs(): void { - $session = $this->mockSession(); - - $session->getOldInput()->willReturn([1]); - $session->getOldInput('check')->willReturn(null); + $this->mockSession(function (MockInterface $mock) { + $mock->shouldReceive('getOldInput')->with('check')->andReturn(null); + $mock->shouldReceive('getOldInput')->with('check.key')->andReturn('yes'); + $mock->shouldReceive('getOldInput')->with('multicheck')->andReturn([1, 3]); + $mock->shouldReceive('getOldInput')->andReturn([1]); + }); static::assertEquals( '', $this->form->checkbox('check', 1, true)->toHtml() ); - $session->getOldInput('check.key')->willReturn('yes'); - static::assertEquals( '', $this->form->checkbox('check[key]', 'yes')->toHtml() ); - $session->getOldInput('multicheck')->willReturn([1, 3]); - static::assertEquals( '', $this->form->checkbox('multicheck[]', 1)->toHtml() @@ -1302,7 +954,7 @@ public function it_can_make_populated_checkbox_inputs() } /** @test */ - public function it_can_make_checkbox_inputs_without_session() + public function it_can_make_checkbox_inputs_without_session(): void { static::assertEquals( '', @@ -1316,11 +968,12 @@ public function it_can_make_checkbox_inputs_without_session() } /** @test */ - public function it_can_make_checkbox_with_model_relation() + public function it_can_make_checkbox_with_model_relation(): void { - $session = $this->mockSession(); - $session->getOldInput()->willReturn([]); - $session->getOldInput('items')->willReturn(null); + $this->mockSession(function (MockInterface $mock) { + $mock->shouldReceive('getOldInput')->with('items')->andReturn(null); + $mock->shouldReceive('getOldInput')->andReturn([]); + }); $models = []; @@ -1354,7 +1007,7 @@ public function it_can_make_checkbox_with_model_relation() } /** @test */ - public function it_can_test_the_checkable_method() + public function it_can_test_the_checkable_method(): void { $reflector = new \ReflectionMethod(FormBuilder::class, 'checkable'); $reflector->setAccessible(true); @@ -1368,7 +1021,7 @@ public function it_can_test_the_checkable_method() } /** @test */ - public function it_can_make_radio_inputs() + public function it_can_make_radio_inputs(): void { static::assertEquals( '', @@ -1392,11 +1045,13 @@ public function it_can_make_radio_inputs() } /** @test */ - public function it_can_make_populated_radio_inputs() + public function it_can_make_populated_radio_inputs(): void { - $session = $this->mockSession(); - $session->getOldInput('radio') - ->willReturn(1); + $this->mockSession(function (MockInterface $mock) { + $mock->shouldReceive('getOldInput') + ->with('radio') + ->andReturn(1); + }); static::assertEquals( '', @@ -1410,7 +1065,7 @@ public function it_can_make_populated_radio_inputs() } /** @test */ - public function it_can_make_submit_inputs() + public function it_can_make_submit_inputs(): void { static::assertEquals( '', @@ -1424,7 +1079,7 @@ public function it_can_make_submit_inputs() } /** @test */ - public function it_can_make_buttons() + public function it_can_make_buttons(): void { static::assertEquals( '', @@ -1443,7 +1098,7 @@ public function it_can_make_buttons() } /** @test */ - public function it_can_make_reset_inputs() + public function it_can_make_reset_inputs(): void { static::assertHtmlStringEqualsHtmlString( '', @@ -1452,7 +1107,7 @@ public function it_can_make_reset_inputs() } /** @test */ - public function it_can_make_image_inputs() + public function it_can_make_image_inputs(): void { $url = 'http://laravel.com/'; @@ -1463,7 +1118,7 @@ public function it_can_make_image_inputs() } /** @test */ - public function it_can_register_and_use_a_component() + public function it_can_register_and_use_a_component(): void { $view = '_components.text'; $name = 'first_name'; @@ -1478,16 +1133,380 @@ public function it_can_register_and_use_a_component() ); } - /* ------------------------------------------------------------------------------------------------ - | Provider Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Other Methods + | ----------------------------------------------------------------- */ + + /** + * @return array + */ + public function provideInputs(): array + { + return [ + [ + '', + 'text', + 'foo', + null, + [], + ], + [ + '', + 'text', + 'foo', + 'foobar', + [], + ], + [ + '', + 'date', + 'dob', + null, + ['class' => 'form-control'], + ], + ]; + } + + /** + * @return array + */ + public function providePassword(): array + { + return [ + [ + '', + 'foo', + [] + ],[ + '', + 'foo', + ['class' => 'form-control'] + ] + ]; + } + + /** + * Provider text inputs data + * + * @return array + */ + public function provideTextInputs(): array + { + return [ + [ + '', + 'foo', + null, + [] + ],[ + '', + 'foo', + 'foobar', + [] + ],[ + '', + 'foo', + null, + ['class' => 'form-control'] + ] + ]; + } + + /** + * @return array + */ + public function provideTextArrayAndObjectModel(): array + { + return [ + [ + '', + [ + 'user' => (object) [ + 'password' => 'apple' + ] + ], + 'user[password]' + ],[ + '', + (object) [ + 'letters' => ['a', 'b', 'c'] + ], + 'letters[1]' + ] + ]; + } + + /** + * @return array + */ + public function provideHiddenData(): array + { + return $this->getInputData('hidden'); + } + + /** + * @return array + */ + public function provideEmailData(): array + { + return $this->getInputData('email'); + } + + /** + * @return array + */ + public function provideTelInputs(): array + { + return $this->getInputData('tel'); + } + + /** + * @return array + */ + public function provideNumberInputs(): array + { + return $this->getInputData('number', 1); + } + + /** + * @return array + */ + public function provideDateInputs(): array + { + return $this->getInputData('date', '2015-02-20', [ + [ + '', + 'foo', + now(), + [] + ] + ]); + } + + /** + * Provide datetime inputs. + * + * @return array + */ + public function provideDateTimeInputs(): array + { + date_default_timezone_set('UTC'); + + return [ + [ + '', + 'datetime', new \DateTime('2015-01-01'), [], + ],[ + '', + 'datetime', new \DateTime('2015-01-01 15:01:01'), [], + ],[ + '', + 'datetime', '2015-01-01 15:01:01', [], + ],[ + '', + 'datetime', '2015-01-01 12:00:00', ['class' => 'datetime-picker'], + ] + ]; + } + + /** + * Provide datetime locale inputs. + * + * @return array + */ + public function provideDateTimeLocaleInputs(): array + { + return [ + [ + '', + 'datetime-locale', new \DateTime('2015-01-01'), [], + ], + [ + '', + 'datetime-local', new \DateTime('2015-01-01 15:01:01'), [], + ], + [ + '', + 'datetime-local', '2015-01-01 15:01:01', [], + ], + [ + '', + 'datetime-local', '2015-01-01 12:00:00', ['class' => 'datetime-local-picker'], + ], + ]; + } + + /** + * Provide color inputs. + * + * @return array + */ + public function provideColorInputs(): array + { + return [ + [ + '', + 'palette', null, [], + ], + [ + '', + 'palette', '#BADA55', [], + ], + [ + '', + 'palette', '#BADA55', ['class' => 'palette-class'], + ], + ]; + } + + /** + * @return array + */ + public function provideTimeInputs(): array + { + return $this->getInputData('time', now()->format('H:i')); + } + + /** + * @return array + */ + public function provideUrlInputs(): array + { + return [ + [ + '', + 'website', + [], + ], + [ + '', + 'website', + ['class' => 'form-control'], + ], + ]; + } + + /** + * @return array + */ + public function provideFileInputs(): array + { + return [ + [ + '', + 'foo', + [], + ], + [ + '', + 'foo', + ['class' => 'form-control'], + ], + ]; + } + + /** + * @return array + */ + public function provideTextAreaInputs(): array + { + return [ + [ + '', + 'foo', null, [] + ],[ + '', + 'foo', 'foobar', ['cols' => 50, 'rows' => 10] + ],[ + '', + 'foo', null, ['class' => 'form-control'] + ],[ + '', + 'foo', null, ['size' => '60x15'] + ],[ + '', + 'encoded_html', 'Eggs & Sausage', ['size' => '60x60'] + ],[ + '', + 'encoded_html', 'Eggs && Sausage', ['size' => '60x60'] + ], + ]; + } + + /** + * Provide form select data + * + * @return array + */ + public function provideSelectData(): array + { + $list = [ + 'L' => 'Large', + 'S' => 'Small', + ]; + $selected = ['L', 'S']; + $attributes = [ + 'name' => 'sizes[]', + 'id' => 'select-id', + 'multiple' => 'multiple', + 'class' => 'class-name', + ]; + + return [ + [ + '', + 'size', $list, null, [], + ], + [ + '', + 'size', $list, 'L', [], + ], + [ + '', + 'size', $list, null, ['id' => 'select-id', 'class' => 'class-name'], + ], + [ + '', + 'sizes', $list, $selected, $attributes, + ], + [ + // Test select with a option Collection + '', + 'sizes', new Collection($list), $selected, $attributes, + ], + [ + // Test selects with a selected Collection + '', + 'sizes', $list, new Collection($selected), $attributes + ], + ]; + } + /** * Provide opening form data * * @return array */ - public function provideOpeningForms() + public function provideOpeningForms(): array { $url = $this->baseUrl . '/foo'; @@ -1519,7 +1538,7 @@ public function provideOpeningForms() * * @return array */ - public function provideLabels() + public function provideLabels(): array { return [ [ @@ -1548,12 +1567,16 @@ public function provideLabels() */ /** - * @return \Prophecy\Prophecy\ObjectProphecy + * Mock the session store. + * + * @param \Closure|null $mock + * + * @return \Mockery\MockInterface */ - protected function mockSession() + protected function mockSession(Closure $mock = null): MockInterface { - return tap($this->prophesize(\Illuminate\Session\Store::class), function ($session) { - $this->form->setSessionStore($session->reveal()); + return tap($this->mock(Session::class, $mock), function ($session) { + $this->form->setSessionStore($session); }); } @@ -1563,7 +1586,7 @@ protected function mockSession() * @param array $data * @param bool $object */ - protected function setModel(array $data, $object = true) + protected function setModel(array $data, $object = true): void { $object = $object ? new FormBuilderModelStub($data) : $data; diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php index 6b27ca3..3d37e20 100644 --- a/tests/HtmlBuilderTest.php +++ b/tests/HtmlBuilderTest.php @@ -1,4 +1,8 @@ -html); static::assertInstanceOf(HtmlBuilder::class, html()); } /** @test */ - public function it_can_convert_html_string_to_entities() + public function it_can_convert_html_string_to_entities(): void { $value = 'Un \'apostrophe\' en gras'; $result = 'Un 'apostrophe' en <strong>gras</strong>'; @@ -32,7 +36,7 @@ public function it_can_convert_html_string_to_entities() } /** @test */ - public function it_can_convert_html_entities_to_string() + public function it_can_convert_html_entities_to_string(): void { $value = 'Un \'apostrophe\' en gras'; $result = 'Un 'apostrophe' en <strong>gras</strong>'; @@ -41,7 +45,7 @@ public function it_can_convert_html_entities_to_string() } /** @test */ - public function it_can_make_script_tags() + public function it_can_make_script_tags(): void { $url = $this->urlTo($file = 'bootstrap.min.js'); @@ -52,7 +56,7 @@ public function it_can_make_script_tags() } /** @test */ - public function it_can_make_style_tags() + public function it_can_make_style_tags(): void { $url = $this->urlTo($file = 'bootstrap.min.css'); @@ -63,7 +67,7 @@ public function it_can_make_style_tags() } /** @test */ - public function it_can_make_image_tags() + public function it_can_make_image_tags(): void { $url = $this->urlTo($file = 'avatar.png'); @@ -74,7 +78,7 @@ public function it_can_make_image_tags() } /** @test */ - public function it_can_make_favicon() + public function it_can_make_favicon(): void { $url = $this->urlTo('bar.ico'); @@ -85,7 +89,7 @@ public function it_can_make_favicon() } /** @test */ - public function it_can_make_link_tags() + public function it_can_make_link_tags(): void { $url = $this->urlTo('hello'); @@ -112,7 +116,7 @@ public function it_can_make_link_tags() } /** @test */ - public function it_can_make_secure_link_tags() + public function it_can_make_secure_link_tags(): void { $title = null; $url = $this->urlTo('hello', [], true); @@ -136,7 +140,7 @@ public function it_can_make_secure_link_tags() } /** @test */ - public function it_must_escape_link_urls() + public function it_must_escape_link_urls(): void { static::assertEquals( 'Escaped url', @@ -145,7 +149,7 @@ public function it_must_escape_link_urls() } /** @test */ - public function it_can_make_link_tags_for_assets() + public function it_can_make_link_tags_for_assets(): void { $file = 'assets/img/logo.png'; $url = "{$this->baseUrl}/{$file}"; @@ -157,7 +161,7 @@ public function it_can_make_link_tags_for_assets() } /** @test */ - public function it_can_make_secure_links_tags_for_assets() + public function it_can_make_secure_links_tags_for_assets(): void { $file = 'assets/img/logo.png'; $url = "https://localhost/{$file}"; @@ -169,7 +173,7 @@ public function it_can_make_secure_links_tags_for_assets() } /** @test */ - public function it_can_make_link_from_route() + public function it_can_make_link_from_route(): void { $title = 'Home'; $route = 'home'; @@ -197,7 +201,7 @@ public function it_can_make_link_from_route() } /** @test */ - public function it_can_make_link_from_action() + public function it_can_make_link_from_action(): void { $title = 'Home'; $action = 'Arcanedev\LaravelHtml\Tests\Stubs\DummyController@index'; @@ -226,7 +230,7 @@ public function it_can_make_link_from_action() } /** @test */ - public function it_can_make_mailto_link() + public function it_can_make_mailto_link(): void { $email = 'j.doe@gmail.com'; $mailto = $this->html->mailto($email)->toHtml(); @@ -251,7 +255,7 @@ public function it_can_make_mailto_link() } /** @test */ - public function it_can_make_ol_tags() + public function it_can_make_ol_tags(): void { $list = [ 'foo' => 'bar', @@ -267,7 +271,7 @@ public function it_can_make_ol_tags() '
  • bar
  • ' . '
  • baz
  • ' . '', - $this->html->ol($list, $attributes) + $this->html->ol($list, $attributes)->toHtml() ); // Empty list @@ -278,7 +282,7 @@ public function it_can_make_ol_tags() } /** @test */ - public function it_can_make_ul_tags() + public function it_can_make_ul_tags(): void { $list = [ 'foo' => 'bar', @@ -294,7 +298,7 @@ public function it_can_make_ul_tags() '
  • bar
  • ' . '
  • baz
  • ' . '', - $this->html->ul($list, $attributes) + $this->html->ul($list, $attributes)->toHtml() ); // Empty list @@ -305,7 +309,7 @@ public function it_can_make_ul_tags() } /** @test */ - public function it_can_make_dl_tags() + public function it_can_make_dl_tags(): void { $list = [ 'foo' => 'bar', @@ -338,7 +342,7 @@ public function it_can_make_dl_tags() '
    bing
    baz
    ' . '
    qux
    qux-foo
    qux-bar
    qux-baz
    ' . '', - $this->html->dl($list, $attributes) + $this->html->dl($list, $attributes)->toHtml() ); // Empty list @@ -349,7 +353,7 @@ public function it_can_make_dl_tags() } /** @test */ - public function it_can_make_nested_listing() + public function it_can_make_nested_listing(): void { $list = [ 'bar', @@ -373,7 +377,7 @@ public function it_can_make_nested_listing() '' . '' . '', - $this->html->ul($list) + $this->html->ul($list)->toHtml() ); static::assertHtmlStringEqualsHtmlString( @@ -388,18 +392,18 @@ public function it_can_make_nested_listing() '' . '' . '', - $this->html->ol($list) + $this->html->ol($list)->toHtml() ); } /** @test */ - public function it_can_obfuscate() + public function it_can_obfuscate(): void { static::assertNotEmpty($this->html->obfuscate('à')); } /** @test */ - public function it_can_make_attributes() + public function it_can_make_attributes(): void { static::assertEquals( 'class="strong" required', @@ -408,7 +412,7 @@ public function it_can_make_attributes() } /** @test */ - public function it_can_make_meta_tags() + public function it_can_make_meta_tags(): void { static::assertEquals( '', @@ -417,7 +421,7 @@ public function it_can_make_meta_tags() } /** @test */ - public function it_can_make_meta_open_graph_tags() + public function it_can_make_meta_open_graph_tags(): void { static::assertEquals( '', @@ -426,7 +430,7 @@ public function it_can_make_meta_open_graph_tags() } /** @test */ - public function it_can_register_a_component() + public function it_can_register_a_component(): void { $this->html->component('tweet', 'components.tweet', [ 'handle', 'body', 'date' @@ -436,7 +440,7 @@ public function it_can_register_a_component() } /** @test */ - public function it_must_throw_bad_method_call_exception_on_component() + public function it_must_throw_bad_method_call_exception_on_component(): void { $this->expectException(\BadMethodCallException::class); $this->expectExceptionMessage('Method Arcanedev\LaravelHtml\HtmlBuilder::btnSuccess does not exist.'); @@ -445,7 +449,7 @@ public function it_must_throw_bad_method_call_exception_on_component() } /** @test */ - public function it_can_generate_nbsp() + public function it_can_generate_nbsp(): void { $nbsp = ' '; @@ -454,7 +458,7 @@ public function it_can_generate_nbsp() } /** @test */ - public function it_can_make_tel_link() + public function it_can_make_tel_link(): void { $phone = '+123-456-789'; diff --git a/tests/HtmlServiceProviderTest.php b/tests/HtmlServiceProviderTest.php index fe596ac..645d941 100644 --- a/tests/HtmlServiceProviderTest.php +++ b/tests/HtmlServiceProviderTest.php @@ -1,5 +1,10 @@ -provider->provides()); diff --git a/tests/Stubs/DummyController.php b/tests/Stubs/DummyController.php index 0f7a484..5aefcde 100644 --- a/tests/Stubs/DummyController.php +++ b/tests/Stubs/DummyController.php @@ -1,4 +1,8 @@ -app->loadDeferredProviders(); + $router = $this->registerRoutes(); $this->urlGenerator = new UrlGenerator($router->getRoutes(), Request::create('/foo', 'GET')); $this->html = new HtmlBuilder($this->urlGenerator); @@ -63,28 +69,13 @@ public function tearDown(): void * * @return array */ - protected function getPackageProviders($app) + protected function getPackageProviders($app): array { return [ \Arcanedev\LaravelHtml\HtmlServiceProvider::class, ]; } - /** - * Get package aliases. - * - * @param \Illuminate\Foundation\Application $app - * - * @return array - */ - protected function getPackageAliases($app) - { - return [ - 'Form' => \Arcanedev\LaravelHtml\Facades\Form::class, - 'Html' => \Arcanedev\LaravelHtml\Facades\Html::class, - ]; - } - /** * Define environment setup. * @@ -109,11 +100,28 @@ protected function getEnvironmentSetUp($app) /** * Migrate the database. */ - protected function migrate() + protected function migrate(): void { $this->loadMigrationsFrom(__DIR__.'/fixtures/migrations'); } + /** + * Get the form builder. + * + * @return \Arcanedev\LaravelHtml\Contracts\FormBuilder + */ + protected function getFormBuilder(): FormBuilderContract + { + return new FormBuilder( + $this->html, + $this->urlGenerator, + tap($this->app['session.store'], function ($session) { + /** @var \Illuminate\Contracts\Session\Session $session */ + $session->put('_token', 'abc'); + }) + ); + } + /** * Register routes for tests. * @@ -158,7 +166,7 @@ protected function urlTo($path, $extra = [], $secure = null) * * @return array */ - protected function getInputData($type, $value = 'bar', array $merge = []) + protected function getInputData(string $type, $value = 'bar', array $merge = []) { return array_merge([ [ diff --git a/tests/Traits/FormAccessible.php b/tests/Traits/FormAccessible.php index 8eabf13..c808a11 100644 --- a/tests/Traits/FormAccessible.php +++ b/tests/Traits/FormAccessible.php @@ -1,8 +1,11 @@ - $this->now, ]; - /** @var \Illuminate\Contracts\Session\Session $session */ - $session = $this->app['session.store']; - $session->put('_token', 'abc'); - - $this->form = new FormBuilder($this->html, $this->urlGenerator, $session); + $this->form = $this->getFormBuilder(); } /* ----------------------------------------------------------------- @@ -78,9 +77,10 @@ public function setUp(): void */ /** @test */ - public function it_can_mutate_values_for_forms() + public function it_can_mutate_values_for_forms(): void { $model = new ModelThatUsesForms($this->modelData); + $this->form->setModel($model); static::assertSame('ponmlkjihgfedcba', $model->getFormValue('string')); @@ -88,9 +88,10 @@ public function it_can_mutate_values_for_forms() } /** @test */ - public function it_can_still_mutate_values_for_views() + public function it_can_still_mutate_values_for_views(): void { $model = new ModelThatUsesForms($this->modelData); + $this->form->setModel($model); static::assertSame('ABCDEFGHIJKLMNOP', $model->string); @@ -98,9 +99,10 @@ public function it_can_still_mutate_values_for_views() } /** @test */ - public function it_does_not_require_the_use_of_this_feature() + public function it_does_not_require_the_use_of_this_feature(): void { $model = new ModelThatDoesntUseForms($this->modelData); + $this->form->setModel($model); static::assertSame('ABCDEFGHIJKLMNOP', $model->string); @@ -108,22 +110,24 @@ public function it_does_not_require_the_use_of_this_feature() } /** @test */ - public function it_can_get_related_value_for_forms() + public function it_can_get_related_value_for_forms(): void { $model = new ModelThatUsesForms($this->modelData); + $this->form->setModel($model); static::assertSame('abcde st', $model->getFormValue('address.street')); } /** @test */ - public function it_can_mutate_related_values_for_forms() + public function it_can_mutate_related_values_for_forms(): void { $model = new ModelThatUsesForms($this->modelData); $model->setRelation( 'related', new ModelThatUsesForms($this->modelData) ); + $this->form->setModel($model); static::assertSame($this->form->getValueAttribute('related[string]'), 'ponmlkjihgfedcba'); diff --git a/tests/fixtures/migrations/2015_01_01_000001_create_models_table.php b/tests/fixtures/migrations/2015_01_01_000001_create_models_table.php index 2ba94c3..3d21845 100644 --- a/tests/fixtures/migrations/2015_01_01_000001_create_models_table.php +++ b/tests/fixtures/migrations/2015_01_01_000001_create_models_table.php @@ -1,5 +1,7 @@ increments('id'); $table->string('string'); $table->string('email'); @@ -21,7 +23,7 @@ public function up() }); } - public function down() + public function down(): void { Schema::drop('models'); }