71 lines
2.0 KiB
PHP
71 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace Lean\Console\Form\Testing;
|
|
|
|
use Picea\Ui\Method\{ FormInterface, FormContextInterface };
|
|
|
|
use Lean\Console\{Attribute\TestParameter, Lib, Entity};
|
|
|
|
use Ulmus\{ EntityCollection };
|
|
use Psr\Container\ContainerInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
class Test implements FormInterface
|
|
{
|
|
protected Lib\Testing\TestPreprocessorInterface $waitFor;
|
|
|
|
protected Lib\Testing\TestFetcherItem $definition;
|
|
|
|
public function __construct(
|
|
protected ContainerInterface $container,
|
|
protected Lib\Testing\TestFetcher $testFetcher,
|
|
) {}
|
|
|
|
public function initialize(FormContextInterface $context) : void
|
|
{
|
|
if ($context->class ?? false) {
|
|
$this->definition = $this->testFetcher->compile()[$context->class];
|
|
}
|
|
}
|
|
|
|
public function validate(FormContextInterface $context) : bool
|
|
{
|
|
if (isset($this->definition->attribute) && $this->definition->attribute->waitFor) {
|
|
$this->waitFor = new $this->definition->attribute->waitFor();
|
|
}
|
|
|
|
return $context->valid();
|
|
}
|
|
|
|
public function execute(FormContextInterface $context) : mixed
|
|
{
|
|
if (isset($this->waitFor)) {
|
|
$this->waitFor->run();
|
|
}
|
|
|
|
$controller = $this->container->get($context->class);
|
|
|
|
$test = $this->definition->methods->filtersOne(fn($e) => $e->attribute->name === $context->name);
|
|
|
|
$request = $context->getRequest()
|
|
->withMethod($context->method)
|
|
->withAttribute('arguments', $test->attribute->arguments)
|
|
->withQueryParams($test->attribute->withQueryParams)
|
|
->withParsedBody($test->attribute->withParsedBody);
|
|
|
|
$arguments = []; # @todo
|
|
|
|
$result = $controller->{$test->name}($request, $arguments);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function attributeCollection(array $collection) : EntityCollection
|
|
{
|
|
$collection = new EntityCollection(...$collection);
|
|
$collection->entityClass = static::class;
|
|
|
|
return $collection;
|
|
}
|
|
}
|