Skip to content

Commit 4bcbafd

Browse files
committed
fix: tests
1 parent 499fb4e commit 4bcbafd

File tree

9 files changed

+62
-33
lines changed

9 files changed

+62
-33
lines changed

phpunit-example.xml.dist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<ini name="intl.default_locale" value="C.UTF-8"/>
1212
<ini name="memory_limit" value="2048M"/>
1313
<env name="DB_CONNECTION" value="mysql"/>
14+
<env name="DB_DATABASE" value="moonshine_tests"/>
15+
<env name="DB_USERNAME" value="root"/>
1416
<!--
1517
<env name="REDIS_HOST" value="127.0.0.1" />
1618
<env name="REDIS_PORT" value="6379" />
@@ -22,4 +24,4 @@
2224
<directory suffix=".php">src</directory>
2325
</include>
2426
</source>
25-
</phpunit>
27+
</phpunit>

resources/views/components/fields-group.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
@foreach($components as $fieldOrDecoration)
66
@if($fieldOrDecoration instanceof Field && $fieldOrDecoration->hasWrapper())
77
<x-moonshine::field-container :field="$fieldOrDecoration">
8+
{!! $fieldOrDecoration->getBeforeRender() !!}
89
{{ $fieldOrDecoration->render() }}
10+
{!! $fieldOrDecoration->getAfterRender() !!}
911
</x-moonshine::field-container>
1012
@else
1113
{{ $fieldOrDecoration->render() }}

src/AssetManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function js(): string
9595
return collect($this->assets)
9696
->push($this->getMainJs())
9797
->filter(
98-
fn ($asset): int|bool => preg_match('/\.js/', (string) $asset)
98+
fn ($asset): int|bool => str_contains((string) $asset, '.js')
9999
)
100100
->map(
101101
fn ($asset): string => "<script defer src='" . asset(
@@ -109,7 +109,7 @@ public function css(): string
109109
return collect($this->assets)
110110
->push($this->getMainCss())
111111
->filter(
112-
fn ($asset): int|bool => preg_match('/\.css/', (string) $asset)
112+
fn ($asset): int|bool => str_contains((string) $asset, '.css')
113113
)
114114
->map(
115115
fn ($asset): string => "<link href='" . asset(

src/Fields/FormElement.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ abstract class FormElement implements MoonShineRenderable, HasAssets
4444

4545
protected array $wrapperAttributes = [];
4646

47+
protected ?Closure $beforeRender = null;
48+
49+
protected ?Closure $afterRender = null;
50+
4751
protected function afterMake(): void
4852
{
4953
if ($this->getAssets()) {
@@ -151,6 +155,34 @@ public function getFormName()
151155
return $this->formName;
152156
}
153157

158+
public function beforeRender(Closure $closure): self
159+
{
160+
$this->beforeRender = $closure;
161+
162+
return $this;
163+
}
164+
165+
public function afterRender(Closure $closure): self
166+
{
167+
$this->afterRender = $closure;
168+
169+
return $this;
170+
}
171+
172+
public function getBeforeRender(): View|string
173+
{
174+
return is_null($this->beforeRender)
175+
? ''
176+
: call_user_func($this->beforeRender, $this);
177+
}
178+
179+
public function getAfterRender(): View|string
180+
{
181+
return is_null($this->afterRender)
182+
? ''
183+
: call_user_func($this->afterRender, $this);
184+
}
185+
154186
public function render(): View|Closure|string
155187
{
156188
if ($this instanceof Field && $this->getView() === '') {

src/Providers/MoonShineServiceProvider.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,6 @@ class MoonShineServiceProvider extends ServiceProvider
5555
],
5656
];
5757

58-
/**
59-
* Register services.
60-
*/
61-
public function register(): void
62-
{
63-
$this->loadAuthConfig();
64-
65-
$this->registerRouteMiddleware();
66-
}
67-
6858
/**
6959
* Setup auth configuration.
7060
*/
@@ -109,11 +99,6 @@ public function boot(): void
10999
),
110100
]);
111101

112-
$this->mergeConfigFrom(
113-
MoonShine::path('/config/moonshine.php'),
114-
'moonshine'
115-
);
116-
117102
$this->loadTranslationsFrom(MoonShine::path('/lang'), 'moonshine');
118103
$this->loadRoutesFrom(MoonShine::path('/routes/moonshine.php'));
119104
$this->loadViewsFrom(MoonShine::path('/resources/views'), 'moonshine');
@@ -132,6 +117,10 @@ public function boot(): void
132117
$this->commands($this->commands);
133118
}
134119

120+
$this->loadAuthConfig();
121+
122+
$this->registerRouteMiddleware();
123+
135124
Blade::withoutDoubleEncoding();
136125
Blade::componentNamespace('MoonShine\Components', 'moonshine');
137126

src/Traits/Fields/Applies.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public function afterApply(mixed $data): mixed
103103
public function afterDestroy(mixed $data): mixed
104104
{
105105
return is_closure($this->onAfterDestroy)
106-
? call_user_func($this->onAfterDestroy, $data)
107-
: $this->resolveAfterDestroy($data, $this->requestValue());
106+
? call_user_func($this->onAfterDestroy, $data, $this->requestValue())
107+
: $this->resolveAfterDestroy($data);
108108
}
109109

110110
public function onApply(Closure $onApply): static

src/Traits/WithAssets.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,11 @@ public function getAssets(): array
1515
{
1616
return $this->assets;
1717
}
18+
19+
public function addAssets(array $assets): self
20+
{
21+
moonshineAssets()->add($assets);
22+
23+
return $this;
24+
}
1825
}

tests/Fixtures/TestServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class TestServiceProvider extends ServiceProvider
1010
{
11-
public function boot()
11+
public function boot(): void
1212
{
1313
$this->loadMigrationsFrom(realpath('./tests/Fixtures/Migrations'));
1414
}

tests/TestCase.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
class TestCase extends Orchestra
3131
{
3232
use InteractsWithViews;
33-
3433
use RefreshDatabase;
3534

3635
protected Authenticatable|MoonshineUser $adminUser;
@@ -39,22 +38,20 @@ class TestCase extends Orchestra
3938

4039
protected function setUp(): void
4140
{
42-
parent::setUp();
41+
$this->afterApplicationCreated(function () {
42+
$this->performApplication()
43+
->resolveFactories()
44+
->resolveSuperUser()
45+
->resolveMoonShineUserResource()
46+
->registerTestResource();
47+
});
4348

44-
$this->performApplication()
45-
->resolveFactories()
46-
->resolveSuperUser()
47-
->resolveMoonShineUserResource()
48-
->registerTestResource();
49+
parent::setUp();
4950
}
5051

51-
protected function getEnvironmentSetUp($app): void
52+
protected function defineEnvironment($app): void
5253
{
5354
$app['config']->set('app.debug', 'true');
54-
55-
$app['config']->set('database.default', 'mysql');
56-
$app['config']->set('database.connections.mysql.database', 'moonshine_tests');
57-
$app['config']->set('database.connections.mysql.username', 'root');
5855
}
5956

6057
protected function performApplication(): static

0 commit comments

Comments
 (0)