diff --git a/CHANGELOG.md b/CHANGELOG.md index bf565fc8a..785efac97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to `laravel-livewire-tables` will be documented in this file +## [Unreleased] - 3.x - Updates to Injection Methods +- Amend AutoInjection/FrontendAsset to ensure it returns the original content correctly +- Remove errant disabling of Blade Directives when disabling auto-injection +- Amended in-line config documentation + ## [Unreleased] - 3.x - setSearchFieldAttributes - Add setSearchFieldAttributes() and getSearchFieldAttributes() diff --git a/README copy.md b/README copy.md deleted file mode 100644 index bce1fceab..000000000 --- a/README copy.md +++ /dev/null @@ -1,94 +0,0 @@ -![Package Logo](https://banners.beyondco.de/Laravel%20Livewire%20Tables.png?theme=light&packageName=rappasoft%2Flaravel-livewire-tables&pattern=hideout&style=style_1&description=A+dynamic+table+component+for+Laravel+Livewire&md=1&fontSize=100px&images=table) - -[![Latest Version on Packagist](https://img.shields.io/packagist/v/rappasoft/laravel-livewire-tables.svg?style=flat-square)](https://packagist.org/packages/rappasoft/laravel-livewire-tables) -[![Styling](https://github.com/rappasoft/laravel-livewire-tables/actions/workflows/php-cs-fixer.yml/badge.svg)](https://github.com/rappasoft/laravel-livewire-tables/actions/workflows/php-cs-fixer.yml) -[![Tests](https://github.com/rappasoft/laravel-livewire-tables/actions/workflows/run-tests.yml/badge.svg)](https://github.com/rappasoft/laravel-livewire-tables/actions/workflows/run-tests.yml) -[![Total Downloads](https://img.shields.io/packagist/dt/rappasoft/laravel-livewire-tables.svg?style=flat-square)](https://packagist.org/packages/rappasoft/laravel-livewire-tables) - -### Enjoying this package? [Buy me a beer 🍺](https://www.buymeacoffee.com/rappasoft) - -A dynamic Laravel Livewire component for data tables. - -![Dark Mode](https://imgur.com/QoEdC7n.png) - -![Full Table](https://i.imgur.com/2kfibjR.png) - -### [Bootstrap 4 Demo](https://tables.laravel-boilerplate.com/bootstrap-4) | [Bootstrap 5 Demo](https://tables.laravel-boilerplate.com/bootstrap-5) | [Tailwind Demo](https://tables.laravel-boilerplate.com/tailwind) | [Demo Repository](https://github.com/rappasoft/laravel-livewire-tables-demo) - -## Installation - -You can install the package via composer: - -``` bash -composer require rappasoft/laravel-livewire-tables -``` - -You must also have [Alpine.js](https://alpinejs.dev) version 3 or greater installed and available to the component. - -## Documentation and Usage Instructions - -See the [documentation](https://rappasoft.com/docs/laravel-livewire-tables) for detailed installation and usage instructions. - -## Basic Example - -```php -setPrimaryKey('id'); - } - - public function columns(): array - { - return [ - Column::make('ID', 'id') - ->sortable(), - Column::make('Name') - ->sortable(), - ]; - } -} - -``` - -### [See advanced example](https://rappasoft.com/docs/laravel-livewire-tables/v2/examples/advanced-example) - -## Testing - -```bash -composer test -``` - -## Changelog - -Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. - -## Contributing - -Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. - -## Security Vulnerabilities - -Please e-mail anthony@rappasoft.com to report any security vulnerabilities instead of the issue tracker. - -## Credits - -- [Anthony Rappa](https://github.com/rappasoft) -- [Joe McElwee](https://github.com/lrljoe) -- [All Contributors](./CONTRIBUTORS.md) - -## License - -The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/config/livewire-tables.php b/config/livewire-tables.php index 3161f9280..3dbdf1757 100644 --- a/config/livewire-tables.php +++ b/config/livewire-tables.php @@ -6,26 +6,34 @@ */ 'theme' => 'tailwind', - 'cache_assets' => true, /** - * Enable or Disable automatic injection of assets + * Filter Frontend Asset Options */ - 'inject_assets' => true, /** - * Enable or Disable automatic injection of assets + * Cache Rappasoft Frontend Assets */ - 'inject_third_party_assets' => false, + 'cache_assets' => false, /** - * Enable or Disable inclusion of published third-party assets + * Enable or Disable automatic injection of core assets */ - 'published_third_party_assets' => false, + 'inject_core_assets_enabled' => true, /** - * Enable or Disable remote third-party assets + * Enable or Disable automatic injection of third-party assets */ - 'remote_third_party_assets' => true, + 'inject_third_party_assets_enabled' => true, + + /** + * Enable Blade Directives (Not required if automatically injecting or using bundler approaches) + */ + 'enable_blade_directives ' => false, + + /** + * Filter Default Configuration Options + * + * */ /** * Configuration options for DateFilter diff --git a/resources/imports/laravel-livewire-tables-thirdparty.js b/resources/imports/laravel-livewire-tables-thirdparty.js new file mode 100644 index 000000000..8dca724bb --- /dev/null +++ b/resources/imports/laravel-livewire-tables-thirdparty.js @@ -0,0 +1,2 @@ +import './css/laravel-livewire-tables-thirdparty.css' +import './js/laravel-livewire-tables-thirdparty.min.js' diff --git a/resources/imports/laravel-livewire-tables.js b/resources/imports/laravel-livewire-tables.js new file mode 100644 index 000000000..0048c0c16 --- /dev/null +++ b/resources/imports/laravel-livewire-tables.js @@ -0,0 +1,2 @@ +import './css/laravel-livewire-tables.css' +import './js/laravel-livewire-tables.js' diff --git a/src/Features/AutoInjectRappasoftAssets.php b/src/Features/AutoInjectRappasoftAssets.php index 791bf876c..73e2c5e12 100644 --- a/src/Features/AutoInjectRappasoftAssets.php +++ b/src/Features/AutoInjectRappasoftAssets.php @@ -14,52 +14,61 @@ class AutoInjectRappasoftAssets extends ComponentHook public static bool $forceAssetInjection = false; + public static bool $shouldInjectRappasoftThirdPartyAssets = false; + + public static bool $shouldInjectRappasoftAssets = false; + public static function provide(): void { + static::$shouldInjectRappasoftAssets = config('livewire-tables.inject_core_assets_enabled', true); + static::$shouldInjectRappasoftThirdPartyAssets = config('livewire-tables.inject_third_party_assets_enabled', true); + on('flush-state', function () { static::$hasRenderedAComponentThisRequest = false; static::$forceAssetInjection = false; }); - if (config('livewire-tables.inject_assets', true) === false) { - return; + if (static::$shouldInjectRappasoftAssets || static::$shouldInjectRappasoftThirdPartyAssets) { + + app('events')->listen(RequestHandled::class, function (RequestHandled $handled) { + + if (! static::$shouldInjectRappasoftAssets && ! static::$shouldInjectRappasoftThirdPartyAssets) { + return; + } + + // If All Scripts Have Been Rendered - Return + if ( + ( + ! static::$shouldInjectRappasoftAssets || app(RappasoftFrontendAssets::class)->hasRenderedRappsoftTableScripts + ) && + ( + ! static::$shouldInjectRappasoftThirdPartyAssets || app(RappasoftFrontendAssets::class)->hasRenderedRappsoftTableThirdPartyScripts + ) + ) { + return; + } + + if (! str($handled->response->headers->get('content-type'))->contains('text/html')) { + return; + } + + if (! method_exists($handled->response, 'status') || ! method_exists($handled->response, 'getContent') || ! method_exists($handled->response, 'setContent') || ! method_exists($handled->response, 'getOriginalContent') || ! property_exists($handled->response, 'original')) { + return; + } + + if ($handled->response->status() !== 200) { + return; + } + + $html = $handled->response->getContent(); + + if (str($html)->contains('')) { + $original = $handled->response->getOriginalContent(); + $handled->response->setContent(static::injectAssets($html)); + $handled->response->original = $original; + } + }); } - - app('events')->listen(RequestHandled::class, function ($handled) { - - if (! static::$forceAssetInjection && config('livewire-tables.inject_assets', true) === false) { - return; - } - if (! str($handled->response->headers->get('content-type'))->contains('text/html')) { - return; - } - if (! method_exists($handled->response, 'status') || $handled->response->status() !== 200) { - return; - } - - if (! method_exists($handled->response, 'getContent') || ! method_exists($handled->response, 'setContent')) { - return; - } - - if ((! static::$hasRenderedAComponentThisRequest) && (! static::$forceAssetInjection)) { - return; - } - if (config('livewire-tables.inject_assets', true) === false && config('livewire-tables.inject_third_party_assets', true) === false) { - return; - } - if (config('livewire-tables.inject_assets', true) === true && app(RappasoftFrontendAssets::class)->hasRenderedRappsoftTableScripts && config('livewire-tables.inject_third_party_assets', true) === true && app(RappasoftFrontendAssets::class)->hasRenderedRappsoftTableThirdPartyScripts) { - return; - } elseif (config('livewire-tables.inject_assets', true) === true && app(RappasoftFrontendAssets::class)->hasRenderedRappsoftTableScripts) { - return; - } elseif (config('livewire-tables.inject_third_party_assets', true) === true && app(RappasoftFrontendAssets::class)->hasRenderedRappsoftTableThirdPartyScripts) { - return; - } - $html = $handled->response->getContent(); - - if (str($html)->contains('')) { - $handled->response->setContent(static::injectAssets($html)); - } - }); } public function dehydrate(): void @@ -69,25 +78,24 @@ public function dehydrate(): void public static function injectAssets(mixed $html): string { - $rappasoftTableStyles = config('livewire-tables.inject_assets', true) ? RappasoftFrontendAssets::tableStyles() : ''; - $rappasoftTableScripts = config('livewire-tables.inject_assets', true) ? RappasoftFrontendAssets::tableScripts() : ''; - $rappasoftTableThirdPartyStyles = config('livewire-tables.inject_third_party_assets', true) ? RappasoftFrontendAssets::tableThirdPartyStyles() : ''; - //$rappasoftTableThirdPartyStyles = ''; - $rappasoftTableThirdPartyScripts = config('livewire-tables.inject_third_party_assets', true) ? RappasoftFrontendAssets::tableThirdPartyScripts() : ''; - //$rappasoftTableThirdPartyScripts = ''; $html = str($html); + $rappaStyles = ((static::$shouldInjectRappasoftAssets === true) ? RappasoftFrontendAssets::tableStyles() : '').' '.((static::$shouldInjectRappasoftThirdPartyAssets === true) ? RappasoftFrontendAssets::tableThirdPartyStyles() : ''); + $rappaScripts = ((static::$shouldInjectRappasoftAssets === true) ? RappasoftFrontendAssets::tableScripts() : '').' '.((static::$shouldInjectRappasoftThirdPartyAssets === true) ? RappasoftFrontendAssets::tableThirdPartyScripts() : ''); if ($html->test('/<\s*head(?:\s|\s[^>])*>/i') && $html->test('/<\s*\/\s*body\s*>/i')) { + static::$shouldInjectRappasoftAssets = static::$shouldInjectRappasoftThirdPartyAssets = false; + return $html - ->replaceMatches('/(<\s*head(?:\s|\s[^>])*>)/i', '$1'.($rappasoftTableStyles.' '.$rappasoftTableThirdPartyStyles)) - ->replaceMatches('/(<\s*\/\s*head\s*>)/i', ($rappasoftTableScripts.' '.$rappasoftTableThirdPartyScripts).'$1') + ->replaceMatches('/(<\s*head(?:\s|\s[^>])*>)/i', '$1'.$rappaStyles) + ->replaceMatches('/(<\s*\/\s*head\s*>)/i', $rappaScripts.'$1') ->toString(); } + static::$shouldInjectRappasoftAssets = static::$shouldInjectRappasoftThirdPartyAssets = false; return $html - ->replaceMatches('/(<\s*html(?:\s[^>])*>)/i', '$1'.($rappasoftTableStyles.' '.$rappasoftTableThirdPartyStyles)) - ->replaceMatches('/(<\s*\/\s*head\s*>)/i', ($rappasoftTableScripts.' '.$rappasoftTableThirdPartyScripts).'$1') + ->replaceMatches('/(<\s*html(?:\s[^>])*>)/i', '$1'.$rappaStyles) + ->replaceMatches('/(<\s*\/\s*head\s*>)/i', $rappaScripts.'$1') ->toString(); } } diff --git a/src/LaravelLivewireTablesServiceProvider.php b/src/LaravelLivewireTablesServiceProvider.php index fe6387d96..92be82358 100644 --- a/src/LaravelLivewireTablesServiceProvider.php +++ b/src/LaravelLivewireTablesServiceProvider.php @@ -24,6 +24,14 @@ public function boot(): void $this->loadViewsFrom(__DIR__.'/../resources/views', 'livewire-tables'); + $this->consoleCommands(); + + (new RappasoftFrontendAssets)->boot(); + + } + + public function consoleCommands() + { if ($this->app->runningInConsole()) { $this->publishes([ __DIR__.'/../resources/lang' => $this->app->langPath('livewire-tables'), @@ -45,12 +53,6 @@ public function boot(): void MakeCommand::class, ]); } - - if (config('livewire-tables.inject_assets', true) === true) { - - (new RappasoftFrontendAssets)->boot(); - } - } public function register(): void @@ -58,10 +60,10 @@ public function register(): void $this->mergeConfigFrom( __DIR__.'/../config/livewire-tables.php', 'livewire-tables' ); - if (config('livewire-tables.inject_assets', true) === true) { - (new RappasoftFrontendAssets)->register(); - ComponentHookRegistry::register(AutoInjectRappasoftAssets::class); - } + (new RappasoftFrontendAssets)->register(); + + ComponentHookRegistry::register(AutoInjectRappasoftAssets::class); + } } diff --git a/src/Mechanisms/RappasoftFrontendAssets.php b/src/Mechanisms/RappasoftFrontendAssets.php index 3af34cb5c..8788d0f61 100644 --- a/src/Mechanisms/RappasoftFrontendAssets.php +++ b/src/Mechanisms/RappasoftFrontendAssets.php @@ -31,8 +31,6 @@ public function boot(): void return Route::get($scriptPath, $handle); }); - Blade::directive('rappasoftTableScripts', [static::class, 'rappasoftTableScripts']); - // Set the CSS route for the core tables CSS app($this::class)->setRappasoftTableStylesRoute(function ($handle) { $stylesPath = '/rappasoft/laravel-livewire-tables/core.min.css'; @@ -40,8 +38,6 @@ public function boot(): void return Route::get($stylesPath, $handle); }); - Blade::directive('rappasoftTableStyles', [static::class, 'rappasoftTableStyles']); - // Set the JS route for the third party JS app($this::class)->setRappasoftTableThirdPartyScriptRoute(function ($handle) { $scriptPath = '/rappasoft/laravel-livewire-tables/thirdparty.min.js'; @@ -49,8 +45,6 @@ public function boot(): void return Route::get($scriptPath, $handle); }); - Blade::directive('rappasoftTableThirdPartyScripts', [static::class, 'rappasoftTableThirdPartyScripts']); - // Set the CSS route for the third party CSS app($this::class)->setRappasoftTableThirdPartyStylesRoute(function ($handle) { $stylesPath = '/rappasoft/laravel-livewire-tables/thirdparty.css'; @@ -58,16 +52,30 @@ public function boot(): void return Route::get($stylesPath, $handle); }); - Blade::directive('rappasoftTableThirdPartyStyles', [static::class, 'rappasoftTableThirdPartyStyles']); + static::registerBladeDirectives(); } + protected function registerBladeDirectives() + { + Blade::directive('rappasoftTableScripts', [static::class, 'rappasoftTableScripts']); + Blade::directive('rappasoftTableStyles', [static::class, 'rappasoftTableStyles']); + Blade::directive('rappasoftTableThirdPartyScripts', [static::class, 'rappasoftTableThirdPartyScripts']); + Blade::directive('rappasoftTableThirdPartyStyles', [static::class, 'rappasoftTableThirdPartyStyles']); + } + protected function pretendResponseIsJs(string $file): \Symfony\Component\HttpFoundation\Response { - $expires = strtotime((config('livewire-tables.cache_assets', false) ? '+1 second' : '+1 hour')); - $lastModified = (config('livewire-tables.cache_assets', false) ? strtotime(now()) : filemtime($file)); - $cacheControl = (config('livewire-tables.cache_assets', false) ? 'public, max-age=1' : 'public, max-age=3600'); + if (config('livewire-tables.cache_assets', false) === true) { + $expires = strtotime('+1 hour'); + $lastModified = filemtime($file); + $cacheControl = 'public, max-age=3600'; + } else { + $expires = strtotime('+1 second'); + $lastModified = \Carbon\Carbon::now()->timestamp; + $cacheControl = 'public, max-age=1'; + } $headers = [ 'Content-Type' => 'application/javascript; charset=utf-8', @@ -81,9 +89,15 @@ protected function pretendResponseIsJs(string $file): \Symfony\Component\HttpFou protected function pretendResponseIsCSS(string $file): \Symfony\Component\HttpFoundation\Response { - $expires = strtotime((config('livewire-tables.cache_assets', false) ? '+1 second' : '+1 hour')); - $lastModified = (config('livewire-tables.cache_assets', false) ? strtotime(now()) : filemtime($file)); - $cacheControl = (config('livewire-tables.cache_assets', false) ? 'public, max-age=1' : 'public, max-age=3600'); + if (config('livewire-tables.cache_assets', false) === true) { + $expires = strtotime('+1 hour'); + $lastModified = filemtime($file); + $cacheControl = 'public, max-age=3600'; + } else { + $expires = strtotime('+1 second'); + $lastModified = \Carbon\Carbon::now()->timestamp; + $cacheControl = 'public, max-age=1'; + } $headers = [ 'Content-Type' => 'text/css; charset=utf-8', diff --git a/src/Traits/Mechanisms/WithRappasoftTableScripts.php b/src/Traits/Mechanisms/WithRappasoftTableScripts.php index 0061e6ea3..b2f13a344 100644 --- a/src/Traits/Mechanisms/WithRappasoftTableScripts.php +++ b/src/Traits/Mechanisms/WithRappasoftTableScripts.php @@ -50,7 +50,7 @@ public static function tableScripts(array $options = []): ?string $scripts = static::tableJs($options); // HTML Label. - $html = $debug ? [''] : []; + $html = $debug ? [''] : []; $html[] = $scripts; @@ -75,7 +75,7 @@ public static function tableJs(array $options = []): string ); return << + HTML; } } diff --git a/src/Traits/Mechanisms/WithRappasoftTableStyles.php b/src/Traits/Mechanisms/WithRappasoftTableStyles.php index 261c1eded..6f663a4d1 100644 --- a/src/Traits/Mechanisms/WithRappasoftTableStyles.php +++ b/src/Traits/Mechanisms/WithRappasoftTableStyles.php @@ -43,7 +43,7 @@ public static function tableStyles(array $options = []): array|string|null $styles = static::tableCss($options); // HTML Label. - $html = $debug ? [''] : []; + $html = $debug ? [''] : []; $html[] = $styles; diff --git a/src/Traits/Mechanisms/WithRappasoftTableThirdPartyScripts.php b/src/Traits/Mechanisms/WithRappasoftTableThirdPartyScripts.php index e6bc28408..453a8c14f 100644 --- a/src/Traits/Mechanisms/WithRappasoftTableThirdPartyScripts.php +++ b/src/Traits/Mechanisms/WithRappasoftTableThirdPartyScripts.php @@ -73,7 +73,7 @@ public static function tableThirdpartyJs(array $options = []): string ); return << + HTML; } } diff --git a/tests/DataTransferObjects/DebuggableDataTest.php b/tests/DataTransferObjects/DebuggableDataTest.php new file mode 100644 index 000000000..efb0b24e2 --- /dev/null +++ b/tests/DataTransferObjects/DebuggableDataTest.php @@ -0,0 +1,50 @@ +assertTrue(true); + } + + /** @test */ + public function test_example2() + { + $this->assertSame($this->basicTable->sortBy('id'), 'asc'); + } + + /** @test */ + public function test_check_all_default_dto_elements() + { + $debuggableDTO = new DebuggableData($this->basicTable); + $debuggableArray = $debuggableDTO->toArray(); + + $defaultQuery = 'select "pets"."id" as "id", "pets"."sort" as "sort", "pets"."name" as "name", "pets"."age" as "age", "breed"."name" as "breed.name", "pets"."last_visit" as "last_visit" from "pets" left join "breeds" as "breed" on "pets"."breed_id" = "breed"."id" limit 10 offset 0'; + $this->assertSame($debuggableArray['query'], $defaultQuery); + $this->assertSame($debuggableArray['filters'], []); + $this->assertSame($debuggableArray['sorts'], []); + $this->assertSame($debuggableArray['search'], ''); + $this->assertFalse($debuggableArray['select-all']); + $this->assertSame($debuggableArray['selected'], []); + } + + /** @test */ + public function test_check_dto_returns_filters_correctly() + { + $debuggableDTO = new DebuggableData($this->basicTable); + $debuggableArray = $debuggableDTO->toArray(); + $this->assertSame($debuggableArray['filters'], []); + + $this->basicTable->setFilter('breed', ['1']); + $debuggableDTO = new DebuggableData($this->basicTable); + $debuggableArray = $debuggableDTO->toArray(); + $this->assertSame($debuggableArray['filters'], ['breed' => ['1']]); + + } +} diff --git a/tests/Features/AutoInjectRappasoftAssetsTest.php b/tests/Features/AutoInjectRappasoftAssetsTest.php index 61b306bfe..4d196eac0 100644 --- a/tests/Features/AutoInjectRappasoftAssetsTest.php +++ b/tests/Features/AutoInjectRappasoftAssetsTest.php @@ -10,42 +10,50 @@ class AutoInjectRappasoftAssetsTest extends TestCase /** @test */ public function shouldInjectRappasoftAndThirdParty() { - config()->set('livewire-tables.inject_assets', true); - config()->set('livewire-tables.inject_third_party_assets', true); - config()->set('livewire-tables.published_third_party_assets', false); - config()->set('livewire-tables.remote_third_party_assets', false); - $this->assertEquals(' ', AutoInjectRappasoftAssets::injectAssets('')); + config()->set('livewire-tables.inject_core_assets_enabled', true); + config()->set('livewire-tables.inject_third_party_assets_enabled', true); + + $injectionReturn = AutoInjectRappasoftAssets::injectAssets(''); + + $this->assertStringContainsStringIgnoringCase('', $injectionReturn); + $this->assertStringContainsStringIgnoringCase('', $injectionReturn); + $this->assertStringContainsStringIgnoringCase('', $injectionReturn); + $this->assertStringContainsStringIgnoringCase('', $injectionReturn); } /** @test */ public function shouldNotInjectRappasoftOrThirdParty() { - config()->set('livewire-tables.inject_assets', false); - config()->set('livewire-tables.inject_third_party_assets', false); - config()->set('livewire-tables.published_third_party_assets', false); - config()->set('livewire-tables.remote_third_party_assets', false); + config()->set('livewire-tables.inject_core_assets_enabled', false); + config()->set('livewire-tables.inject_third_party_assets_enabled', false); + + $injectionReturn = AutoInjectRappasoftAssets::injectAssets(''); + $this->assertEquals(' ', AutoInjectRappasoftAssets::injectAssets('')); } /** @test */ public function shouldOnlyInjectThirdParty() { - config()->set('livewire-tables.inject_assets', false); - config()->set('livewire-tables.inject_third_party_assets', true); - config()->set('livewire-tables.published_third_party_assets', false); - config()->set('livewire-tables.remote_third_party_assets', false); + config()->set('livewire-tables.inject_core_assets_enabled', false); + config()->set('livewire-tables.inject_third_party_assets_enabled', true); - $this->assertEquals(' ', AutoInjectRappasoftAssets::injectAssets('')); + $injectionReturn = AutoInjectRappasoftAssets::injectAssets(''); + + $this->assertStringContainsStringIgnoringCase('', $injectionReturn); + $this->assertStringContainsStringIgnoringCase('', $injectionReturn); } /** @test */ public function shouldOnlyInjectRappasoft() { - config()->set('livewire-tables.inject_assets', true); - config()->set('livewire-tables.inject_third_party_assets', false); - config()->set('livewire-tables.published_third_party_assets', false); - config()->set('livewire-tables.remote_third_party_assets', false); + config()->set('livewire-tables.inject_core_assets_enabled', true); + config()->set('livewire-tables.inject_third_party_assets_enabled', false); + + $injectionReturn = AutoInjectRappasoftAssets::injectAssets(''); + + $this->assertStringContainsStringIgnoringCase('', $injectionReturn); + $this->assertStringContainsStringIgnoringCase('', $injectionReturn); - $this->assertEquals(' ', AutoInjectRappasoftAssets::injectAssets('')); } } diff --git a/tests/Mechanisms/RappasoftFrontendAssetsTest.php b/tests/Mechanisms/RappasoftFrontendAssetsTest.php index 34832e102..32867efff 100644 --- a/tests/Mechanisms/RappasoftFrontendAssetsTest.php +++ b/tests/Mechanisms/RappasoftFrontendAssetsTest.php @@ -7,6 +7,148 @@ class RappasoftFrontendAssetsTest extends TestCase { + /** + * @test + */ + public function jsResponseSetupCacheEnabled(): array + { + config()->set('livewire-tables.cache_assets', true); + $lastModified = \Carbon\Carbon::now()->timestamp; + $assets = app(RappasoftFrontendAssets::class); + $response = $assets->returnRappasoftTableJavaScriptAsFile(); + $this->assertIsObject($response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\BinaryFileResponse::class, $response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\ResponseHeaderBag::class, $response->headers); + $this->assertIsIterable($response->headers->all()); + + return ['lastModified' => $lastModified, 'responseHeaders' => $response->headers->all()]; + } + + /** + * @test + */ + public function jsResponseSetupCacheDisabled(): array + { + config()->set('livewire-tables.cache_assets', false); + $date = date_create(); + $assets = app(RappasoftFrontendAssets::class); + $response = $assets->returnRappasoftTableJavaScriptAsFile(); + $this->assertIsObject($response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\BinaryFileResponse::class, $response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\ResponseHeaderBag::class, $response->headers); + $this->assertIsIterable($response->headers->all()); + + return ['lastModified' => date_timestamp_get($date), 'responseHeaders' => $response->headers->all()]; + } + + /** + * @test + */ + public function cssResponseSetupCacheEnabled(): array + { + config()->set('livewire-tables.cache_assets', true); + $date = date_create(); + $assets = app(RappasoftFrontendAssets::class); + $response = $assets->returnRappasoftTableStylesAsFile(); + $this->assertIsObject($response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\BinaryFileResponse::class, $response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\ResponseHeaderBag::class, $response->headers); + $this->assertIsIterable($response->headers->all()); + + return ['lastModified' => date_timestamp_get($date), 'responseHeaders' => $response->headers->all()]; + } + + /** + * @test + */ + public function cssResponseSetupCacheDisabled(): array + { + config()->set('livewire-tables.cache_assets', false); + + $date = date_create(); + + $assets = app(RappasoftFrontendAssets::class); + $response = $assets->returnRappasoftTableStylesAsFile(); + $this->assertIsObject($response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\BinaryFileResponse::class, $response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\ResponseHeaderBag::class, $response->headers); + $this->assertIsIterable($response->headers->all()); + + return ['lastModified' => date_timestamp_get($date), 'responseHeaders' => $response->headers->all()]; + } + + /** + * @test + */ + public function thirdPartyCssResponseSetupCacheEnabled(): array + { + config()->set('livewire-tables.cache_assets', true); + + $date = date_create(); + + $assets = app(RappasoftFrontendAssets::class); + $response = $assets->returnRappasoftTableThirdPartyStylesAsFile(); + $this->assertIsObject($response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\BinaryFileResponse::class, $response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\ResponseHeaderBag::class, $response->headers); + $this->assertIsIterable($response->headers->all()); + + return ['lastModified' => date_timestamp_get($date), 'responseHeaders' => $response->headers->all()]; + } + + /** + * @test + */ + public function thirdPartyCssResponseSetupCacheDisabled(): array + { + config()->set('livewire-tables.cache_assets', false); + + $date = date_create(); + + $assets = app(RappasoftFrontendAssets::class); + $response = $assets->returnRappasoftTableThirdPartyStylesAsFile(); + $this->assertIsObject($response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\BinaryFileResponse::class, $response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\ResponseHeaderBag::class, $response->headers); + $this->assertIsIterable($response->headers->all()); + + return ['lastModified' => date_timestamp_get($date), 'responseHeaders' => $response->headers->all()]; + } + + /** + * @test + */ + public function thirdPartyJsResponseSetupCacheEnabled(): array + { + config()->set('livewire-tables.cache_assets', true); + $lastModified = \Carbon\Carbon::now()->timestamp; + $assets = app(RappasoftFrontendAssets::class); + $response = $assets->returnRappasoftTableThirdPartyJavaScriptAsFile(); + $this->assertIsObject($response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\BinaryFileResponse::class, $response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\ResponseHeaderBag::class, $response->headers); + $this->assertIsIterable($response->headers->all()); + + return ['lastModified' => $lastModified, 'responseHeaders' => $response->headers->all()]; + } + + /** + * @test + */ + public function thirdPartyJsResponseSetupCacheDisabled(): array + { + config()->set('livewire-tables.cache_assets', false); + $date = date_create(); + $assets = app(RappasoftFrontendAssets::class); + $response = $assets->returnRappasoftTableThirdPartyJavaScriptAsFile(); + $this->assertIsObject($response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\BinaryFileResponse::class, $response); + $this->assertInstanceOf(\Symfony\Component\HttpFoundation\ResponseHeaderBag::class, $response->headers); + $this->assertIsIterable($response->headers->all()); + + return ['lastModified' => date_timestamp_get($date), 'responseHeaders' => $response->headers->all()]; + } + /** @test */ public function styles() { @@ -54,4 +196,164 @@ public function thirdPartyscripts() $this->assertTrue($assets->hasRenderedRappsoftTableThirdPartyScripts); } + + /** + * @test + * + * @depends jsResponseSetupCacheEnabled + */ + public function check_pretend_response_is_js_returns_correct_cache_control_cache_enabled(array $jsResponseSetupCacheEnabled) + { + $this->assertSame('max-age=3600, public', $jsResponseSetupCacheEnabled['responseHeaders']['cache-control'][0]); + } + + /** + * @test + * + * @depends jsResponseSetupCacheEnabled + */ + public function check_pretend_response_is_js_returns_correct_content_type_cache_enabled(array $jsResponseSetupCacheEnabled) + { + $this->assertSame('application/javascript; charset=utf-8', $jsResponseSetupCacheEnabled['responseHeaders']['content-type'][0]); + } + + /** + * @test + * + * @depends jsResponseSetupCacheDisabled + */ + public function check_pretend_response_is_js_returns_correct_cache_control_cache_disabled(array $jsResponseSetupCacheDisabled) + { + $this->assertSame('max-age=1, public', $jsResponseSetupCacheDisabled['responseHeaders']['cache-control'][0]); + } + + /** + * @test + * + * @depends jsResponseSetupCacheDisabled + */ + public function check_pretend_response_is_js_returns_correct_content_type_cache_disabled(array $jsResponseSetupCacheDisabled) + { + $this->assertSame('application/javascript; charset=utf-8', $jsResponseSetupCacheDisabled['responseHeaders']['content-type'][0]); + } + + /** + * @test + * + * @depends cssResponseSetupCacheEnabled + */ + public function check_pretend_response_is_css_returns_correct_cache_control_caching_enabled(array $cssResponseSetupCacheEnabled) + { + $this->assertSame('max-age=3600, public', $cssResponseSetupCacheEnabled['responseHeaders']['cache-control'][0]); + } + + /** + * @test + * + * @depends cssResponseSetupCacheEnabled + */ + public function check_pretend_response_is_css_returns_correct_content_type_caching_enabled(array $cssResponseSetupCacheEnabled) + { + $this->assertSame('text/css; charset=utf-8', $cssResponseSetupCacheEnabled['responseHeaders']['content-type'][0]); + } + + /** + * @test + * + * @depends cssResponseSetupCacheDisabled + */ + public function check_pretend_response_is_css_returns_correct_cache_control_caching_disabled(array $cssResponseSetupCacheDisabled) + { + $this->assertSame('max-age=1, public', $cssResponseSetupCacheDisabled['responseHeaders']['cache-control'][0]); + } + + /** + * @test + * + * @depends cssResponseSetupCacheDisabled + */ + public function check_pretend_response_is_css_returns_correct_content_type_caching_disabled(array $cssResponseSetupCacheDisabled) + { + $this->assertSame('text/css; charset=utf-8', $cssResponseSetupCacheDisabled['responseHeaders']['content-type'][0]); + } + + /** + * @test + * + * @depends thirdPartyCssResponseSetupCacheEnabled + */ + public function tp_check_pretend_response_is_css_returns_correct_cache_control_caching_enabled(array $thirdPartyCssResponseSetupCacheEnabled) + { + $this->assertSame('max-age=3600, public', $thirdPartyCssResponseSetupCacheEnabled['responseHeaders']['cache-control'][0]); + } + + /** + * @test + * + * @depends thirdPartyCssResponseSetupCacheEnabled + */ + public function tp_check_pretend_response_is_css_returns_correct_content_type_caching_enabled(array $thirdPartyCssResponseSetupCacheEnabled) + { + $this->assertSame('text/css; charset=utf-8', $thirdPartyCssResponseSetupCacheEnabled['responseHeaders']['content-type'][0]); + } + + /** + * @test + * + * @depends thirdPartyCssResponseSetupCacheDisabled + */ + public function tp_check_pretend_response_is_css_returns_correct_cache_control_caching_disabled(array $thirdPartyCssResponseSetupCacheDisabled) + { + $this->assertSame('max-age=1, public', $thirdPartyCssResponseSetupCacheDisabled['responseHeaders']['cache-control'][0]); + } + + /** + * @test + * + * @depends thirdPartyCssResponseSetupCacheDisabled + */ + public function tp_check_pretend_response_is_css_returns_correct_content_type_caching_disabled(array $thirdPartyCssResponseSetupCacheDisabled) + { + $this->assertSame('text/css; charset=utf-8', $thirdPartyCssResponseSetupCacheDisabled['responseHeaders']['content-type'][0]); + } + + /** + * @test + * + * @depends thirdPartyJsResponseSetupCacheEnabled + */ + public function tp_check_pretend_response_is_js_returns_correct_cache_control_cache_enabled(array $thirdPartyJsResponseSetupCacheEnabled) + { + $this->assertSame('max-age=3600, public', $thirdPartyJsResponseSetupCacheEnabled['responseHeaders']['cache-control'][0]); + } + + /** + * @test + * + * @depends thirdPartyJsResponseSetupCacheEnabled + */ + public function tp_check_pretend_response_is_js_returns_correct_content_type_cache_enabled(array $thirdPartyJsResponseSetupCacheEnabled) + { + $this->assertSame('application/javascript; charset=utf-8', $thirdPartyJsResponseSetupCacheEnabled['responseHeaders']['content-type'][0]); + } + + /** + * @test + * + * @depends thirdPartyJsResponseSetupCacheDisabled + */ + public function tp_check_pretend_response_is_js_returns_correct_cache_control_cache_disabled(array $thirdPartyJsResponseSetupCacheDisabled) + { + $this->assertSame('max-age=1, public', $thirdPartyJsResponseSetupCacheDisabled['responseHeaders']['cache-control'][0]); + } + + /** + * @test + * + * @depends thirdPartyJsResponseSetupCacheDisabled + */ + public function tp_check_pretend_response_is_js_returns_correct_content_type_cache_disabled(array $thirdPartyJsResponseSetupCacheDisabled) + { + $this->assertSame('application/javascript; charset=utf-8', $thirdPartyJsResponseSetupCacheDisabled['responseHeaders']['content-type'][0]); + } }