Skip to content

Commit e600cc0

Browse files
author
DKravtsov
committed
added application tests.
1 parent 4a54c22 commit e600cc0

26 files changed

Lines changed: 1346 additions & 3 deletions

‎phpunit.xml.dist‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
<ini name="date.timezone" value="UTC" />
3333
</php>
3434
<testsuites>
35+
<testsuite name="Application">
36+
<directory suffix="Test.php">./tests/Application</directory>
37+
</testsuite>
3538
<testsuite name="Integration">
3639
<directory suffix="Test.php">./tests/Integration</directory>
3740
</testsuite>

‎readme.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ jobs:
180180
## Excluding Files and Directories (Exact Segment Matching)
181181
The `--exclude` logic has been completely overhauled to fix long-standing upstream issues (such as [Issue #202](https://github.com/sebastianbergmann/phpcpd/issues/202)).
182182

183-
It now uses **exact path segment matching**. This means excluding a directory named `Vendor` will safely ignore the `Vendor/` folder, but will **not** accidentally exclude a folder named `Vendor2/`. You can reliably ignore specific directories, nested paths, or individual files.
183+
It now uses **exact path segment matching**. This means excluding a directory named `vendor` will safely ignore the `vendor/` folder, but will **not** accidentally exclude a folder named `vendor2/`. You can reliably ignore specific directories, nested paths, or individual files.
184184

185185
### Using the CLI
186186
Pass the `--exclude` flag for each specific path segment you want to ignore:
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Systemsdk\PhpCPD\Tests\Application;
6+
7+
use DOMDocument;
8+
use PHPUnit\Framework\TestCase;
9+
use Systemsdk\PhpCPD\Cli\Application;
10+
11+
final class ApplicationConfigAndSuppressTest extends TestCase
12+
{
13+
private string $configPath;
14+
private string $reportPath;
15+
private string $expectedPath;
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$configDistPath = getcwd() . '/phpcpd.config.php.dist';
22+
$this->configPath = getcwd() . '/phpcpd.config.php';
23+
24+
$reportDir = getcwd() . '/reports/phpcpd';
25+
$this->reportPath = $reportDir . '/phpcpd-report.xml';
26+
27+
// Corrected path typos to match standard project structure
28+
$this->expectedPath = getcwd() . '/tests/Fixture/Log/pmd_expected2.xml';
29+
30+
if (!is_dir($reportDir)) {
31+
mkdir($reportDir, 0777, true);
32+
}
33+
34+
// Clean up previous runs
35+
if (file_exists($this->reportPath)) {
36+
unlink($this->reportPath);
37+
}
38+
if (file_exists($this->configPath)) {
39+
unlink($this->configPath);
40+
}
41+
42+
// Create the config file from the dist template
43+
if (file_exists($configDistPath)) {
44+
copy($configDistPath, $this->configPath);
45+
}
46+
}
47+
48+
protected function tearDown(): void
49+
{
50+
parent::tearDown();
51+
52+
// Clean up generated files after the test
53+
if (file_exists($this->reportPath)) {
54+
unlink($this->reportPath);
55+
}
56+
57+
if (file_exists($this->configPath)) {
58+
unlink($this->configPath);
59+
}
60+
}
61+
62+
public function testConfigLoadingAndSuppressCpdAuditInPmdReport(): void
63+
{
64+
// Ensure the config file was actually created successfully before running
65+
self::assertFileExists($this->configPath);
66+
67+
$application = new Application();
68+
69+
$argv = [
70+
'phpcpd',
71+
'tests/Fixture/SuppressCase1',
72+
'--log-pmd=' . $this->reportPath,
73+
];
74+
75+
ob_start();
76+
$application->run($argv);
77+
ob_get_clean();
78+
79+
self::assertFileExists($this->reportPath);
80+
81+
// Read and sanitize XML to remove dynamic timestamp and version attributes
82+
$expectedXml = $this->sanitizeXml((string)file_get_contents($this->expectedPath));
83+
$actualXml = $this->sanitizeXml((string)file_get_contents($this->reportPath));
84+
85+
// Use strict XML string comparison (ignores whitespace and formatting)
86+
self::assertXmlStringEqualsXmlString($expectedXml, $actualXml);
87+
}
88+
89+
/**
90+
* Removes dynamic attributes (phpcpdVersion, timestamp) from the root XML node to prevent test failures during
91+
* new version releases.
92+
*/
93+
private function sanitizeXml(string $xmlContent): string
94+
{
95+
$dom = new DOMDocument();
96+
$dom->loadXML($xmlContent);
97+
98+
$rootNode = $dom->documentElement;
99+
100+
if ($rootNode !== null) {
101+
if ($rootNode->hasAttribute('phpcpdVersion')) {
102+
$rootNode->removeAttribute('phpcpdVersion');
103+
}
104+
if ($rootNode->hasAttribute('timestamp')) {
105+
$rootNode->removeAttribute('timestamp');
106+
}
107+
}
108+
109+
return (string)$dom->saveXML();
110+
}
111+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Systemsdk\PhpCPD\Tests\Application;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Systemsdk\PhpCPD\Cli\Application;
9+
10+
final class ApplicationExcludeTest extends TestCase
11+
{
12+
private string $reportPath;
13+
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
$reportDir = getcwd() . '/reports/phpcpd';
19+
$this->reportPath = $reportDir . '/phpcpd-exclude-report.json';
20+
21+
if (!is_dir($reportDir)) {
22+
mkdir($reportDir, 0777, true);
23+
}
24+
25+
if (file_exists($this->reportPath)) {
26+
unlink($this->reportPath);
27+
}
28+
}
29+
30+
protected function tearDown(): void
31+
{
32+
parent::tearDown();
33+
34+
if (file_exists($this->reportPath)) {
35+
unlink($this->reportPath);
36+
}
37+
}
38+
39+
public function testExcludeExactSegmentMatching(): void
40+
{
41+
$application = new Application();
42+
43+
$argv = [
44+
'phpcpd',
45+
'tests/Fixture/Exclude',
46+
'--exclude', 'Vendor',
47+
'--log-json=' . $this->reportPath,
48+
];
49+
50+
ob_start();
51+
$application->run($argv);
52+
ob_get_clean();
53+
54+
self::assertFileExists($this->reportPath);
55+
56+
$jsonOutput = (string)file_get_contents($this->reportPath);
57+
58+
// 'Vendor' must be entirely missing from the results
59+
self::assertStringNotContainsString('Vendor/', $jsonOutput);
60+
61+
// 'Vendor2' must be present because exact segment matching prevents accidental exclusion
62+
self::assertStringContainsString('Vendor2/', $jsonOutput);
63+
64+
// 'Src' must also be present
65+
self::assertStringContainsString('Src/', $jsonOutput);
66+
}
67+
68+
public function testMultipleExcludesAndNestedPaths(): void
69+
{
70+
$application = new Application();
71+
72+
$argv = [
73+
'phpcpd',
74+
'tests/Fixture/Exclude',
75+
'--exclude', 'Vendor',
76+
'--exclude', 'App/Services',
77+
'--log-json=' . $this->reportPath,
78+
];
79+
80+
ob_start();
81+
$application->run($argv);
82+
ob_get_clean();
83+
84+
self::assertFileExists($this->reportPath);
85+
86+
$jsonOutput = (string)file_get_contents($this->reportPath);
87+
88+
// Both explicitly excluded paths should be missing
89+
self::assertStringNotContainsString('Vendor/', $jsonOutput);
90+
self::assertStringNotContainsString('App/Services/', $jsonOutput);
91+
92+
// The remaining valid paths should still be scanned and present in the report
93+
self::assertStringContainsString('Vendor2/', $jsonOutput);
94+
self::assertStringContainsString('Src/', $jsonOutput);
95+
}
96+
97+
public function testExcludeIndividualFile(): void
98+
{
99+
$application = new Application();
100+
101+
// Testing the exclusion of a specific file as mentioned in the docs
102+
$argv = [
103+
'phpcpd',
104+
'tests/Fixture/Exclude',
105+
'--exclude', 'Src/FileA.php',
106+
'--log-json=' . $this->reportPath,
107+
];
108+
109+
ob_start();
110+
$application->run($argv);
111+
ob_get_clean();
112+
113+
self::assertFileExists($this->reportPath);
114+
115+
$jsonOutput = (string)file_get_contents($this->reportPath);
116+
117+
// The specific excluded file must be missing
118+
self::assertStringNotContainsString('Src/FileA.php', $jsonOutput);
119+
120+
// However, other files in the same directory must still be scanned and present
121+
self::assertStringContainsString('Src/FileB.php', $jsonOutput);
122+
}
123+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Systemsdk\PhpCPD\Tests\Application;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Systemsdk\PhpCPD\Cli\Application;
9+
10+
final class ApplicationHelpVersionTest extends TestCase
11+
{
12+
public function testInvalidArgumentTriggersExceptionAndReturnsErrorCode(): void
13+
{
14+
$application = new Application();
15+
16+
// Testing the catch (Exception) block for invalid arguments
17+
$argv = [
18+
'phpcpd',
19+
'--an-invalid-flag-that-does-not-exist',
20+
];
21+
22+
ob_start();
23+
$exitCode = $application->run($argv);
24+
$output = (string)ob_get_clean();
25+
26+
// Must return 1 according to line 46 in the screenshot
27+
self::assertSame(1, $exitCode, 'Application should return error code 1 for invalid arguments.');
28+
29+
// Output must contain the version string (line 43)
30+
self::assertStringContainsString('Copy/Paste Detector', $output);
31+
}
32+
33+
public function testVersionFlagReturnsSuccessCode(): void
34+
{
35+
$application = new Application();
36+
37+
// Testing the $arguments->version() block
38+
$argv = [
39+
'phpcpd',
40+
'--version',
41+
];
42+
43+
ob_start();
44+
$exitCode = $application->run($argv);
45+
$output = (string)ob_get_clean();
46+
47+
// Must return 0 according to line 55 in the screenshot
48+
self::assertSame(0, $exitCode, 'Application should return success code 0 for --version flag.');
49+
50+
// Output must contain the version string (printed on line 50 before the version check)
51+
self::assertStringContainsString('Copy/Paste Detector', $output);
52+
}
53+
54+
public function testHelpFlagReturnsSuccessCodeAndDisplaysHelpText(): void
55+
{
56+
$application = new Application();
57+
58+
// Testing the $arguments->help() block
59+
$argv = [
60+
'phpcpd',
61+
'--help',
62+
];
63+
64+
ob_start();
65+
$exitCode = $application->run($argv);
66+
$output = (string)ob_get_clean();
67+
68+
// Must return 0 according to line 61 in the screenshot
69+
self::assertSame(0, $exitCode, 'Application should return success code 0 for --help flag.');
70+
71+
// Output must contain the version string and some help instructions
72+
self::assertStringContainsString('Copy/Paste Detector', $output);
73+
self::assertStringContainsString('Usage:', $output);
74+
self::assertStringContainsString('options:', $output);
75+
}
76+
}

0 commit comments

Comments
 (0)