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
87 changes: 87 additions & 0 deletions src/Features/Traits/Console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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 InvalidArgumentException;
use PHPUnit\Framework\Assert;

trait Console
{
use Application;

protected string $consoleOutput = "";
protected int $consoleCode;

/**
* @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->consoleCode = $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
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 console output contains :output
* @Then console output should contain :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);
}

/**
* @Then console exit code is :code
* @Then console exit code should be :code
*/
public function consoleExitCodeIs(int $code): void
{
Assert::assertEquals($this->consoleCode, $code);
}
}
Loading