Skip to content

Commit 1681315

Browse files
Fixing the uninitialized datetime value. (#21)
* Adding tests for the command classes. * Fixing the un-initialized fixed datetime value.
1 parent f6311cc commit 1681315

4 files changed

Lines changed: 179 additions & 35 deletions

File tree

src/Business/Utility/Datetime.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
*/
1212
class Datetime extends PHPDateTime
1313
{
14-
public static ?string $fixedDate;
14+
/**
15+
* Used only in testing to fix the date for all tests.
16+
*/
17+
public static ?string $fixedDate = null;
1518

1619
public function __construct()
1720
{

src/Command/CognitiveMetricsCommand.php

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
105105
$metricsCollection = $this->metricsFacade->getCognitiveMetrics($path);
106106

107107
$this->handleBaseLine($input, $metricsCollection);
108-
$this->handleExport($input, $output, $metricsCollection);
108+
109+
$reportType = $input->getOption(self::OPTION_REPORT_TYPE);
110+
$reportFile = $input->getOption(self::OPTION_REPORT_FILE);
111+
112+
if ($reportType !== null || $reportFile !== null) {
113+
if ($this->isOneReportOptionMissing($reportType, $reportFile, $output)) {
114+
return Command::FAILURE;
115+
}
116+
if (!$this->isValidReportType($reportType, $output)) {
117+
return Command::FAILURE;
118+
}
119+
$this->metricsFacade->exportMetricsReport($metricsCollection, $reportType, $reportFile);
120+
return Command::SUCCESS;
121+
}
109122

110123
$this->renderer->render($metricsCollection, $this->metricsFacade->getConfig());
111124

@@ -146,39 +159,6 @@ private function loadConfiguration(string $configFile, OutputInterface $output):
146159
}
147160
}
148161

149-
/**
150-
* Handles exporting metrics to different formats based on user input.
151-
*
152-
* @param InputInterface $input
153-
* @param OutputInterface $output
154-
* @param CognitiveMetricsCollection $metricsCollection
155-
* @return bool
156-
* @throws CognitiveAnalysisException
157-
* @throws JsonException
158-
*/
159-
private function handleExport(InputInterface $input, OutputInterface $output, CognitiveMetricsCollection $metricsCollection): bool
160-
{
161-
$reportType = $input->getOption(self::OPTION_REPORT_TYPE);
162-
$reportFile = $input->getOption(self::OPTION_REPORT_FILE);
163-
164-
if (
165-
$this->areBothReportOptionsMissing($reportType, $reportFile)
166-
|| $this->isOneReportOptionMissing($reportType, $reportFile, $output)
167-
|| !$this->isValidReportType($reportType, $output)
168-
) {
169-
return false;
170-
}
171-
172-
$this->metricsFacade->exportMetricsReport($metricsCollection, $reportType, $reportFile);
173-
174-
return true;
175-
}
176-
177-
private function areBothReportOptionsMissing(?string $reportType, ?string $reportFile): bool
178-
{
179-
return $reportType === null && $reportFile === null;
180-
}
181-
182162
private function isOneReportOptionMissing(?string $reportType, ?string $reportFile, OutputInterface $output): bool
183163
{
184164
if (($reportType !== null && $reportFile === null) || ($reportType === null && $reportFile !== null)) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phauthentic\CognitiveCodeAnalysis\Tests\Unit\Command;
6+
7+
use Phauthentic\CognitiveCodeAnalysis\Application;
8+
use Phauthentic\CognitiveCodeAnalysis\CognitiveAnalysisException;
9+
use Phauthentic\CognitiveCodeAnalysis\Command\ChurnCommand;
10+
use PHPUnit\Framework\Attributes\Test;
11+
use PHPUnit\Framework\TestCase;
12+
use Symfony\Component\Console\Command\Command;
13+
use Symfony\Component\Console\Tester\CommandTester;
14+
15+
/**
16+
*
17+
*/
18+
class ChurnCommandTest extends TestCase
19+
{
20+
#[Test]
21+
public function testAnalyseNonExistentPath(): void
22+
{
23+
$this->expectException(CognitiveAnalysisException::class);
24+
$this->expectExceptionMessage('Path does not exist: does-not-exist');
25+
26+
$application = new Application();
27+
$command = $application->getContainer()->get(ChurnCommand::class);
28+
$tester = new CommandTester($command);
29+
30+
$tester->execute([
31+
'path' => 'does-not-exist',
32+
]);
33+
}
34+
35+
#[Test]
36+
public function testChurn(): void
37+
{
38+
$application = new Application();
39+
$command = $application->getContainer()->get(ChurnCommand::class);
40+
$tester = new CommandTester($command);
41+
42+
$tester->execute([
43+
'path' => __DIR__ . '/../../../src',
44+
]);
45+
46+
$this->assertEquals(Command::SUCCESS, $tester->getStatusCode(), 'Command should succeed');
47+
}
48+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phauthentic\CognitiveCodeAnalysis\Tests\Unit\Command;
6+
7+
use Phauthentic\CognitiveCodeAnalysis\Application;
8+
use Phauthentic\CognitiveCodeAnalysis\CognitiveAnalysisException;
9+
use Phauthentic\CognitiveCodeAnalysis\Command\CognitiveMetricsCommand;
10+
use PHPUnit\Framework\Attributes\DataProvider;
11+
use PHPUnit\Framework\Attributes\Test;
12+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\Console\Command\Command;
14+
use Symfony\Component\Console\Tester\CommandTester;
15+
16+
/**
17+
*
18+
*/
19+
class CognitiveMetricsCommandTest extends TestCase
20+
{
21+
#[Test]
22+
public function testAnalyseNonExistentPath(): void
23+
{
24+
$this->expectException(CognitiveAnalysisException::class);
25+
$this->expectExceptionMessage('Path does not exist: does-not-exist');
26+
27+
$application = new Application();
28+
$command = $application->getContainer()->get(CognitiveMetricsCommand::class);
29+
$tester = new CommandTester($command);
30+
31+
$tester->execute([
32+
'path' => 'does-not-exist',
33+
]);
34+
}
35+
36+
#[Test]
37+
public function testAnalyse(): void
38+
{
39+
$application = new Application();
40+
$command = $application->getContainer()->get(CognitiveMetricsCommand::class);
41+
$tester = new CommandTester($command);
42+
43+
$tester->execute([
44+
'path' => __DIR__ . '/../../../src',
45+
]);
46+
47+
$this->assertEquals(Command::SUCCESS, $tester->getStatusCode(), 'Command should succeed');
48+
}
49+
50+
#[Test]
51+
#[DataProvider('reportDataProvider')]
52+
public function testAnalyseWithJsonReport(array $input, int $returnCode): void
53+
{
54+
$tmpDir = sys_get_temp_dir();
55+
$file = $tmpDir . '/cognitive-metrics.json';
56+
$application = new Application();
57+
$command = $application->getContainer()->get(CognitiveMetricsCommand::class);
58+
$tester = new CommandTester($command);
59+
60+
$tester->execute(
61+
input: $input
62+
);
63+
64+
$this->assertEquals($returnCode, $tester->getStatusCode());
65+
66+
if ($returnCode === Command::SUCCESS) {
67+
$this->assertFileExists($file);
68+
} else {
69+
$this->assertFileDoesNotExist($file);
70+
}
71+
72+
unlink($file);
73+
}
74+
75+
public static function reportDataProvider(): array
76+
{
77+
$tmpDir = sys_get_temp_dir();
78+
$file = $tmpDir . '/cognitive-metrics.json';
79+
80+
return [
81+
'Successful Report Generation' => [
82+
'input' => [
83+
'path' => __DIR__ . '/../../../src',
84+
'--report-type' => 'json',
85+
'--report-file' => $file,
86+
],
87+
'returnCode' => Command::SUCCESS,
88+
],
89+
'Invalid Report Type' => [
90+
'input' => [
91+
'path' => __DIR__ . '/../../../src',
92+
'--report-type' => 'invalid-type',
93+
'--report-file' => $file,
94+
],
95+
'returnCode' => Command::FAILURE,
96+
],
97+
'Missing Report Option' => [
98+
'input' => [
99+
'path' => __DIR__ . '/../../../src',
100+
'--report-file' => $file,
101+
],
102+
'returnCode' => Command::FAILURE,
103+
],
104+
'Missing Report File Option' => [
105+
'input' => [
106+
'path' => __DIR__ . '/../../../src',
107+
'--report-type' => 'json',
108+
],
109+
'returnCode' => Command::FAILURE,
110+
]
111+
];
112+
}
113+
}

0 commit comments

Comments
 (0)