Skip to content

Commit c5fcc63

Browse files
authored
Merge pull request #86 from inpsyde/feat/e2e-tests
2 parents da47537 + 0aae3e5 commit c5fcc63

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+173
-7
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/unit/cases",
54+
"tests/e2e/cases"
5455
]
5556
}
5657
},

phpcs.xml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
<file>./Inpsyde/Helpers</file>
77
<file>./Inpsyde/Sniffs</file>
88
<file>./tests/src</file>
9-
<file>./tests/cases</file>
9+
<file>./tests/unit/cases</file>
10+
<file>./tests/e2e/cases</file>
1011

1112
<!--
1213
PHP 7.4 and higher.
@@ -24,7 +25,17 @@
2425
</rule>
2526

2627
<rule ref="Inpsyde.CodeQuality.FunctionLength">
27-
<exclude-pattern>./tests/cases/</exclude-pattern>
28+
<exclude-pattern>./tests/unit/cases/</exclude-pattern>
29+
</rule>
30+
31+
<rule ref="Inpsyde.CodeQuality.Psr4">
32+
<exclude-pattern>./Inpsyde/Sniffs/</exclude-pattern>
33+
<properties>
34+
<property name="psr4" type="array">
35+
<element key="Inpsyde\CodingStandard\Helpers" value="Inpsyde/Helpers"/>
36+
<element key="Inpsyde\CodingStandard\Tests" value="tests/src|tests/unit/cases|tests/e2e/cases"/>
37+
</property>
38+
</properties>
2839
</rule>
2940

3041
</ruleset>

phpunit.xml.dist

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
</coverage>
1212
<testsuites>
1313
<testsuite name="fixtures">
14-
<file>tests/cases/FixturesTest.php</file>
14+
<file>tests/unit/cases/FixturesTest.php</file>
1515
</testsuite>
1616
<testsuite name="helpers">
17-
<directory>tests/cases/Helpers</directory>
17+
<directory>tests/unit/cases/Helpers</directory>
18+
</testsuite>
19+
<testsuite name="e2e">
20+
<directory>tests/e2e/cases</directory>
1821
</testsuite>
1922
</testsuites>
2023
</phpunit>

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
putenv("LIB_PATH={$libDir}");
3939
putenv('SNIFFS_NAMESPACE=Inpsyde\\Sniffs');
40-
putenv("FIXTURES_PATH={$testsDir}/fixtures");
40+
putenv("FIXTURES_PATH={$testsDir}/unit/fixtures");
4141

4242
if (!defined('PHPUNIT_COMPOSER_INSTALL')) {
4343
define('PHPUNIT_COMPOSER_INSTALL', $autoload);

tests/e2e/cases/E2eTest.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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/fixtures/test-package';
18+
$this->phpCsBinary = $libPath . '/vendor/bin/phpcs';
19+
}
20+
21+
public function testInpsydeAndTemplatesRulesets(): void
22+
{
23+
$output = [];
24+
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec
25+
exec(
26+
sprintf(
27+
'cd %s && %s',
28+
$this->testPackagePath,
29+
$this->phpCsBinary
30+
),
31+
$output
32+
);
33+
34+
/** @var array<string> $output> */
35+
if ($output[0] !== 'EE 2 / 2 (100%)') {
36+
throw new RuntimeException(implode("\n", $output));
37+
}
38+
39+
$json = end($output);
40+
41+
// phpcs:disable Inpsyde.CodeQuality.LineLength.TooLong
42+
$expectedMessages = [
43+
'index.php' => [
44+
[
45+
'source' => 'Inpsyde.CodeQuality.NoElse.ElseFound',
46+
'line' => 12,
47+
],
48+
[
49+
'source' => 'WordPress.Security.EscapeOutput.OutputNotEscaped',
50+
'line' => 13,
51+
],
52+
],
53+
'template.php' => [
54+
55+
[
56+
'source' => 'WordPress.Security.EscapeOutput.OutputNotEscaped',
57+
'line' => 12,
58+
],
59+
[
60+
'source' => 'InpsydeTemplates.Formatting.TrailingSemicolon.Found',
61+
'line' => 12,
62+
],
63+
[
64+
'source' => 'Inpsyde.CodeQuality.DisableCallUserFunc.call_user_func_call_user_func',
65+
'line' => 15,
66+
],
67+
68+
],
69+
];
70+
// phpcs:enable Inpsyde.CodeQuality.LineLength.TooLong
71+
72+
self::assertSame($expectedMessages, $this->phpCsMessages($json));
73+
}
74+
75+
/**
76+
* @psalm-return array<string, list<array{source: string, line: positive-int}>>
77+
*/
78+
private function phpCsMessages(string $json): array
79+
{
80+
/** @var array{
81+
* files: array<string, array{
82+
* messages: list<array{
83+
* source: string,
84+
* line: positive-int,
85+
* ...
86+
* }>
87+
* }>
88+
* } $data */
89+
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
90+
91+
$result = [];
92+
foreach ($data['files'] as $fileName => $fileData) {
93+
$baseName = basename($fileName);
94+
$result[$baseName] = [];
95+
foreach ($fileData['messages'] as ['source' => $source, 'line' => $line]) {
96+
$result[$baseName][] = ['source' => $source, 'line' => $line];
97+
}
98+
}
99+
100+
return $result;
101+
}
102+
}
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+
}
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');
File renamed without changes.

0 commit comments

Comments
 (0)