Skip to content

Commit 4bafc1c

Browse files
committed
update consoleApp
1 parent ec07763 commit 4bafc1c

File tree

2 files changed

+76
-26
lines changed

2 files changed

+76
-26
lines changed

src/ConsoleApp.php

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,14 @@ class ConsoleApp
88
private CommandInterface $currentCommand;
99
private CommandInterface $nextCommand;
1010
private CommandFactoryInterface $commandFactory;
11-
12-
public function __construct()
13-
{
14-
$this->docopt = <<<'DOCOPT'
15-
gendiff -h
16-
17-
Generate diff
18-
19-
Usage:
20-
gendiff (-h|--help)
21-
gendiff (-v|--version)
22-
gendiff [Options]... FILE1 FILE2
23-
24-
Options:
25-
-h --help Show this screen
26-
-v --version Show version
27-
--format <fmt> Report format [default: stylish]
28-
29-
DOCOPT;
30-
31-
$this->commandFactory = new CommandFactory($this->docopt);
11+
private FileReaderInterface $fileReader;
12+
13+
public function __construct(
14+
CommandFactoryInterface $commandFactory,
15+
FileReaderInterface $fileReader
16+
) {
17+
$this->commandFactory = $commandFactory;
18+
$this->fileReader = $fileReader;
3219
}
3320

3421
public function run(): void
@@ -39,13 +26,11 @@ public function run(): void
3926

4027
$this->currentCommand = $this->commandFactory->getCommand("difference");
4128
$this->nextCommand = $this->currentCommand
42-
->setFileNames($this->nextCommand)
43-
->setFileReader(new FileReader())
44-
->execute();
29+
->setFileReader($this->fileReader)
30+
->execute($this->nextCommand);
4531

4632
$this->currentCommand = $this->commandFactory->getCommand("show");
4733
$this->currentCommand
48-
->execute($this->nextCommand)
49-
->showDiffsToConsole();
34+
->execute($this->nextCommand);
5035
}
5136
}

tests/ConsoleAppTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Differ;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use PHPUnit\Framework\Attributes\CoversMethod;
8+
use Differ\CommandFactory;
9+
use Differ\DisplayCommand;
10+
use Differ\FilesDiffCommand;
11+
use Differ\FileReader;
12+
13+
#[CoversClass(ConsoleApp::class)]
14+
#[CoversClass(CommandFactory::class)]
15+
#[CoversClass(DisplayCommand::class)]
16+
#[CoversClass(FilesDiffCommand::class)]
17+
#[CoversClass(FileReader::class)]
18+
#[CoversMethod(ConsoleApp::class, 'run')]
19+
class ConsoleAppTest extends TestCase
20+
{
21+
public function testConsoleAppRunning()
22+
{
23+
$outputString = "{\n" .
24+
" hexlet.io\n" .
25+
" - 50\n" .
26+
" + 20\n" .
27+
" - 123.234.53.22\n" .
28+
" - \n" .
29+
"}\n";
30+
31+
$commandLineParserStubToGetContent = $this->createConfiguredStub(
32+
CommandLineParser::class,
33+
[
34+
'getFileNames' => [
35+
'FILE1' => __DIR__ . "/../file1.json",
36+
'FILE2' => __DIR__ . "/../file2.json"
37+
]
38+
]
39+
);
40+
$cmdLineParserStub = $this->createConfiguredStub(
41+
CommandLineParser::class,
42+
[
43+
'execute' => $commandLineParserStubToGetContent
44+
]
45+
);
46+
47+
$filesDiffCommand = new FilesDiffCommand();
48+
$displayCommand = new DisplayCommand();
49+
50+
$commandFactoryMock = $this->createMock(CommandFactory::class);
51+
$commandFactoryMock->expects($this->exactly(3))
52+
->method('getCommand')
53+
->willReturnMap([
54+
['parse', $cmdLineParserStub],
55+
['difference', $filesDiffCommand],
56+
['show', $displayCommand]
57+
]);
58+
$fileReader = new FileReader();
59+
60+
$consoleApp = new ConsoleApp($commandFactoryMock, $fileReader);
61+
$consoleApp->run();
62+
63+
$this->expectOutputString($outputString);
64+
}
65+
}

0 commit comments

Comments
 (0)