Skip to content

Commit

Permalink
Add Console::setLanguage method
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed May 10, 2024
1 parent feb4f4a commit 44e7112
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ class Console
*/
public function __construct(Language $language = null)
{
if ($language === null) {
$language = new Language('en');
if ($language) {
$this->setLanguage($language);
}
$this->language = $language->addDirectory(__DIR__ . '/Languages');
global $argv;
$this->prepare($argv ?? []);
$this->setDefaultCommands();
Expand Down Expand Up @@ -130,14 +129,30 @@ public function getArgument(int $position) : ?string
return $this->arguments[$position] ?? null;
}

/**
* Set the Language instance.
*
* @param Language|null $language
*
* @return static
*/
public function setLanguage(Language $language = null) : static
{
$this->language = $language ?? new Language();
$this->language->addDirectory(__DIR__ . '/Languages');
return $this;
}

/**
* Get the Language instance.
*
* @return Language
*/
#[Pure]
public function getLanguage() : Language
{
if (!isset($this->language)) {
$this->setLanguage();
}
return $this->language;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Framework\CLI\Command;
use Framework\CLI\Streams\Stderr;
use Framework\CLI\Streams\Stdout;
use Framework\Language\Language;
use PHPUnit\Framework\TestCase;

final class ConsoleTest extends TestCase
Expand All @@ -29,6 +30,17 @@ protected function tearDown() : void
Stdout::reset();
}

public function testLanguage() : void
{
$language = new Language();
$console = new ConsoleMock($language);
self::assertSame($language, $console->getLanguage());
self::assertContains(
\realpath(__DIR__ . '/../src/Languages') . \DIRECTORY_SEPARATOR,
$console->getLanguage()->getDirectories()
);
}

public function testEmptyLine() : void
{
$this->console->prepare([
Expand Down

0 comments on commit 44e7112

Please sign in to comment.