Skip to content

Commit 44e7112

Browse files
committed
Add Console::setLanguage method
1 parent feb4f4a commit 44e7112

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/Console.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ class Console
5858
*/
5959
public function __construct(Language $language = null)
6060
{
61-
if ($language === null) {
62-
$language = new Language('en');
61+
if ($language) {
62+
$this->setLanguage($language);
6363
}
64-
$this->language = $language->addDirectory(__DIR__ . '/Languages');
6564
global $argv;
6665
$this->prepare($argv ?? []);
6766
$this->setDefaultCommands();
@@ -130,14 +129,30 @@ public function getArgument(int $position) : ?string
130129
return $this->arguments[$position] ?? null;
131130
}
132131

132+
/**
133+
* Set the Language instance.
134+
*
135+
* @param Language|null $language
136+
*
137+
* @return static
138+
*/
139+
public function setLanguage(Language $language = null) : static
140+
{
141+
$this->language = $language ?? new Language();
142+
$this->language->addDirectory(__DIR__ . '/Languages');
143+
return $this;
144+
}
145+
133146
/**
134147
* Get the Language instance.
135148
*
136149
* @return Language
137150
*/
138-
#[Pure]
139151
public function getLanguage() : Language
140152
{
153+
if (!isset($this->language)) {
154+
$this->setLanguage();
155+
}
141156
return $this->language;
142157
}
143158

tests/ConsoleTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Framework\CLI\Command;
1313
use Framework\CLI\Streams\Stderr;
1414
use Framework\CLI\Streams\Stdout;
15+
use Framework\Language\Language;
1516
use PHPUnit\Framework\TestCase;
1617

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

33+
public function testLanguage() : void
34+
{
35+
$language = new Language();
36+
$console = new ConsoleMock($language);
37+
self::assertSame($language, $console->getLanguage());
38+
self::assertContains(
39+
\realpath(__DIR__ . '/../src/Languages') . \DIRECTORY_SEPARATOR,
40+
$console->getLanguage()->getDirectories()
41+
);
42+
}
43+
3244
public function testEmptyLine() : void
3345
{
3446
$this->console->prepare([

0 commit comments

Comments
 (0)