Skip to content

Commit 485dca2

Browse files
authored
Merge pull request #1 from gwleuverink/fix/allow-usage-in-multiple-packages
Fix/allow usage in multiple packages
2 parents c3cbd48 + 6e7787c commit 485dca2

10 files changed

+86
-104
lines changed

README.md

+5-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![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)
44
[![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)
55

6-
Simplify your Laravel package development with automatic asset injection.
6+
Simplify your Laravel package development with automatic asset injection.
77

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

@@ -54,37 +54,24 @@ class InjectAssets implements AssetInjector
5454
}
5555
```
5656

57-
2. Bind the implementation in your package's Service Provider.
57+
2. Register the implementation in your package's Service Provider.
5858

5959
```php
6060
namespace YourPackage;
6161

6262
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
63-
use Leuverink\AssetInjector\Contracts\AssetInjector;
63+
use Leuverink\AssetInjector\AssetManager;
6464
use YourPackage\InjectAssets;
6565

6666
class ServiceProvider extends BaseServiceProvider
6767
{
68-
public function register()
68+
public function boot()
6969
{
70-
$this->app->bind(
71-
AssetInjector::class,
72-
InjectAssets::class
73-
);
70+
AssetManager::register(new InjectAssets);
7471
}
7572
}
7673
```
7774

78-
3. Usage with Orchestra testbench
79-
80-
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`
81-
82-
```yaml
83-
providers:
84-
- YourPackage\ServiceProvider
85-
- Leuverink\AssetInjector\ServiceProvider
86-
```
87-
8875
## How It Works
8976

9077
- Assets are automatically included in full-page responses (not partial HTML responses).

composer.json

-7
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@
3131
"orchestra/testbench": "^9",
3232
"pestphp/pest": "^2.35"
3333
},
34-
"extra": {
35-
"laravel": {
36-
"providers": [
37-
"Leuverink\\AssetInjector\\ServiceProvider"
38-
]
39-
}
40-
},
4134
"scripts": {
4235
"lint": "vendor/bin/duster lint",
4336
"fix": "vendor/bin/duster fix",

src/AssetManager.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Leuverink\AssetInjector;
4+
5+
use Illuminate\Support\Facades\Event;
6+
use Leuverink\AssetInjector\Contracts\AssetInjector;
7+
use Illuminate\Foundation\Http\Events\RequestHandled;
8+
9+
class AssetManager
10+
{
11+
public static function register(AssetInjector $injector)
12+
{
13+
Event::listen(function (RequestHandled $event) use ($injector) {
14+
$listener = new InjectAssets($injector);
15+
$listener($event);
16+
});
17+
}
18+
}

src/ServiceProvider.php

-18
This file was deleted.

testbench.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
providers:
2-
- Leuverink\AssetInjector\ServiceProvider
2+
# - Leuverink\AssetInjector\ServiceProvider

tests/Feature/InjectsAssetsTest.php

+36-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
<?php
22

3-
use Leuverink\AssetInjector\Contracts\AssetInjector;
3+
use Tests\Stubs\Implement;
4+
use Tests\Stubs\DisabledImplement;
5+
use Leuverink\AssetInjector\AssetManager;
46

57
it('injects assets into response', function () {
8+
AssetManager::register(new Implement);
9+
610
Route::get('test-inject-in-response', fn () => '<html><head></head></html>');
711

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

1317
it('injects assets into head tag', function () {
18+
AssetManager::register(new Implement);
19+
1420
Route::get('test-inject-in-response', fn () => '<html><head></head></html>');
1521

1622
$expected = <<< 'HTML'
@@ -27,6 +33,8 @@
2733
});
2834

2935
it('injects assets into html body when no head tag is present', function () {
36+
AssetManager::register(new Implement);
37+
3038
Route::get('test-inject-in-response', fn () => '<html></html>');
3139

3240
$expected = <<< 'HTML'
@@ -43,6 +51,8 @@
4351
});
4452

4553
it('injects assets into the end of the html body when no head tag is present', function () {
54+
AssetManager::register(new Implement);
55+
4656
Route::get('test-inject-in-response', fn () => '<html><p>Hello World</p></html>');
4757

4858
$expected = <<< 'HTML'
@@ -59,6 +69,8 @@
5969
});
6070

6171
it('doesnt inject assets into responses without a closing html tag', function () {
72+
AssetManager::register(new Implement);
73+
6274
Route::get('test-inject-in-response', fn () => 'OK');
6375

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

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

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

7687
$this->get('test-inject-in-response')
7788
->assertOk()
78-
->assertDontSee('<!--[TEST_PACKAGE]-->', false);
89+
->assertSee('<!--[TEST_PACKAGE]-->', false)
90+
->assertDontSee('<!--[DISABLED_TEST_PACKAGE]-->', false);
91+
});
92+
93+
it('can inject more than one implement', function () {
94+
AssetManager::register(new Implement);
95+
AssetManager::register(new Implement);
96+
97+
Route::get('test-inject-in-response', fn () => '<html><head></head></html>');
98+
99+
$this->get('test-inject-in-response')
100+
->assertOk()
101+
->assertSee(<<<'HTML'
102+
<!--[TEST_PACKAGE]-->
103+
TEST_PACKAGE_ASSETS_INJECTED
104+
<!--[ENDTEST_PACKAGE]-->
105+
106+
<!--[TEST_PACKAGE]-->
107+
TEST_PACKAGE_ASSETS_INJECTED
108+
<!--[ENDTEST_PACKAGE]-->
109+
HTML, false);
79110
});

tests/Pest.php

+2-43
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,4 @@
11
<?php
22

3-
/*
4-
|--------------------------------------------------------------------------
5-
| Test Case
6-
|--------------------------------------------------------------------------
7-
|
8-
| The closure you provide to your test functions is always bound to a specific PHPUnit test
9-
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
10-
| need to change it using the "uses()" function to bind a different classes or traits.
11-
|
12-
*/
13-
14-
uses(Tests\TestCase::class)->in('Feature');
15-
16-
/*
17-
|--------------------------------------------------------------------------
18-
| Expectations
19-
|--------------------------------------------------------------------------
20-
|
21-
| When you're writing tests, you often need to check that values meet certain conditions. The
22-
| "expect()" function gives you access to a set of "expectations" methods that you can use
23-
| to assert different things. Of course, you may extend the Expectation API at any time.
24-
|
25-
*/
26-
27-
// expect()->extend('toBeOne', function () {
28-
// return $this->toBe(1);
29-
// });
30-
31-
/*
32-
|--------------------------------------------------------------------------
33-
| Functions
34-
|--------------------------------------------------------------------------
35-
|
36-
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
37-
| project that you don't want to repeat in every file. Here you can also expose helpers as
38-
| global functions to help you to reduce the number of lines of code in your test files.
39-
|
40-
*/
41-
42-
// function something()
43-
// {
44-
// // ..
45-
// }
3+
uses(Tests\TestCase::class)
4+
->in('Feature');

tests/Stubs/DisabledImplement.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Tests\Stubs;
4+
5+
use Leuverink\AssetInjector\Contracts\AssetInjector;
6+
7+
class DisabledImplement implements AssetInjector
8+
{
9+
public function identifier(): string
10+
{
11+
return 'DISABLED_TEST_PACKAGE';
12+
}
13+
14+
public function enabled(): bool
15+
{
16+
return false;
17+
}
18+
19+
public function inject(): string
20+
{
21+
return 'DISABLED_TEST_PACKAGE_ASSETS_INJECTED';
22+
}
23+
}

tests/Implement.php tests/Stubs/Implement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests;
3+
namespace Tests\Stubs;
44

55
use Leuverink\AssetInjector\Contracts\AssetInjector;
66

tests/TestCase.php

-11
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,8 @@
44

55
use Orchestra\Testbench\Concerns\WithWorkbench;
66
use Orchestra\Testbench\TestCase as BaseTestCase;
7-
use Leuverink\AssetInjector\Contracts\AssetInjector;
87

98
abstract class TestCase extends BaseTestCase
109
{
1110
use WithWorkbench;
12-
13-
protected function setUp(): void
14-
{
15-
parent::setUp();
16-
17-
$this->app->bind(
18-
AssetInjector::class,
19-
Implement::class
20-
);
21-
}
2211
}

0 commit comments

Comments
 (0)