Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Sep 20, 2023
1 parent 499fb4e commit 4bcbafd
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 33 deletions.
4 changes: 3 additions & 1 deletion phpunit-example.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<ini name="intl.default_locale" value="C.UTF-8"/>
<ini name="memory_limit" value="2048M"/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="moonshine_tests"/>
<env name="DB_USERNAME" value="root"/>
<!--
<env name="REDIS_HOST" value="127.0.0.1" />
<env name="REDIS_PORT" value="6379" />
Expand All @@ -22,4 +24,4 @@
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
</phpunit>
2 changes: 2 additions & 0 deletions resources/views/components/fields-group.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
@foreach($components as $fieldOrDecoration)
@if($fieldOrDecoration instanceof Field && $fieldOrDecoration->hasWrapper())
<x-moonshine::field-container :field="$fieldOrDecoration">
{!! $fieldOrDecoration->getBeforeRender() !!}
{{ $fieldOrDecoration->render() }}
{!! $fieldOrDecoration->getAfterRender() !!}
</x-moonshine::field-container>
@else
{{ $fieldOrDecoration->render() }}
Expand Down
4 changes: 2 additions & 2 deletions src/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function js(): string
return collect($this->assets)
->push($this->getMainJs())
->filter(
fn ($asset): int|bool => preg_match('/\.js/', (string) $asset)
fn ($asset): int|bool => str_contains((string) $asset, '.js')
)
->map(
fn ($asset): string => "<script defer src='" . asset(
Expand All @@ -109,7 +109,7 @@ public function css(): string
return collect($this->assets)
->push($this->getMainCss())
->filter(
fn ($asset): int|bool => preg_match('/\.css/', (string) $asset)
fn ($asset): int|bool => str_contains((string) $asset, '.css')
)
->map(
fn ($asset): string => "<link href='" . asset(
Expand Down
32 changes: 32 additions & 0 deletions src/Fields/FormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ abstract class FormElement implements MoonShineRenderable, HasAssets

protected array $wrapperAttributes = [];

protected ?Closure $beforeRender = null;

protected ?Closure $afterRender = null;

protected function afterMake(): void
{
if ($this->getAssets()) {
Expand Down Expand Up @@ -151,6 +155,34 @@ public function getFormName()
return $this->formName;
}

public function beforeRender(Closure $closure): self
{
$this->beforeRender = $closure;

return $this;
}

public function afterRender(Closure $closure): self
{
$this->afterRender = $closure;

return $this;
}

public function getBeforeRender(): View|string
{
return is_null($this->beforeRender)
? ''
: call_user_func($this->beforeRender, $this);
}

public function getAfterRender(): View|string
{
return is_null($this->afterRender)
? ''
: call_user_func($this->afterRender, $this);
}

public function render(): View|Closure|string
{
if ($this instanceof Field && $this->getView() === '') {
Expand Down
19 changes: 4 additions & 15 deletions src/Providers/MoonShineServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ class MoonShineServiceProvider extends ServiceProvider
],
];

/**
* Register services.
*/
public function register(): void
{
$this->loadAuthConfig();

$this->registerRouteMiddleware();
}

/**
* Setup auth configuration.
*/
Expand Down Expand Up @@ -109,11 +99,6 @@ public function boot(): void
),
]);

$this->mergeConfigFrom(
MoonShine::path('/config/moonshine.php'),
'moonshine'
);

$this->loadTranslationsFrom(MoonShine::path('/lang'), 'moonshine');
$this->loadRoutesFrom(MoonShine::path('/routes/moonshine.php'));
$this->loadViewsFrom(MoonShine::path('/resources/views'), 'moonshine');
Expand All @@ -132,6 +117,10 @@ public function boot(): void
$this->commands($this->commands);
}

$this->loadAuthConfig();

$this->registerRouteMiddleware();

Blade::withoutDoubleEncoding();
Blade::componentNamespace('MoonShine\Components', 'moonshine');

Expand Down
4 changes: 2 additions & 2 deletions src/Traits/Fields/Applies.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public function afterApply(mixed $data): mixed
public function afterDestroy(mixed $data): mixed
{
return is_closure($this->onAfterDestroy)
? call_user_func($this->onAfterDestroy, $data)
: $this->resolveAfterDestroy($data, $this->requestValue());
? call_user_func($this->onAfterDestroy, $data, $this->requestValue())
: $this->resolveAfterDestroy($data);
}

public function onApply(Closure $onApply): static
Expand Down
7 changes: 7 additions & 0 deletions src/Traits/WithAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ public function getAssets(): array
{
return $this->assets;
}

public function addAssets(array $assets): self
{
moonshineAssets()->add($assets);

return $this;
}
}
2 changes: 1 addition & 1 deletion tests/Fixtures/TestServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class TestServiceProvider extends ServiceProvider
{
public function boot()
public function boot(): void
{
$this->loadMigrationsFrom(realpath('./tests/Fixtures/Migrations'));
}
Expand Down
21 changes: 9 additions & 12 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
class TestCase extends Orchestra
{
use InteractsWithViews;

use RefreshDatabase;

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

protected function setUp(): void
{
parent::setUp();
$this->afterApplicationCreated(function () {
$this->performApplication()
->resolveFactories()
->resolveSuperUser()
->resolveMoonShineUserResource()
->registerTestResource();
});

$this->performApplication()
->resolveFactories()
->resolveSuperUser()
->resolveMoonShineUserResource()
->registerTestResource();
parent::setUp();
}

protected function getEnvironmentSetUp($app): void
protected function defineEnvironment($app): void
{
$app['config']->set('app.debug', 'true');

$app['config']->set('database.default', 'mysql');
$app['config']->set('database.connections.mysql.database', 'moonshine_tests');
$app['config']->set('database.connections.mysql.username', 'root');
}

protected function performApplication(): static
Expand Down

0 comments on commit 4bcbafd

Please sign in to comment.