|
| 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