Skip to content

Commit a6116b7

Browse files
committed
tests: add E2E tests
1 parent 679010d commit a6116b7

File tree

6 files changed

+151
-1
lines changed

6 files changed

+151
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"psr-4": {
5151
"Inpsyde\\CodingStandard\\Tests\\": [
5252
"tests/src/",
53-
"tests/cases/"
53+
"tests/cases/",
54+
"tests/e2e/"
5455
]
5556
}
5657
},

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@
1616
<testsuite name="helpers">
1717
<directory>tests/cases/Helpers</directory>
1818
</testsuite>
19+
<testsuite name="e2e">
20+
<directory>tests/e2e/E2eTest.php</directory>
21+
</testsuite>
1922
</testsuites>
2023
</phpunit>

tests/e2e/E2eTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Inpsyde\CodingStandard\Tests;
6+
7+
use RuntimeException;
8+
9+
class E2eTest extends TestCase
10+
{
11+
private string $testPackagePath = '';
12+
private string $phpCsBinary = '';
13+
14+
protected function setUp(): void
15+
{
16+
$libPath = (string) getenv('LIB_PATH');
17+
$this->testPackagePath = $libPath . '/tests/e2e/test-package';
18+
$this->phpCsBinary = $libPath . '/vendor/bin/phpcs';
19+
}
20+
21+
public function testInpsydeAndTemplatesRulesets(): void
22+
{
23+
$output = [];
24+
exec(
25+
sprintf(
26+
'cd %s && %s',
27+
$this->testPackagePath,
28+
$this->phpCsBinary
29+
),
30+
$output
31+
);
32+
33+
/** @var array<string> $output> */
34+
if ($output[0] !== 'EE 2 / 2 (100%)') {
35+
throw new RuntimeException(implode("\n", $output));
36+
}
37+
38+
$json = end($output);
39+
40+
self::assertSame([
41+
'index.php' => [
42+
[
43+
'source' => 'Inpsyde.CodeQuality.NoElse.ElseFound',
44+
'line' => 12,
45+
],
46+
[
47+
'source' => 'WordPress.Security.EscapeOutput.OutputNotEscaped',
48+
'line' => 13,
49+
],
50+
],
51+
'template.php' => [
52+
53+
[
54+
'source' => 'WordPress.Security.EscapeOutput.OutputNotEscaped',
55+
'line' => 12,
56+
],
57+
[
58+
'source' => 'InpsydeTemplates.Formatting.TrailingSemicolon.Found',
59+
'line' => 12,
60+
],
61+
[
62+
'source' => 'Inpsyde.CodeQuality.DisableCallUserFunc.call_user_func_call_user_func',
63+
'line' => 15,
64+
],
65+
66+
],
67+
], $this->phpCsNormalizedOutput($json));
68+
}
69+
70+
/**
71+
* @psalm-return array<string, list<array{source: string, line: positive-int}>>
72+
*/
73+
private function phpCsNormalizedOutput(string $json): array
74+
{
75+
/** @var array{
76+
* files: array<string, array{
77+
* messages: list<array{
78+
* source: string,
79+
* line: positive-int,
80+
* ...
81+
* }>
82+
* }>
83+
* } $data */
84+
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
85+
86+
$result = [];
87+
foreach ($data['files'] as $fileName => $fileData) {
88+
$baseName = basename($fileName);
89+
$result[$baseName] = [];
90+
foreach ($fileData['messages'] as ['source' => $source, 'line' => $line]) {
91+
$result[$baseName][] = ['source' => $source, 'line' => $line];
92+
}
93+
}
94+
95+
return $result;
96+
}
97+
}

tests/e2e/test-package/index.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$value = rand(0, 1);
6+
7+
$successMessage = 'Success!';
8+
$errorMessage = 'Error!';
9+
10+
if ($value > 0.5) {
11+
echo esc_html($successMessage);
12+
} else {
13+
echo $errorMessage;
14+
}

tests/e2e/test-package/phpcs.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Coding Standard Test">
3+
4+
<file>./index.php</file>
5+
<file>./templates</file>
6+
7+
<arg value="sp"/>
8+
<arg name="report" value="json"/>
9+
10+
<rule ref="Inpsyde"/>
11+
12+
<rule ref="Inpsyde.CodeQuality.NoElse">
13+
<exclude-pattern>*/templates/*</exclude-pattern>
14+
</rule>
15+
16+
<rule ref="InpsydeTemplates">
17+
<include-pattern>*/templates/*</include-pattern>
18+
</rule>
19+
20+
</ruleset>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$value = rand(0, 1);
6+
$successMessage = 'Success!';
7+
$errorMessage = 'Error!' ?>
8+
9+
<?php if ($value > 0.5) : ?>
10+
<?= esc_html($successMessage) ?>
11+
<?php else : ?>
12+
<?= $errorMessage; ?>
13+
<?php endif;
14+
15+
call_user_func('strtolower', 'foo');

0 commit comments

Comments
 (0)