Skip to content

Commit

Permalink
tests: add E2E tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shvlv committed Mar 7, 2024
1 parent 679010d commit a6116b7
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"psr-4": {
"Inpsyde\\CodingStandard\\Tests\\": [
"tests/src/",
"tests/cases/"
"tests/cases/",
"tests/e2e/"
]
}
},
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
<testsuite name="helpers">
<directory>tests/cases/Helpers</directory>
</testsuite>
<testsuite name="e2e">
<directory>tests/e2e/E2eTest.php</directory>
</testsuite>
</testsuites>
</phpunit>
97 changes: 97 additions & 0 deletions tests/e2e/E2eTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Inpsyde\CodingStandard\Tests;

use RuntimeException;

class E2eTest extends TestCase
{
private string $testPackagePath = '';
private string $phpCsBinary = '';

protected function setUp(): void
{
$libPath = (string) getenv('LIB_PATH');
$this->testPackagePath = $libPath . '/tests/e2e/test-package';
$this->phpCsBinary = $libPath . '/vendor/bin/phpcs';
}

public function testInpsydeAndTemplatesRulesets(): void
{
$output = [];
exec(
sprintf(
'cd %s && %s',
$this->testPackagePath,
$this->phpCsBinary
),
$output
);

/** @var array<string> $output> */
if ($output[0] !== 'EE 2 / 2 (100%)') {
throw new RuntimeException(implode("\n", $output));
}

$json = end($output);

self::assertSame([
'index.php' => [
[
'source' => 'Inpsyde.CodeQuality.NoElse.ElseFound',
'line' => 12,
],
[
'source' => 'WordPress.Security.EscapeOutput.OutputNotEscaped',
'line' => 13,
],
],
'template.php' => [

[
'source' => 'WordPress.Security.EscapeOutput.OutputNotEscaped',
'line' => 12,
],
[
'source' => 'InpsydeTemplates.Formatting.TrailingSemicolon.Found',
'line' => 12,
],
[
'source' => 'Inpsyde.CodeQuality.DisableCallUserFunc.call_user_func_call_user_func',
'line' => 15,
],

],
], $this->phpCsNormalizedOutput($json));
}

/**
* @psalm-return array<string, list<array{source: string, line: positive-int}>>
*/
private function phpCsNormalizedOutput(string $json): array
{
/** @var array{
* files: array<string, array{
* messages: list<array{
* source: string,
* line: positive-int,
* ...
* }>
* }>
* } $data */
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);

$result = [];
foreach ($data['files'] as $fileName => $fileData) {
$baseName = basename($fileName);
$result[$baseName] = [];
foreach ($fileData['messages'] as ['source' => $source, 'line' => $line]) {
$result[$baseName][] = ['source' => $source, 'line' => $line];
}
}

return $result;
}
}
14 changes: 14 additions & 0 deletions tests/e2e/test-package/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

$value = rand(0, 1);

$successMessage = 'Success!';
$errorMessage = 'Error!';

if ($value > 0.5) {
echo esc_html($successMessage);
} else {
echo $errorMessage;
}
20 changes: 20 additions & 0 deletions tests/e2e/test-package/phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<ruleset name="Coding Standard Test">

<file>./index.php</file>
<file>./templates</file>

<arg value="sp"/>
<arg name="report" value="json"/>

<rule ref="Inpsyde"/>

<rule ref="Inpsyde.CodeQuality.NoElse">
<exclude-pattern>*/templates/*</exclude-pattern>
</rule>

<rule ref="InpsydeTemplates">
<include-pattern>*/templates/*</include-pattern>
</rule>

</ruleset>
15 changes: 15 additions & 0 deletions tests/e2e/test-package/templates/template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

$value = rand(0, 1);
$successMessage = 'Success!';
$errorMessage = 'Error!' ?>

<?php if ($value > 0.5) : ?>
<?= esc_html($successMessage) ?>
<?php else : ?>
<?= $errorMessage; ?>
<?php endif;

call_user_func('strtolower', 'foo');

0 comments on commit a6116b7

Please sign in to comment.