Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add E2E tests #86

Merged
merged 6 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/unit/cases",
"tests/e2e/cases"
]
}
},
Expand Down
15 changes: 13 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
<file>./Inpsyde/Helpers</file>
<file>./Inpsyde/Sniffs</file>
<file>./tests/src</file>
<file>./tests/cases</file>
<file>./tests/unit/cases</file>
<file>./tests/e2e/cases</file>

<!--
PHP 7.4 and higher.
Expand All @@ -24,7 +25,17 @@
</rule>

<rule ref="Inpsyde.CodeQuality.FunctionLength">
<exclude-pattern>./tests/cases/</exclude-pattern>
<exclude-pattern>./tests/unit/cases/</exclude-pattern>
</rule>

<rule ref="Inpsyde.CodeQuality.Psr4">
<exclude-pattern>./Inpsyde/Sniffs/</exclude-pattern>
<properties>
<property name="psr4" type="array">
<element key="Inpsyde\CodingStandard\Helpers" value="Inpsyde/Helpers"/>
<element key="Inpsyde\CodingStandard\Tests" value="tests/src|tests/unit/cases|tests/e2e/cases"/>
</property>
</properties>
</rule>

</ruleset>
7 changes: 5 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
</coverage>
<testsuites>
<testsuite name="fixtures">
<file>tests/cases/FixturesTest.php</file>
<file>tests/unit/cases/FixturesTest.php</file>
</testsuite>
<testsuite name="helpers">
<directory>tests/cases/Helpers</directory>
<directory>tests/unit/cases/Helpers</directory>
</testsuite>
<testsuite name="e2e">
<directory>tests/e2e/cases</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

putenv("LIB_PATH={$libDir}");
putenv('SNIFFS_NAMESPACE=Inpsyde\\Sniffs');
putenv("FIXTURES_PATH={$testsDir}/fixtures");
putenv("FIXTURES_PATH={$testsDir}/unit/fixtures");

if (!defined('PHPUNIT_COMPOSER_INSTALL')) {
define('PHPUNIT_COMPOSER_INSTALL', $autoload);
Expand Down
102 changes: 102 additions & 0 deletions tests/e2e/cases/E2eTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?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/fixtures/test-package';
$this->phpCsBinary = $libPath . '/vendor/bin/phpcs';
}

public function testInpsydeAndTemplatesRulesets(): void
{
$output = [];
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec
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);

// phpcs:disable Inpsyde.CodeQuality.LineLength.TooLong
$expectedMessages = [
'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,
],

],
];
// phpcs:enable Inpsyde.CodeQuality.LineLength.TooLong

self::assertSame($expectedMessages, $this->phpCsMessages($json));
}

/**
* @psalm-return array<string, list<array{source: string, line: positive-int}>>
*/
private function phpCsMessages(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/fixtures/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/fixtures/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/fixtures/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');
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace {

// @phpcsSniffPropertiesStart
$psr4 = ["\\Inpsyde\\CodingStandard\\Tests\\" => "tests/"];
$psr4 = ["\\Inpsyde\\CodingStandard\\Tests\\" => "tests/unit"];
$exclude = ["\\I\\Am\\Excluded\\Psr4Fixture"];
// @phpcsSniffPropertiesEnd
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.