diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ce79f9e..01330a0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: true matrix: - php: ['7.4'] + php: ['7.4', '8.0'] laravel: ['7.*', '8.*'] include: - laravel: '8.*' @@ -29,7 +29,7 @@ jobs: uses: actions/checkout@v2 - name: Cache dependencies - uses: actions/cache@v1 + uses: actions/cache@v2 with: path: ~/.composer/cache/files key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} @@ -38,21 +38,21 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} - extensions: common, curl, json, mbstring, zip, sqlite, pdo_sqlite - coverage: xdebug + extensions: curl, json, mbstring, zip, sqlite, pdo_sqlite + coverage: pcov - name: Install dependencies run: | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update - composer update --prefer-dist --no-interaction --no-suggest + composer update --prefer-dist --no-interaction - # lower php and laravel versions + # Lower php and laravel versions. - name: PHPUnit if: matrix.php != '7.4' || matrix.laravel != '8.*' run: vendor/bin/phpunit - # last php and laravel versions + # Last php and laravel versions. - name: Code analysis if: matrix.php == '7.4' && matrix.laravel == '8.*' diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index 8a97050..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,28 +0,0 @@ -filter: - excluded_paths: [tests/*] -checks: - php: true -tools: - php_mess_detector: true - php_pdepend: true - php_code_coverage: false - external_code_coverage: false - php_analyzer: true - php_code_sniffer: - config: - standard: PSR4 - filter: - paths: ['src'] - sensiolabs_security_checker: true - php_loc: - enabled: true - excluded_dirs: [vendor, tests] - php_cpd: - enabled: true - excluded_dirs: [vendor, tests] -build: - nodes: - analysis: - tests: - override: - - php-scrutinizer-run diff --git a/CHANGELOG.md b/CHANGELOG.md index 03386e4..f143966 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,21 @@ # Changelog -## [1.4.0](https://github.com/Okipa/laravel-html-helper/compare/1.4.0...1.3.2) +## [2.0.0](https://github.com/Okipa/laravel-html-helper/compare/1.4.0...2.0.0) + +2020-11-14 + +* Added PHP 8 support +* Removed Scrutinizer analysis +* Updated PHPCS checker and fixer norm to PSR-12 +* Helpers naming breaking changes + +:point_right: [See the upgrade guide](/docs/upgrade-guides/from-v1-to-v2.md) + +## [1.4.0](https://github.com/Okipa/laravel-html-helper/compare/1.3.1...1.4.0) 2020-08-11 -* Added support for Laravel 8. +* Added support for Laravel 8 * Dropped support for Laravel 5.8 and 6 * Dropped support for PHP 7.2 and 7.3 @@ -12,40 +23,40 @@ 2020-03-03 -* Added testing files to .gitattributes export-ignore. +* Added testing files to .gitattributes export-ignore ## [1.3.0](https://github.com/Okipa/laravel-html-helper/compare/1.2.0...1.3.0) 2020-02-18 -* Dropped support for Laravel 5.5, 5.6 and 5.7. -* Added support for Laravel 7. +* Dropped support for Laravel 5.5, 5.6 and 5.7 +* Added support for Laravel 7 ## [1.2.0](https://github.com/Okipa/laravel-html-helper/compare/1.1.1...1.2.0) 2019-09-04 -* Added support for Laravel 6. -* Dropped support for php 7.1. +* Added support for Laravel 6 +* Dropped support for php 7.1 ## [1.1.1](https://github.com/Okipa/laravel-html-helper/compare/1.1.0...1.1.1) 2019-08-02 -* Fixed `htmlAttributes()` feature which was adding a space before the returned value, even if it was supposed to be an empty space. +* Fixed `html_attributes()` feature which was adding a space before the returned value, even if it was supposed to be an empty space ## [1.1.0](https://github.com/Okipa/laravel-html-helper/compare/1.0.5...1.1.0) 2019-08-02 -* `classTag()` and `htmlAttributes()` helpers are now callable directly after a html tag : they do add a space if the helper returns something and no space if nothing is returned. +* `html_classes()` and `html_attributes()` helpers are now callable directly after a html tag : they do add a space if the helper returns something and no space if nothing is returned ```html -) }}> +) }}> ``` ## [1.0.5](https://github.com/Okipa/laravel-html-helper/compare/1.1.4...1.0.5) 2019-02-07 -* Updated treatment to return an empty string when the classes list to return is empty. +* Updated treatment to return an empty string when the classes list to return is empty diff --git a/README.md b/README.md index ef737a2..07ac1fe 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,43 @@ -# Laravel Html Helper - -[![Source Code](https://img.shields.io/badge/source-okipa/laravel--html--helper-blue.svg)](https://github.com/Okipa/laravel-html-helper) -[![Latest Version](https://img.shields.io/github/release/okipa/laravel-html-helper.svg?style=flat-square)](https://github.com/Okipa/laravel-html-helper/releases) -[![Total Downloads](https://img.shields.io/packagist/dt/okipa/laravel-html-helper.svg?style=flat-square)](https://packagist.org/packages/okipa/laravel-html-helper) -[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) -[![Build status](https://github.com/Okipa/laravel-html-helper/workflows/CI/badge.svg)](https://github.com/Okipa/laravel-html-helper/actions) -[![Coverage Status](https://coveralls.io/repos/github/Okipa/laravel-html-helper/badge.svg?branch=master)](https://coveralls.io/github/Okipa/laravel-html-helper?branch=master) -[![Quality Score](https://img.shields.io/scrutinizer/g/Okipa/laravel-html-helper.svg?style=flat-square)](https://scrutinizer-ci.com/g/Okipa/laravel-html-helper/?branch=master) - -Useful helpers to dynamically generate clean HTML with Laravel. +![Laravel HTML Helper](/docs/laravel-html-helper.png) +

+ + Latest Stable Version + + + Total Downloads + + + Build Status + + + Coverage Status + + + License: MIT + +

+ +Easily handle conditional HTML generation with the following helpers. ## Compatibility | Laravel | PHP | Package | |---|---|---| -| ^7.* | ^7.4 | ^1.4 | +| ^7.* | ^7.4 | ^2.0 | | ^5.8 | ^7.2 | ^1.3 | | ^5.5 | ^7.2 | ^1.2 | | ^5.5 | ^7.1 | ^1.0 | +## Upgrade guide + +* [From v1 to V2](/docs/upgrade-guides/from-v1-to-v2.md) + ## Table of Contents - [Installation](#installation) - [API](#api) - - [classTag](#classtag) - - [htmlAttributes](#htmlattributes) + - [html_classes](#html_classes) + - [html_attributes](#html_attributes) - [Testing](#testing) - [Changelog](#changelog) - [Contributing](#contributing) @@ -33,63 +46,63 @@ Useful helpers to dynamically generate clean HTML with Laravel. ## Installation -- Install the package with composer : +- Install the package with composer: ```bash -composer require "okipa/laravel-html-helper:^1.3" +composer require okipa/laravel-html-helper ``` ## API -### `classTag(...$classList) : HtmlString` +### html_classes -In you view : +Calling this helper generates an HTML `class` tag encapsulating the given dynamic classes. + +It accepts combination of strings, integers, arrays or null arguments. ```blade - 'class6']], - 7 +@php + $id = 17; + $fullScreen = false; + $darkMode = true; +@endphp + ``` -Will produce : - ```html -
+
``` -In your code, you can do exactly the same using the `(new Okipa\LaravelHtmlHelper\HtmlClassTag)->render()` method. +You can call `app(Okipa\LaravelHtmlHelper\HtmlClasses)->toHtml($classes)` if you want to avoid helper use. -### `htmlAttributes(...$attributesList) : HtmlString` +### html_attributes -In you view : +Calling this helper generates dynamic HTML attributes, taking care about the given key-only, value-only or key-value combinations. + +It accepts combination of strings, arrays or null arguments. ```blade - 'attribute2Value'], - ['attribute3Key' => null], - ['attribute4Value', 'attribute5Value'], - '', - null, - ['' => 'attribute6Value'], - ['attributes7Value', ['attribute8Value', 'attribute9Key' => 'attribute9Value']], - ['attribute10Key' => ['attribute11Value']], - ['attribute12Key' => ''] +@php + $dragAndDrop = true; + $disabled = false; +@endphp + __('Are you sure you want to delete this line?')], + $dragAndDrop ? 'data-drag-drop' : null, + $disabled ? ['disabled', 'data-forbid-click'] : null, + 'required' ) }}> ``` -Will produce : - ```html -
+
``` -In your code, you can do exactly the same using the `(new Okipa\LaravelHtmlHelper\HtmlAttributes)->render()` method. +You can call `app(Okipa\LaravelHtmlHelper\HtmlAttributes)->toHtml($attributes)` if you want to avoid helper use. ## Testing diff --git a/composer.json b/composer.json index 464e805..0716dca 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ } ], "require": { - "php": "^7.4", + "php": "^7.4||^8.0", "illuminate/support": "^7.0||^8.0" }, "require-dev": { @@ -50,8 +50,8 @@ "vendor/bin/phpcbf", "vendor/bin/phpcs", "vendor/bin/phpmd src text phpmd.xml", - "vendor/bin/phpstan analyse", - "vendor/bin/phpunit" + "vendor/bin/phpstan analyse --memory-limit=-1 --error-format=table", + "vendor/bin/phpunit -d memory_limit=-1 --testdox --coverage-text" ] }, "config": { diff --git a/docs/laravel-html-helper.png b/docs/laravel-html-helper.png new file mode 100644 index 0000000..9ac66c2 Binary files /dev/null and b/docs/laravel-html-helper.png differ diff --git a/docs/upgrade-guides/from-v1-to-v2.md b/docs/upgrade-guides/from-v1-to-v2.md new file mode 100644 index 0000000..4986b67 --- /dev/null +++ b/docs/upgrade-guides/from-v1-to-v2.md @@ -0,0 +1,18 @@ +# Upgrade from v1 to V2 + +Follow the steps below to upgrade the package. + +## Naming changes + +Helpers have been renamed. Make sure you replaced all following occurrences when upgrading: +* Search and replace all occurrences of `classTag(` by `html_classes(` +* Search and replace all occurrences of `HtmlClassTag(` by `HtmlClasses(` +* Search and replace all occurrences of `htmlAttributes(` by `html_attributes(` + +## See all changes + +See all change with the [comparison tool](https://github.com/Okipa/laravel-html-helper/compare/1.4.0...2.0.0). + +## Undocumented changes + +If you see any forgotten and undocumented change, please submit a PR to add them to this upgrade guide. diff --git a/phpcs.xml b/phpcs.xml index 6cbe9a9..44df66c 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,10 +1,9 @@ - - PSR-2 validation + + PSR-12 validation ./src - ./tests/* - + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7b4ca04..af1d6b0 100755 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,6 @@ - + stopOnFailure="false" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> + + + src/ + + + + + tests - - - src/ - - - - - + diff --git a/src/HtmlAttributes.php b/src/HtmlAttributes.php index 9755938..cb372f5 100644 --- a/src/HtmlAttributes.php +++ b/src/HtmlAttributes.php @@ -2,110 +2,83 @@ namespace Okipa\LaravelHtmlHelper; -use Exception; use Illuminate\Support\HtmlString; +use RuntimeException; -class HtmlAttributes extends HtmlHelper +class HtmlAttributes { /** - * Render the generated html. - * - * @param mixed ...$attributesList + * @param string|array|null ...$attributes * * @return \Illuminate\Support\HtmlString - * @throws \Exception */ - public function render(...$attributesList): HtmlString + public function toHtml(...$attributes): HtmlString { - return $this->generateHtmlString(...$attributesList); + return $this->generateHtmlString(...$attributes); } /** - * Render html attributes from the given attributes list. - * - * @param mixed ...$attributesList + * @param string|array|null ...$attributes * * @return \Illuminate\Support\HtmlString - * @throws \Exception */ - protected function generateHtmlString(...$attributesList): HtmlString + protected function generateHtmlString(...$attributes): HtmlString { - $attributesArray = $this->buildAttributesArray(...$attributesList); - $html = $this->buildHtmlString($attributesArray); + $builtAttributes = $this->buildAttributes(...$attributes); + $html = $this->buildHtmlString($builtAttributes); return new HtmlString($html); } - /** - * Build the attributes array - * - * @return array - * @throws \Exception - */ - protected function buildAttributesArray(): array + protected function buildAttributes(): array { - $attributesArray = []; + $attributes = []; foreach (func_get_args() as $arg) { switch (gettype($arg)) { case 'string': - $attributesArray[] = $arg; + $attributes[] = $arg; break; case 'array': - $this->analyseArrayAttributes($arg, $attributesArray); + $this->buildAttributeFromArray($arg, $attributes); break; case 'NULL': break; default: - throw new Exception('The given attributes arguments should be strings or arrays : ' + throw new RuntimeException('The given attributes arguments should be strings or arrays: ' . gettype($arg) . ' type given.'); } } - return array_map('trim', array_filter($attributesArray)); + return array_map('trim', array_filter($attributes)); } - /** - * @param array $array - * @param array $attributes - */ - private function analyseArrayAttributes(array $array, array &$attributes): void + protected function buildAttributeFromArray(array $attribute, array &$attributes): void { - foreach ($array as $key => $value) { + foreach ($attribute as $key => $value) { if (is_array($value)) { - if (! empty($key) && is_string($key)) { + if (is_string($key)) { $attributes[] = $key; } - $this->analyseArrayAttributes($value, $attributes); - } else { - if (! empty($key) && is_string($key)) { - if ($value) { - $attributes[$key] = $value; - } else { - $attributes[] = $key; - } - } else { - if ($value) { - $attributes[] = $value; - } else { - $attributes[] = $key; - } - } + $this->buildAttributeFromArray($value, $attributes); + continue; + } + if (is_string($key) && $value) { + $attributes[$key] = $value; + continue; } + if ($value) { + $attributes[] = $value; + continue; + } + $attributes[] = $key; } } - /** - * Build the html string from the attributes array. - * - * @param array $attributesArray - * - * @return string - */ - protected function buildHtmlString(array $attributesArray): string + protected function buildHtmlString(array $attributes): string { $html = ''; - foreach ($attributesArray as $key => $attribute) { - $spacer = strlen($html) ? ' ' : ''; + foreach ($attributes as $key => $attribute) { + $spacer = $html ? ' ' : ''; if ($key && is_string($key)) { $html .= $spacer . $key . ($attribute ? '="' . $attribute . '"' : ''); } else { @@ -113,6 +86,6 @@ protected function buildHtmlString(array $attributesArray): string } } - return strlen($html) ? ' ' . $html : ''; + return ($html ? ' ' : '') . $html; } } diff --git a/src/HtmlClassTag.php b/src/HtmlClassTag.php deleted file mode 100644 index e045390..0000000 --- a/src/HtmlClassTag.php +++ /dev/null @@ -1,53 +0,0 @@ -generateHtmlClassTag(...$classList); - } - - /** - * Render a html class tag filled with the given class list. - * - * @return \Illuminate\Support\HtmlString - * @throws \Exception - */ - protected function generateHtmlClassTag(): HtmlString - { - $classArray = []; - foreach (func_get_args() as $arg) { - switch (gettype($arg)) { - case 'string': - case 'integer': - $classArray[] = $arg; - break; - case 'array': - $classArray = ! empty($arg) ? array_merge($classArray, $arg) : $classArray; - break; - case 'NULL': - break; - default: - throw new Exception('The given class arguments should be strings, integers or arrays : ' - . gettype($arg) . ' type given.'); - } - } - $classArray = array_map('trim', array_filter(Arr::flatten($classArray))); - - return new HtmlString(! empty($classArray) ? ' class="' . implode(' ', $classArray) . '"' : ''); - } -} diff --git a/src/HtmlClasses.php b/src/HtmlClasses.php new file mode 100644 index 0000000..6dbe6ed --- /dev/null +++ b/src/HtmlClasses.php @@ -0,0 +1,44 @@ +generateHtmlClasses(...$classes); + } + + protected function generateHtmlClasses(): HtmlString + { + $classes = []; + foreach (func_get_args() as $arg) { + switch (gettype($arg)) { + case 'string': + case 'integer': + $classes[] = $arg; + break; + case 'array': + $classes = $arg ? array_merge($classes, $arg) : $classes; + break; + case 'NULL': + break; + default: + throw new RuntimeException('Classes should be strings, integers, arrays or null: ' + . gettype($arg) . ' given.'); + } + } + $classes = array_map('trim', array_filter(Arr::flatten($classes))); + + return new HtmlString($classes ? ' class="' . implode(' ', $classes) . '"' : ''); + } +} diff --git a/src/HtmlHelper.php b/src/HtmlHelper.php deleted file mode 100644 index 023f3d2..0000000 --- a/src/HtmlHelper.php +++ /dev/null @@ -1,28 +0,0 @@ -render(); - } - - /** - * Render the generated html. - * - * @param mixed ...$args - * - * @return \Illuminate\Support\HtmlString - */ - abstract public function render(...$args): HtmlString; -} diff --git a/src/HtmlHelperServiceProvider.php b/src/HtmlHelperServiceProvider.php deleted file mode 100644 index 6aba20d..0000000 --- a/src/HtmlHelperServiceProvider.php +++ /dev/null @@ -1,16 +0,0 @@ -app->singleton('Okipa\LaravelHtmlHelper', function (Application $app) { - return $app->make(HtmlHelper::class); - }); - } -} diff --git a/src/helpers.php b/src/helpers.php index 350768d..56303ed 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -2,30 +2,28 @@ use Illuminate\Support\HtmlString; use Okipa\LaravelHtmlHelper\HtmlAttributes; -use Okipa\LaravelHtmlHelper\HtmlClassTag; +use Okipa\LaravelHtmlHelper\HtmlClasses; -if (! function_exists('classTag')) { +if (! function_exists('html_classes')) { /** - * @param mixed ...$classList + * @param string|int|array|null ...$classList * * @return \Illuminate\Support\HtmlString - * @throws \Exception */ - function classTag(...$classList): HtmlString + function html_classes(...$classList): HtmlString { - return (new HtmlClassTag)->render(...$classList); + return app(HtmlClasses::class)->toHtml(...$classList); } } -if (! function_exists('htmlAttributes')) { +if (! function_exists('html_attributes')) { /** - * @param mixed ...$attributesList + * @param string|array|null ...$attributesList * * @return \Illuminate\Support\HtmlString - * @throws \Exception */ - function htmlAttributes(...$attributesList): HtmlString + function html_attributes(...$attributesList): HtmlString { - return (new HtmlAttributes)->render(...$attributesList); + return app(HtmlAttributes::class)->toHtml(...$attributesList); } } diff --git a/tests/HtmlHelperTestCase.php b/tests/HtmlHelperTestCase.php index 7d4deda..a9974f8 100644 --- a/tests/HtmlHelperTestCase.php +++ b/tests/HtmlHelperTestCase.php @@ -2,20 +2,9 @@ namespace Okipa\LaravelHtmlHelper\Test; -use Okipa\LaravelHtmlHelper\HtmlHelperServiceProvider; use Orchestra\Testbench\TestCase; abstract class HtmlHelperTestCase extends TestCase { - /** - * Get package providers. - * - * @param \Illuminate\Foundation\Application $app - * - * @return array - */ - protected function getPackageProviders($app) - { - return [HtmlHelperServiceProvider::class]; - } + // } diff --git a/tests/Unit/ClassTagTest.php b/tests/Unit/ClassTagTest.php deleted file mode 100644 index 989d125..0000000 --- a/tests/Unit/ClassTagTest.php +++ /dev/null @@ -1,67 +0,0 @@ - 'class6']], - [], - 7 - ); - $this->assertEquals(' class="class1 class2 class3 class4 class5 class6 7"', $html); - } - - public function testEmptyClassTagHtml() - { - $this->assertEquals('', classTag([])); - } - - public function testSuccessRenderClassTagHtmlFromClass() - { - $html = app(\Okipa\LaravelHtmlHelper\HtmlClassTag::class)->render( - 'class1', - ['class2', 'class3', null], - null, - [], - ['class4', ['class5 ', 'class6Key' => 'class6']], - 7 - ); - $this->assertEquals(' class="class1 class2 class3 class4 class5 class6 7"', $html); - } - - public function testFailRenderClassTagHtmlWithObjectGiven() - { - $this->expectException(Exception::class); - classTag(new stdClass()); - } - - public function testFailRenderClassTagHtmlWithDoubleGiven() - { - $this->expectException(Exception::class); - classTag(12.7); - } - - public function testFailRenderClassTagHtmlWithBooleanGiven() - { - $this->expectException(Exception::class); - classTag(true); - } - - public function testRenderedHtml() - { - view()->addNamespace('htmlHelper', 'tests/views'); - $html = view('htmlHelper::classTag')->render(); - $this->assertStringContainsString('
', $html); - } -} diff --git a/tests/Unit/HtmlAttributesTest.php b/tests/Unit/HtmlAttributesTest.php index 1cb8644..807be27 100644 --- a/tests/Unit/HtmlAttributesTest.php +++ b/tests/Unit/HtmlAttributesTest.php @@ -9,9 +9,10 @@ class HtmlAttributesTest extends HtmlHelperTestCase { - public function testSuccessRenderAttributesHtmlFromHelper() + /** @test */ + public function it_generates_html_attributes_from_helper_call(): void { - $html = htmlAttributes( + $html = html_attributes( 'attribute1Value', ['attribute2Key' => 'attribute2Value'], ['attribute3Key' => null], @@ -23,7 +24,7 @@ public function testSuccessRenderAttributesHtmlFromHelper() ['attribute10Key' => ['attribute11Value']], ['attribute12Key' => ''] ); - $this->assertEquals( + self::assertEquals( ' attribute1Value attribute2Key="attribute2Value" attribute3Key attribute4Value ' . 'attribute5Value attribute6Value attributes7Value attribute8Value attribute9Key="attribute9Value" ' . 'attribute10Key attribute11Value attribute12Key', @@ -31,9 +32,10 @@ public function testSuccessRenderAttributesHtmlFromHelper() ); } - public function testSuccessRenderAttributesHtmlFromClass() + /** @test */ + public function it_generates_html_attributes_from_class_call(): void { - $html = app(HtmlAttributes::class)->render( + $html = app(HtmlAttributes::class)->toHtml( 'attribute1Value', ['attribute2Key' => 'attribute2Value'], ['attribute3Key' => null], @@ -45,7 +47,7 @@ public function testSuccessRenderAttributesHtmlFromClass() ['attribute10Key' => ['attribute11Value']], ['attribute12Key' => ''] ); - $this->assertEquals( + self::assertEquals( ' attribute1Value attribute2Key="attribute2Value" attribute3Key attribute4Value attribute5Value ' . 'attribute6Value attributes7Value attribute8Value attribute9Key="attribute9Value" attribute10Key ' . 'attribute11Value attribute12Key', @@ -53,35 +55,40 @@ public function testSuccessRenderAttributesHtmlFromClass() ); } - public function testFailRenderAttributesHtmlWithIntGiven() + /** @test */ + public function it_triggers_exception_with_object(): void { $this->expectException(Exception::class); - classTag(htmlAttributes(10)); + html_attributes(new stdClass()); } - public function testFailRenderAttributesHtmlWithObjectGiven() + /** @test */ + public function it_triggers_exception_with_double(): void { $this->expectException(Exception::class); - htmlAttributes(new stdClass()); + html_attributes(12.7); } - public function testFailRenderAttributesHtmlWithDoubleGiven() + /** @test */ + public function it_triggers_exception_with_bool(): void { $this->expectException(Exception::class); - htmlAttributes(12.7); + html_attributes(true); } - public function testFailRenderAttributesHtmlWithBooleanGiven() + /** @test */ + public function it_triggers_exception_with_int(): void { $this->expectException(Exception::class); - htmlAttributes(true); + html_classes(html_attributes(10)); } - public function testRenderedHtml() + /** @test */ + public function it_renders_html_attributes_in_view(): void { - view()->addNamespace('htmlHelper', 'tests/views'); - $html = view('htmlHelper::htmlAttributes')->render(); - $this->assertStringContainsString( + view()->addNamespace('html_helper', 'tests/views'); + $html = view('html_helper::html-attributes')->toHtml(); + self::assertStringContainsString( '
', @@ -89,10 +96,11 @@ public function testRenderedHtml() ); } - public function testHtmlNoAttributesRenderedHtml() + /** @test */ + public function it_renders_no_html_attributes_in_view(): void { - view()->addNamespace('htmlHelper', 'tests/views'); - $html = view('htmlHelper::noHtmlAttributes')->render(); - $this->assertStringContainsString('
', $html); + view()->addNamespace('html_helper', 'tests/views'); + $html = view('html_helper::no-html-attributes')->toHtml(); + self::assertStringContainsString('
', $html); } } diff --git a/tests/Unit/HtmlClassesTest.php b/tests/Unit/HtmlClassesTest.php new file mode 100644 index 0000000..3de6dab --- /dev/null +++ b/tests/Unit/HtmlClassesTest.php @@ -0,0 +1,77 @@ + 'class6']], + [], + 7 + ); + self::assertEquals(' class="class1 class2 class3 class4 class5 class6 7"', $html); + } + + /** @test */ + public function it_generates_html_classes_from_class_call(): void + { + $html = app(HtmlClasses::class)->toHtml( + 'class1', + ['class2', 'class3', null], + null, + [], + ['class4', ['class5 ', 'class6Key' => 'class6']], + 7 + ); + self::assertEquals(' class="class1 class2 class3 class4 class5 class6 7"', $html); + } + + /** @test */ + public function it_returns_empty_string_with_empty_inputs(): void + { + self::assertEquals('', html_classes([])); + self::assertEquals('', html_classes(null)); + self::assertEquals('', html_classes('')); + } + + /** @test */ + public function it_triggers_exception_with_object(): void + { + $this->expectException(Exception::class); + html_classes(new stdClass()); + } + + /** @test */ + public function it_triggers_exception_with_double(): void + { + $this->expectException(Exception::class); + html_classes(12.7); + } + + /** @test */ + public function it_triggers_exception_with_bool(): void + { + $this->expectException(Exception::class); + html_classes(true); + } + + /** @test */ + public function it_renders_html_classes_in_view(): void + { + view()->addNamespace('html_helper', 'tests/views'); + $html = view('html_helper::html-classes')->toHtml(); + self::assertStringContainsString('
', $html); + } +} diff --git a/tests/views/classTag.blade.php b/tests/views/classTag.blade.php deleted file mode 100644 index d53e87f..0000000 --- a/tests/views/classTag.blade.php +++ /dev/null @@ -1 +0,0 @@ - 'class6']], 7) }}> diff --git a/tests/views/html-attributes.blade.php b/tests/views/html-attributes.blade.php new file mode 100644 index 0000000..8097fe1 --- /dev/null +++ b/tests/views/html-attributes.blade.php @@ -0,0 +1 @@ + 'attribute2Value'], ['attribute3Key' => null], ['attribute4Value', 'attribute5Value'], '', null, ['' => 'attribute6Value'], ['attributes7Value', ['attribute8Value', 'attribute9Key' => 'attribute9Value']], ['attribute10Key' => ['attribute11Value']], ['attribute12Key' => '']) }}> diff --git a/tests/views/html-classes.blade.php b/tests/views/html-classes.blade.php new file mode 100644 index 0000000..2ca452a --- /dev/null +++ b/tests/views/html-classes.blade.php @@ -0,0 +1 @@ + 'class6']], 7) }}> diff --git a/tests/views/htmlAttributes.blade.php b/tests/views/htmlAttributes.blade.php deleted file mode 100644 index bdf6be4..0000000 --- a/tests/views/htmlAttributes.blade.php +++ /dev/null @@ -1 +0,0 @@ - 'attribute2Value'], ['attribute3Key' => null], ['attribute4Value', 'attribute5Value'], '', null, ['' => 'attribute6Value'], ['attributes7Value', ['attribute8Value', 'attribute9Key' => 'attribute9Value']], ['attribute10Key' => ['attribute11Value']], ['attribute12Key' => '']) }}> diff --git a/tests/views/no-html-attributes.blade.php b/tests/views/no-html-attributes.blade.php new file mode 100644 index 0000000..a0a5e55 --- /dev/null +++ b/tests/views/no-html-attributes.blade.php @@ -0,0 +1 @@ + diff --git a/tests/views/noHtmlAttributes.blade.php b/tests/views/noHtmlAttributes.blade.php deleted file mode 100644 index 8680e3f..0000000 --- a/tests/views/noHtmlAttributes.blade.php +++ /dev/null @@ -1 +0,0 @@ -