Skip to content

Commit

Permalink
Improve console tester
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed May 14, 2024
1 parent bb31f81 commit e890f6b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/Testing/ConsoleTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
namespace Tempest\Console\Testing;

use Closure;
use Exception;
use Fiber;
use PHPUnit\Framework\Assert;
use ReflectionMethod;
use Tempest\AppConfig;
use Tempest\Console\Components\InteractiveComponentRenderer;
use Tempest\Console\Console;
use Tempest\Console\ConsoleApplication;
use Tempest\Console\ConsoleArgumentBag;
use Tempest\Console\ConsoleCommand;
use Tempest\Console\Exceptions\ConsoleExceptionHandler;
use Tempest\Console\GenericConsole;
use Tempest\Console\Input\InputBuffer;
Expand All @@ -21,22 +24,20 @@
use Tempest\Console\Output\OutputBuffer;
use Tempest\Container\Container;
use Tempest\Highlight\Highlighter;
use Tempest\Support\Reflection\Attributes;

final class ConsoleTester
{
private ?MemoryOutputBuffer $output = null;
private ?MemoryInputBuffer $input = null;
private ?InteractiveComponentRenderer $componentRenderer = null;

public function __construct(private Container $container)
{
public function __construct(
private readonly Container $container,
) {
}

/**
* @param string|Closure $command
* @return $this
*/
public function call(string|Closure $command): self
public function call(string|Closure|array $command): self
{
$clone = clone $this;

Expand Down Expand Up @@ -66,6 +67,22 @@ public function call(string|Closure $command): self
$command($console);
});
} else {
if (is_string($command) && class_exists($command)) {
$command = [$command, '__invoke'];
}

if (is_array($command) || class_exists($command)) {
$attribute = Attributes::find(ConsoleCommand::class)
->in(new ReflectionMethod(...$command))
->first();

if (! $attribute) {
throw new Exception("Could not resolve console command from {$command[0]}::{$command[1]}");
}

$command = $attribute->getName();
}

$fiber = new Fiber(function () use ($command, $clone) {
$clone->container->singleton(ConsoleArgumentBag::class, new ConsoleArgumentBag(['tempest', ...explode(' ', $command)]));

Expand Down Expand Up @@ -98,7 +115,7 @@ public function input(int|string|Key $input): self

public function submit(int|string $input = ''): self
{
$input = (string) $input;
$input = (string)$input;

$this->input($input . Key::ENTER->value);

Expand Down
47 changes: 47 additions & 0 deletions tests/Testing/ConsoleTesterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Console\Testing;

use Tempest\Console\Console;
use Tests\Tempest\Console\Fixtures\ComplexCommand;
use Tests\Tempest\Console\Fixtures\InteractiveCommand;
use Tests\Tempest\Console\TestCase;

/**
* @internal
* @small
*/
class ConsoleTesterTest extends TestCase
{
public function test_call_with_invokable(): void
{
$this->console
->call(ComplexCommand::class)
->assertContains('complex <a> <b> <c>');
}

public function test_call_with_closure(): void
{
$this->console
->call(function (Console $console) {
$console->writeln('hi');
})
->assertContains('hi');
}

public function test_call_with_callable(): void
{
$this->console
->call([InteractiveCommand::class, 'validation'])
->assertContains('a');
}

public function test_call_with_command(): void
{
$this->console
->call('interactive:validation')
->assertContains('a');
}
}

0 comments on commit e890f6b

Please sign in to comment.