Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4 - Add console testing tools #31

Merged
merged 14 commits into from
Jul 1, 2024
13 changes: 13 additions & 0 deletions src/Features/Console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Features;

use Behat\Behat\Context\Context;
use Blumilk\BLT\Features\Traits\Console as ConsoleTrait;

class Console implements Context
{
use ConsoleTrait;
}
2 changes: 2 additions & 0 deletions src/Features/Toolbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Behat\Behat\Context\Context;
use Blumilk\BLT\Features\Traits\Application;
use Blumilk\BLT\Features\Traits\Authentication;
use Blumilk\BLT\Features\Traits\Console;
use Blumilk\BLT\Features\Traits\Database;
use Blumilk\BLT\Features\Traits\Dispatcher;
use Blumilk\BLT\Features\Traits\Eloquent;
Expand All @@ -21,6 +22,7 @@ class Toolbox implements Context
{
use Application;
use Authentication;
use Console;
use Database;
use Dispatcher;
use Eloquent;
Expand Down
79 changes: 79 additions & 0 deletions src/Features/Traits/Console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Features\Traits;

use Behat\Gherkin\Node\TableNode;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Facades\Artisan;
use InvalidArgumentException;
use PHPUnit\Framework\Assert;

trait Console
{
use Application;

protected string $consoleOutput = "";

/**
* @Given I run artisan command :command
* @Given I run artisan command :command in console
krzysztofrewak marked this conversation as resolved.
Show resolved Hide resolved
* @throws BindingResolutionException
*/
public function runArtisanCommand(string $command): void
{
$this->consoleOutput = "";
$this->getContainer()->make(Kernel::class)->call($command);
$this->consoleOutput = $this->getContainer()->make(Kernel::class)->output();
krzysztofrewak marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @Given I run artisan command :command with
krzysztofrewak marked this conversation as resolved.
Show resolved Hide resolved
* @throws BindingResolutionException
*/
public function runCommandWithOptionsAndArguments(string $command, TableNode $table): void
{
$options = "";
$arguments = "";

foreach ($table->getRows() as $row) {
if ($row[0] === "option") {
$options .= " --" . $row[1];
} elseif ($row[0] === "argument") {
$arguments .= " " . $row[1];
} else {
throw new InvalidArgumentException("Invalid key: $row[0]. Allowed keys are 'option' and 'argument'.");
}
}

$this->runArtisanCommand("$command $options $arguments");
}

/**
* @Then I see :output in console
krzysztofrewak marked this conversation as resolved.
Show resolved Hide resolved
* @Then I should see :output in console
* @Then Console output contains :output
*/
public function seeInConsole(string $output): void
{
Assert::assertStringContainsString($output, $this->consoleOutput);
}

/**
* @Then Console output is not empty
*/
public function consoleOutputIsNotEmpty(): void
{
Assert::assertNotEmpty($this->consoleOutput);
}

/**
* @Then Console output is empty
*/
public function consoleOutputIsEmpty(): void
{
Assert::assertEmpty($this->consoleOutput);
}
}
Loading