[增添]添加了datasource的setting数据库以及默认值
This commit is contained in:
595
vendor/filament/forms/src/Testing/TestsComponentActions.php
vendored
Normal file
595
vendor/filament/forms/src/Testing/TestsComponentActions.php
vendored
Normal file
@@ -0,0 +1,595 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Forms\Testing;
|
||||
|
||||
use Closure;
|
||||
use Filament\Forms\ComponentContainer;
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Testing\Assert;
|
||||
use Livewire\Features\SupportTesting\Testable;
|
||||
|
||||
use function Livewire\store;
|
||||
|
||||
/**
|
||||
* @method HasForms instance()
|
||||
*
|
||||
* @mixin Testable
|
||||
*/
|
||||
class TestsComponentActions
|
||||
{
|
||||
public function mountFormComponentAction(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $name] = $this->parseNestedFormComponentActionComponentAndName($component, $name, $formName);
|
||||
|
||||
foreach ($name as $actionNestingIndex => $actionName) {
|
||||
$this->call(
|
||||
'mountFormComponentAction',
|
||||
$component[$actionNestingIndex],
|
||||
$actionName,
|
||||
$arguments,
|
||||
);
|
||||
}
|
||||
|
||||
if (store($this->instance())->has('redirect')) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (! count($this->instance()->mountedFormComponentActions)) {
|
||||
$this->assertNotDispatched('open-modal');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->assertSet('mountedFormComponentActionsComponents', $component);
|
||||
$this->assertSet('mountedFormComponentActions', $name);
|
||||
|
||||
$this->assertDispatched('open-modal', id: "{$this->instance()->getId()}-form-component-action");
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function unmountFormComponentAction(): Closure
|
||||
{
|
||||
return function (): static {
|
||||
$this->call('unmountFormComponentAction');
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function setFormComponentActionData(): Closure
|
||||
{
|
||||
return function (array $data): static {
|
||||
foreach (Arr::dot($data, prepend: 'mountedFormComponentActionsData.' . array_key_last($this->instance()->mountedFormComponentActionsData) . '.') as $key => $value) {
|
||||
$this->set($key, $value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionDataSet(): Closure
|
||||
{
|
||||
return function (array $data): static {
|
||||
foreach (Arr::dot($data, prepend: 'mountedFormComponentActionsData.' . array_key_last($this->instance()->mountedFormComponentActionsData) . '.') as $key => $value) {
|
||||
$this->assertSet($key, $value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function callFormComponentAction(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, array $data = [], array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionVisible($component, $name, $arguments, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->mountFormComponentAction($component, $name, $arguments, $formName);
|
||||
|
||||
if (! $this->instance()->getMountedFormComponentAction()) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->setFormComponentActionData($data);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->callMountedFormComponentAction($arguments);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function callMountedFormComponentAction(): Closure
|
||||
{
|
||||
return function (array $arguments = []): static {
|
||||
$action = $this->instance()->getMountedFormComponentAction();
|
||||
|
||||
if (! $action) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->call('callMountedFormComponentAction', $arguments);
|
||||
|
||||
if (store($this->instance())->has('redirect')) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (! count($this->instance()->mountedFormComponentActions)) {
|
||||
$this->assertDispatched('close-modal', id: "{$this->instance()->getId()}-form-component-action");
|
||||
}
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionExists(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertInstanceOf(
|
||||
Action::class,
|
||||
$action,
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}] is registered to [{$component->getKey()}] on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionDoesNotExist(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertNull(
|
||||
$action,
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}] is not registered to [{$component->getKey()}] on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionVisible(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertFalse(
|
||||
$action->isHidden(),
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], is visible on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionHidden(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertTrue(
|
||||
$action->isHidden(),
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], is hidden on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionEnabled(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertFalse(
|
||||
$action->isDisabled(),
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], is enabled on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionDisabled(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertFalse(
|
||||
$action->isEnabled(),
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], is disabled on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionHasIcon(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string $icon, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertTrue(
|
||||
$action->getIcon() === $icon,
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], has icon [{$icon}] on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionDoesNotHaveIcon(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string $icon, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertFalse(
|
||||
$action->getIcon() === $icon,
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], does not have icon [{$icon}] on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionHasLabel(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string $label, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertTrue(
|
||||
$action->getLabel() === $label,
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], has label [{$label}] on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionDoesNotHaveLabel(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string $label, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertFalse(
|
||||
$action->getLabel() === $label,
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], does not have label [{$label}] on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionHasColor(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string | array $color, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
$colorName = is_string($color) ? $color : 'custom';
|
||||
|
||||
Assert::assertTrue(
|
||||
$action->getColor() === $color,
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], has [{$colorName}] color on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionDoesNotHaveColor(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string | array $color, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
$colorName = is_string($color) ? $color : 'custom';
|
||||
|
||||
Assert::assertFalse(
|
||||
$action->getColor() === $color,
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], does not have [{$colorName}] color on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionHasUrl(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string $url, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertTrue(
|
||||
$action->getUrl() === $url,
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], has URL [{$url}] on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionDoesNotHaveUrl(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string $url, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertFalse(
|
||||
$action->getUrl() === $url,
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], does not have URL [{$url}] on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionShouldOpenUrlInNewTab(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertTrue(
|
||||
$action->shouldOpenUrlInNewTab(),
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], should open url in new tab on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionShouldNotOpenUrlInNewTab(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, array $arguments = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $action] = $this->getNestedFormComponentActionComponentAndName($component, $name, $formName, $arguments);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
$prettyName = implode(' > ', Arr::wrap($name));
|
||||
|
||||
Assert::assertFalse(
|
||||
$action->shouldOpenUrlInNewTab(),
|
||||
message: "Failed asserting that a form component action with name [{$prettyName}], registered to [{$component->getKey()}], should not open url in new tab on the [{$livewireClass}] component.",
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionMounted(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $name] = $this->parseNestedFormComponentActionComponentAndName($component, $name, $formName);
|
||||
|
||||
$this->assertSet('mountedFormComponentActions', $name);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionNotMounted(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormComponentActionExists($component, $name, $formName);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $name] = $this->parseNestedFormComponentActionComponentAndName($component, $name, $formName);
|
||||
|
||||
$this->assertNotSet('mountedFormComponentActions', $name);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentActionHalted(): Closure
|
||||
{
|
||||
return $this->assertFormComponentActionMounted();
|
||||
}
|
||||
|
||||
public function assertHasFormComponentActionErrors(): Closure
|
||||
{
|
||||
return function (array $keys = []): static {
|
||||
$this->assertHasErrors(
|
||||
collect($keys)
|
||||
->mapWithKeys(function ($value, $key): array {
|
||||
if (is_int($key)) {
|
||||
return [$key => 'mountedFormComponentActionsData.' . array_key_last($this->instance()->mountedFormComponentActionsData) . '.' . $value];
|
||||
}
|
||||
|
||||
return ['mountedFormComponentActionsData.' . array_key_last($this->instance()->mountedFormComponentActionsData) . '.' . $key => $value];
|
||||
})
|
||||
->all(),
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertHasNoFormComponentActionErrors(): Closure
|
||||
{
|
||||
return function (array $keys = []): static {
|
||||
$this->assertHasNoErrors(
|
||||
collect($keys)
|
||||
->mapWithKeys(function ($value, $key): array {
|
||||
if (is_int($key)) {
|
||||
return [$key => 'mountedFormComponentActionsData.' . array_key_last($this->instance()->mountedFormComponentActionsData) . '.' . $value];
|
||||
}
|
||||
|
||||
return ['mountedFormComponentActionsData.' . array_key_last($this->instance()->mountedFormComponentActionsData) . '.' . $key => $value];
|
||||
})
|
||||
->all(),
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function getNestedFormComponentActionComponentAndName(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string $formName = 'form', array $arguments = []): array {
|
||||
$isSingular = ! is_array($component);
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
[$component, $name] = $this->parseNestedFormComponentActionComponentAndName($component, $name, $formName);
|
||||
|
||||
foreach ($component as $componentNestingIndex => $componentKey) {
|
||||
foreach ($this->instance()->getCachedForms() as $form) {
|
||||
$formComponent = $form->getComponent($componentKey);
|
||||
|
||||
if (! $formComponent) {
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$action = ($formComponent ?? null)?->getAction($name[$componentNestingIndex]);
|
||||
$action?->arguments($isSingular ? $arguments : $arguments[$componentNestingIndex] ?? []);
|
||||
}
|
||||
|
||||
return [$formComponent ?? null, $action ?? null];
|
||||
};
|
||||
}
|
||||
|
||||
public function parseNestedFormComponentActionComponentAndName(): Closure
|
||||
{
|
||||
return function (string | array $component, string | array $name, string $formName = 'form'): array {
|
||||
$this->assertFormExists($formName);
|
||||
|
||||
$component = Arr::wrap($component);
|
||||
|
||||
$livewire = $this->instance();
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $livewire->{$formName};
|
||||
|
||||
$components = [];
|
||||
|
||||
foreach ($component as $componentIndex => $componentKey) {
|
||||
if ($componentIndex) {
|
||||
$components[] = 'mountedFormComponentActionData.' . ($componentIndex - 1) . ".{$componentKey}";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$components[] = "{$form->getStatePath()}.{$componentKey}";
|
||||
}
|
||||
|
||||
return [$components, $this->parseNestedActionName($name)];
|
||||
};
|
||||
}
|
||||
}
|
||||
364
vendor/filament/forms/src/Testing/TestsForms.php
vendored
Normal file
364
vendor/filament/forms/src/Testing/TestsForms.php
vendored
Normal file
@@ -0,0 +1,364 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Forms\Testing;
|
||||
|
||||
use Closure;
|
||||
use Filament\Forms\ComponentContainer;
|
||||
use Filament\Forms\Components\Component;
|
||||
use Filament\Forms\Components\Field;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Testing\Assert;
|
||||
use Livewire\Features\SupportTesting\Testable;
|
||||
|
||||
/**
|
||||
* @method HasForms instance()
|
||||
*
|
||||
* @mixin Testable
|
||||
*/
|
||||
class TestsForms
|
||||
{
|
||||
public function fillForm(): Closure
|
||||
{
|
||||
return function (array | Closure $state = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormExists($formName);
|
||||
|
||||
$livewire = $this->instance();
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $livewire->{$formName};
|
||||
|
||||
$formStatePath = $form->getStatePath();
|
||||
|
||||
if ($state instanceof Closure) {
|
||||
$state = $state($form->getRawState());
|
||||
}
|
||||
|
||||
if (is_array($state)) {
|
||||
$state = Arr::undot($state);
|
||||
|
||||
if (filled($formStatePath)) {
|
||||
$state = Arr::undot([$formStatePath => $state]);
|
||||
}
|
||||
|
||||
foreach (Arr::dot($state) as $key => $value) {
|
||||
if ($value instanceof UploadedFile ||
|
||||
(is_array($value) && isset($value[0]) && $value[0] instanceof UploadedFile)
|
||||
) {
|
||||
$this->set($key, $value);
|
||||
Arr::set($state, $key, $this->get($key));
|
||||
}
|
||||
}
|
||||
|
||||
$this->call('fillFormDataForTesting', $state);
|
||||
}
|
||||
|
||||
$this->refresh();
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormSet(): Closure
|
||||
{
|
||||
return function (array | Closure $state, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormExists($formName);
|
||||
|
||||
$livewire = $this->instance();
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $livewire->{$formName};
|
||||
|
||||
$formStatePath = $form->getStatePath();
|
||||
|
||||
if ($state instanceof Closure) {
|
||||
$state = $state($form->getRawState());
|
||||
}
|
||||
|
||||
if (is_array($state)) {
|
||||
foreach (Arr::dot($state, prepend: filled($formStatePath) ? "{$formStatePath}." : '') as $key => $value) {
|
||||
$this->assertSet($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertHasFormErrors(): Closure
|
||||
{
|
||||
return function (array $keys = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormExists($formName);
|
||||
|
||||
$livewire = $this->instance();
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $livewire->{$formName};
|
||||
|
||||
$formStatePath = $form->getStatePath();
|
||||
|
||||
$this->assertHasErrors(
|
||||
collect($keys)
|
||||
->mapWithKeys(function ($value, $key) use ($formStatePath): array {
|
||||
if (is_int($key)) {
|
||||
return [$key => (filled($formStatePath) ? "{$formStatePath}.{$value}" : $value)];
|
||||
}
|
||||
|
||||
return [(filled($formStatePath) ? "{$formStatePath}.{$key}" : $key) => $value];
|
||||
})
|
||||
->all(),
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertHasNoFormErrors(): Closure
|
||||
{
|
||||
return function (array $keys = [], string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormExists($formName);
|
||||
|
||||
$livewire = $this->instance();
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $livewire->{$formName};
|
||||
|
||||
$formStatePath = $form->getStatePath();
|
||||
|
||||
$this->assertHasNoErrors(
|
||||
collect($keys)
|
||||
->mapWithKeys(function ($value, $key) use ($formStatePath): array {
|
||||
if (is_int($key)) {
|
||||
return [$key => (filled($formStatePath) ? "{$formStatePath}.{$value}" : $value)];
|
||||
}
|
||||
|
||||
return [(filled($formStatePath) ? "{$formStatePath}.{$key}" : $key) => $value];
|
||||
})
|
||||
->all(),
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormExists(): Closure
|
||||
{
|
||||
return function (string $name = 'form'): static {
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $this->instance()->{$name};
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
|
||||
Assert::assertInstanceOf(
|
||||
ComponentContainer::class,
|
||||
$form,
|
||||
"Failed asserting that a form with the name [{$name}] exists on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormComponentExists(): Closure
|
||||
{
|
||||
return function (string $componentKey, string | Closure $formName = 'form', ?Closure $checkComponentUsing = null): static {
|
||||
if ($formName instanceof Closure) {
|
||||
$checkComponentUsing = $formName;
|
||||
$formName = 'form';
|
||||
}
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormExists($formName);
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $this->instance()->{$formName};
|
||||
|
||||
/** @var ?Component $component */
|
||||
$component = $form->getFlatComponentsByKey(withHidden: true)[$componentKey] ?? null;
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
|
||||
Assert::assertInstanceOf(
|
||||
Component::class,
|
||||
$component,
|
||||
"Failed asserting that a component with the key [{$componentKey}] exists on the form with the name [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
if ($checkComponentUsing) {
|
||||
Assert::assertTrue(
|
||||
$checkComponentUsing($component),
|
||||
"Failed asserting that a component with the key [{$componentKey}] and provided configuration exists on the form with the name [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormFieldExists(): Closure
|
||||
{
|
||||
return function (string $fieldName, string | Closure $formName = 'form', ?Closure $checkFieldUsing = null): static {
|
||||
if ($formName instanceof Closure) {
|
||||
$checkFieldUsing = $formName;
|
||||
$formName = 'form';
|
||||
}
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormExists($formName);
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $this->instance()->{$formName};
|
||||
|
||||
/** @var ?Field $field */
|
||||
$field = $form->getFlatFields(withHidden: true)[$fieldName] ?? null;
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
|
||||
Assert::assertInstanceOf(
|
||||
Field::class,
|
||||
$field,
|
||||
"Failed asserting that a field with the name [{$fieldName}] exists on the form with the name [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
if ($checkFieldUsing) {
|
||||
Assert::assertTrue(
|
||||
$checkFieldUsing($field),
|
||||
"Failed asserting that a field with the name [{$fieldName}] and provided configuration exists on the form with the name [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormFieldIsDisabled(): Closure
|
||||
{
|
||||
return function (string $fieldName, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormFieldExists($fieldName, $formName);
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $this->instance()->{$formName};
|
||||
|
||||
/** @var Field $field */
|
||||
$field = $form->getFlatFields(withHidden: true)[$fieldName];
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormFieldExists($fieldName, $formName, function (Field $field) use ($fieldName, $formName, $livewireClass): bool {
|
||||
Assert::assertTrue(
|
||||
$field->isDisabled(),
|
||||
"Failed asserting that a field with the name [{$fieldName}] is disabled on the form named [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormFieldIsEnabled(): Closure
|
||||
{
|
||||
return function (string $fieldName, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormFieldExists($fieldName, $formName);
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $this->instance()->{$formName};
|
||||
|
||||
/** @var Field $field */
|
||||
$field = $form->getFlatFields(withHidden: true)[$fieldName];
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormFieldExists($fieldName, $formName, function (Field $field) use ($fieldName, $formName, $livewireClass): bool {
|
||||
Assert::assertFalse(
|
||||
$field->isDisabled(),
|
||||
"Failed asserting that a field with the name [{$fieldName}] is enabled on the form named [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormFieldIsReadOnly(): Closure
|
||||
{
|
||||
return function (string $fieldName, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormFieldExists($fieldName, $formName);
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $this->instance()->{$formName};
|
||||
|
||||
/** @var TextInput $field */
|
||||
$field = $form->getFlatFields(withHidden: true)[$fieldName];
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
|
||||
Assert::assertTrue(
|
||||
$field->isReadOnly(),
|
||||
"Failed asserting that a field with the name [{$fieldName}] is read-only on the form named [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormFieldIsHidden(): Closure
|
||||
{
|
||||
return function (string $fieldName, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormFieldExists($fieldName, $formName);
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $this->instance()->{$formName};
|
||||
|
||||
$fields = $form->getFlatFields(withHidden: false);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
|
||||
Assert::assertArrayNotHasKey(
|
||||
$fieldName,
|
||||
$fields,
|
||||
"Failed asserting that a field with the name [{$fieldName}] is hidden on the form named [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormFieldIsVisible(): Closure
|
||||
{
|
||||
return function (string $fieldName, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormFieldExists($fieldName, $formName);
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $this->instance()->{$formName};
|
||||
|
||||
$fields = $form->getFlatFields(withHidden: false);
|
||||
|
||||
$livewireClass = $this->instance()::class;
|
||||
|
||||
Assert::assertArrayHasKey(
|
||||
$fieldName,
|
||||
$fields,
|
||||
"Failed asserting that a field with the name [{$fieldName}] is visible on the form named [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user