Skip to content

Commit ec07763

Browse files
committed
updated display command
1 parent a79d7d7 commit ec07763

File tree

2 files changed

+77
-93
lines changed

2 files changed

+77
-93
lines changed

src/DisplayCommand.php

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,40 @@
44

55
class DisplayCommand implements CommandInterface
66
{
7-
private array $filesDiffContent;
8-
private array $filesContent;
9-
10-
public function __construct()
11-
{
12-
$this->filesContent = [];
13-
$this->filesDiffContent = [];
14-
}
15-
16-
private function constructContent($accum, $item)
7+
// Property to store displaying mode
8+
private string $mode;
9+
const AVAILABLE_MODES = [
10+
"differents",
11+
"content"
12+
];
13+
14+
public function __construct(string $mode = self::AVAILABLE_MODES[0])
1715
{
18-
return $accum .= "\n " . $item;
16+
$this->mode = $mode;
1917
}
2018

2119
public function execute(CommandInterface $command = null): CommandInterface
2220
{
2321
if (!is_null($command)) {
24-
$filesContent = $command->getFilesContent();
25-
26-
$keys = array_keys($filesContent);
27-
$file1Content = $filesContent[$keys[0]];
28-
$file2Content = $filesContent[$keys[1]];
29-
30-
$this->filesContent[] = "file1.json content:\n";
31-
$this->filesContent[] = array_reduce(
32-
$file1Content,
33-
[$this, 'constructContent'],
34-
"{"
35-
) . "\r}\n";
36-
37-
$this->filesContent[] = "file2.json content:\n";
38-
$this->filesContent[] = array_reduce(
39-
$file2Content,
40-
[$this, 'constructContent'],
41-
"{"
42-
) . "\n}\n";
43-
44-
$file1Keys = array_keys($file1Content);
45-
$this->filesDiffContent = array_map(
46-
function ($file1Key) use ($file1Content, $file2Content) {
47-
if (array_key_exists($file1Key, $file2Content)) {
48-
if (!strcmp($file1Content[$file1Key], $file2Content[$file1Key])) {
49-
return " " . $file1Content[$file1Key] . "\n";
50-
} else {
51-
return " - " . $file1Content[$file1Key] . "\n" .
52-
" + " . $file2Content[$file1Key] . "\n";
53-
}
54-
} else {
55-
return " - " . $file1Content[$file1Key] . "\n";
56-
}
57-
},
58-
$file1Keys
59-
);
22+
switch ($this->mode) {
23+
case self::AVAILABLE_MODES[0]:
24+
print_r($command->getFilesDiffs());
25+
break;
26+
case self::AVAILABLE_MODES[1]:
27+
print_r($command->getFilesContent());
28+
break;
29+
default:
30+
print_r("error: unknown mode");
31+
}
6032
}
33+
6134
return $this;
6235
}
6336

64-
public function showContentToConsole()
37+
public function setMode(string $mode): CommandInterface
6538
{
66-
echo implode("", $this->filesContent);
67-
}
39+
$this->mode = $mode;
6840

69-
public function showDiffsToConsole()
70-
{
71-
echo "{\n" . implode("", $this->filesDiffContent) . "}\n";
72-
}
73-
74-
public function getDiffs()
75-
{
76-
return "{\n" . implode("", $this->filesDiffContent) . "}\n";
41+
return $this;
7742
}
7843
}

tests/DisplayCommandTest.php

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,77 @@
44

55
use PHPUnit\Framework\TestCase;
66
use PHPUnit\Framework\Attributes\CoversClass;
7+
use PHPUnit\Framework\Attributes\CoversMethod;
78
use Differ\DisplayCommand;
8-
use Fixtures\CommandLineParsersStub;
99

1010
#[CoversClass(DisplayCommand::class)]
11+
#[CoversMethod(DisplayCommand::class, 'execute')]
1112
class DisplayCommandTest extends TestCase
1213
{
13-
public $displayCmd;
14-
private $file1Content;
15-
private $file2Content;
14+
private $filesContent;
15+
private $filesDiffs;
16+
private $filesDiffCmd;
1617

1718
protected function setUp(): void
18-
{
19-
$this->displayCmd = new DisplayCommand();
20-
21-
$this->file1Content =
22-
[
23-
"id" => "none",
24-
"host" => "hexlet.io",
25-
"timeout" => 50
26-
];
27-
$this->file2Content =
28-
[
29-
"timeout" => 20,
30-
"verbose" => 1,
31-
"host" => "hexlet.io"
32-
];
19+
{
20+
$this->filesContent = "file 1 content:\n" .
21+
"{\n" .
22+
" 'id': 'none',\n" .
23+
" 'host': 'hexlet.io',\n" .
24+
" 'timeout': 50\n" .
25+
"}\n" .
26+
"file 2 content:\n" .
27+
"{\n" .
28+
" 'timeout': 20,\n" .
29+
" 'verbose': 1,\n" .
30+
" 'host': 'hexlet.io'\n" .
31+
"}\n";
32+
33+
$this->filesDiffs = "{\n" .
34+
" + 'id': 'none',\n" .
35+
" 'host': 'hexlet.io',\n" .
36+
" - 'timeout': 50,\n" .
37+
" + 'timeout': 20,\n" .
38+
" + 'verbose': 1\n" .
39+
"}\n";
40+
41+
$this->filesDiffCmd = $this->createConfiguredStub(
42+
FilesDiffCommand::class,
43+
[
44+
'getFilesContent' => $this->filesContent,
45+
'getFilesDiffs' => $this->filesDiffs,
46+
]
47+
);
3348
}
3449

3550
public function testInstance()
3651
{
37-
$this->assertInstanceOf(DisplayCommand::class, $this->displayCmd);
52+
$displayCmd = new DisplayCommand();
53+
54+
$this->assertInstanceOf(DisplayCommand::class, $displayCmd);
3855
}
3956

40-
public function testExecute()
57+
public function testFilesDiffs()
4158
{
42-
$filesDiffCmd = $this->createConfiguredStub(
43-
FilesDiffCommand::class,
44-
[
45-
'getFilesContent' =>
46-
[
47-
"FILE1" => $this->file1Content,
48-
"FILE2" => $this->file2Content
49-
]
50-
]
51-
);
59+
$displayCmd = new DisplayCommand();
60+
61+
$displayCmd->execute($this->filesDiffCmd);
62+
$this->expectOutputString($this->filesDiffs);
63+
}
64+
65+
public function testFilesContent()
66+
{
67+
$displayCmd = new DisplayCommand();
5268

53-
$this->assertInstanceOf(DisplayCommand::class, $this->displayCmd
54-
->execute($filesDiffCmd));
69+
$displayCmd->setMode("content")->execute($this->filesDiffCmd);
70+
$this->expectOutputString($this->filesContent);
5571
}
5672

57-
public function testGetDiffs()
73+
public function testUnknownDisplayMode()
5874
{
59-
$this->assertEquals("{\n}\n", $this->displayCmd->getDiffs());
75+
$displayCmd = new DisplayCommand();
76+
77+
$displayCmd->setMode("extra")->execute($this->filesDiffCmd);
78+
$this->expectOutputString("error: unknown mode");
6079
}
6180
}

0 commit comments

Comments
 (0)