Skip to content

Commit

Permalink
Merge pull request #1 from gwleuverink/fix/allow-usage-in-multiple-pa…
Browse files Browse the repository at this point in the history
…ckages

Fix/allow usage in multiple packages
  • Loading branch information
gwleuverink authored Sep 26, 2024
2 parents c3cbd48 + 6e7787c commit 485dca2
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 104 deletions.
23 changes: 5 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![codestyle](https://github.com/gwleuverink/inject-package-assets/actions/workflows/codestyle.yml/badge.svg)](https://github.com/gwleuverink/inject-package-assets/actions/workflows/codestyle.yml)
[![tests](https://github.com/gwleuverink/inject-package-assets/actions/workflows/tests.yml/badge.svg)](https://github.com/gwleuverink/inject-package-assets/actions/workflows/tests.yml)

Simplify your Laravel package development with automatic asset injection.
Simplify your Laravel package development with automatic asset injection.

This package allows you to seamlessly insert JavaScript and CSS into web responses without requiring manual inclusion by your package users 🚀

Expand Down Expand Up @@ -54,37 +54,24 @@ class InjectAssets implements AssetInjector
}
```

2. Bind the implementation in your package's Service Provider.
2. Register the implementation in your package's Service Provider.

```php
namespace YourPackage;

use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Leuverink\AssetInjector\Contracts\AssetInjector;
use Leuverink\AssetInjector\AssetManager;
use YourPackage\InjectAssets;

class ServiceProvider extends BaseServiceProvider
{
public function register()
public function boot()
{
$this->app->bind(
AssetInjector::class,
InjectAssets::class
);
AssetManager::register(new InjectAssets);
}
}
```

3. Usage with Orchestra testbench

If you like to test the integration within your own package you'll need to register AssetInjector's service provider by adding it to your `testbench.yaml`

```yaml
providers:
- YourPackage\ServiceProvider
- Leuverink\AssetInjector\ServiceProvider
```
## How It Works

- Assets are automatically included in full-page responses (not partial HTML responses).
Expand Down
7 changes: 0 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@
"orchestra/testbench": "^9",
"pestphp/pest": "^2.35"
},
"extra": {
"laravel": {
"providers": [
"Leuverink\\AssetInjector\\ServiceProvider"
]
}
},
"scripts": {
"lint": "vendor/bin/duster lint",
"fix": "vendor/bin/duster fix",
Expand Down
18 changes: 18 additions & 0 deletions src/AssetManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Leuverink\AssetInjector;

use Illuminate\Support\Facades\Event;
use Leuverink\AssetInjector\Contracts\AssetInjector;
use Illuminate\Foundation\Http\Events\RequestHandled;

class AssetManager
{
public static function register(AssetInjector $injector)
{
Event::listen(function (RequestHandled $event) use ($injector) {
$listener = new InjectAssets($injector);
$listener($event);
});
}
}
18 changes: 0 additions & 18 deletions src/ServiceProvider.php

This file was deleted.

2 changes: 1 addition & 1 deletion testbench.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
providers:
- Leuverink\AssetInjector\ServiceProvider
# - Leuverink\AssetInjector\ServiceProvider
41 changes: 36 additions & 5 deletions tests/Feature/InjectsAssetsTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

use Leuverink\AssetInjector\Contracts\AssetInjector;
use Tests\Stubs\Implement;
use Tests\Stubs\DisabledImplement;
use Leuverink\AssetInjector\AssetManager;

it('injects assets into response', function () {
AssetManager::register(new Implement);

Route::get('test-inject-in-response', fn () => '<html><head></head></html>');

$this->get('test-inject-in-response')
Expand All @@ -11,6 +15,8 @@
});

it('injects assets into head tag', function () {
AssetManager::register(new Implement);

Route::get('test-inject-in-response', fn () => '<html><head></head></html>');

$expected = <<< 'HTML'
Expand All @@ -27,6 +33,8 @@
});

it('injects assets into html body when no head tag is present', function () {
AssetManager::register(new Implement);

Route::get('test-inject-in-response', fn () => '<html></html>');

$expected = <<< 'HTML'
Expand All @@ -43,6 +51,8 @@
});

it('injects assets into the end of the html body when no head tag is present', function () {
AssetManager::register(new Implement);

Route::get('test-inject-in-response', fn () => '<html><p>Hello World</p></html>');

$expected = <<< 'HTML'
Expand All @@ -59,6 +69,8 @@
});

it('doesnt inject assets into responses without a closing html tag', function () {
AssetManager::register(new Implement);

Route::get('test-inject-in-response', fn () => 'OK');

$this->get('test-inject-in-response')
Expand All @@ -67,13 +79,32 @@
});

it('doesnt inject assets when implementation returns false from enabled method', function () {
$this->partialMock(AssetInjector::class)
->shouldReceive('enabled')->once()
->andReturn(false);
AssetManager::register(new Implement);
AssetManager::register(new DisabledImplement);

Route::get('test-inject-in-response', fn () => '<html><head></head></html>');

$this->get('test-inject-in-response')
->assertOk()
->assertDontSee('<!--[TEST_PACKAGE]-->', false);
->assertSee('<!--[TEST_PACKAGE]-->', false)
->assertDontSee('<!--[DISABLED_TEST_PACKAGE]-->', false);
});

it('can inject more than one implement', function () {
AssetManager::register(new Implement);
AssetManager::register(new Implement);

Route::get('test-inject-in-response', fn () => '<html><head></head></html>');

$this->get('test-inject-in-response')
->assertOk()
->assertSee(<<<'HTML'
<!--[TEST_PACKAGE]-->
TEST_PACKAGE_ASSETS_INJECTED
<!--[ENDTEST_PACKAGE]-->
<!--[TEST_PACKAGE]-->
TEST_PACKAGE_ASSETS_INJECTED
<!--[ENDTEST_PACKAGE]-->
HTML, false);
});
45 changes: 2 additions & 43 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,4 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

// expect()->extend('toBeOne', function () {
// return $this->toBe(1);
// });

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

// function something()
// {
// // ..
// }
uses(Tests\TestCase::class)
->in('Feature');
23 changes: 23 additions & 0 deletions tests/Stubs/DisabledImplement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Tests\Stubs;

use Leuverink\AssetInjector\Contracts\AssetInjector;

class DisabledImplement implements AssetInjector
{
public function identifier(): string
{
return 'DISABLED_TEST_PACKAGE';
}

public function enabled(): bool
{
return false;
}

public function inject(): string
{
return 'DISABLED_TEST_PACKAGE_ASSETS_INJECTED';
}
}
2 changes: 1 addition & 1 deletion tests/Implement.php → tests/Stubs/Implement.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests;
namespace Tests\Stubs;

use Leuverink\AssetInjector\Contracts\AssetInjector;

Expand Down
11 changes: 0 additions & 11 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,8 @@

use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Leuverink\AssetInjector\Contracts\AssetInjector;

abstract class TestCase extends BaseTestCase
{
use WithWorkbench;

protected function setUp(): void
{
parent::setUp();

$this->app->bind(
AssetInjector::class,
Implement::class
);
}
}

0 comments on commit 485dca2

Please sign in to comment.