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

Adding CI tests #40

Merged
merged 12 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root = true

[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = tab
indent_size = 4

[*.{ts,tsx,js,jsx,scss,css,json,yaml,yml,feature,xml}]
indent_style = space
indent_size = 2

# Dotfiles
[.*]
indent_style = space
indent_size = 2
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CI

on:
pull_request:

jobs:
php-tests:
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- uses: actions/checkout@v4

- name: Run general tests
uses: alleyinteractive/action-test-general@develop

- name: Run PHP Tests
uses: alleyinteractive/action-test-php@develop
with:
skip-services: 'true'
wordpress-version: 'false'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ composer.lock
# IDEs
.idea
.vscode
.DS_Store
.phpunit.result.cache
10 changes: 10 additions & 0 deletions Alley-Interactive/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,14 @@
<rule ref="Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound">
<exclude-pattern>(class|trait)-(instance|singleton).php</exclude-pattern>
</rule>

<!-- Allow PSR-4 file format for test files -->
srtfisher marked this conversation as resolved.
Show resolved Hide resolved
<rule ref="WordPress.Files.FileName.NotHyphenatedLowercase">
<exclude-pattern>tests/*Test.php</exclude-pattern>
</rule>

<!-- Allow PSR-4 file format for test files -->
<rule ref="WordPress.Files.FileName.InvalidClassFileName">
<exclude-pattern>tests/*Test.php</exclude-pattern>
</rule>
</ruleset>
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ You can create a custom ruleset for your project that extends or customizes thes

This project adheres to [Keep a CHANGELOG](https://keepachangelog.com/en/1.0.0/).

### Unreleased

- Allow PSR-4 style `ClassName.php` file names to support our migration to PSR-4 for test files.

### 2.0.2

- Fix issue with files with `js`/`css` in the path being ignored.
Expand Down
49 changes: 35 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
{
"name": "alleyinteractive/alley-coding-standards",
"description": "PHPCS sniffs for Alley Interactive",
"type": "phpcodesniffer-standard",
"keywords": [ "phpcs", "static analysis" ],
"license": "GPL-2.0-or-later",
"require": {
"automattic/vipwpcs": "^3.0",
"phpcompatibility/phpcompatibility-wp": "^2.1.4"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
"name": "alleyinteractive/alley-coding-standards",
"description": "PHPCS sniffs for Alley Interactive",
"license": "GPL-2.0-or-later",
"type": "phpcodesniffer-standard",
"keywords": [
"phpcs",
"static analysis"
],
"require": {
"automattic/vipwpcs": "^3.0",
"phpcompatibility/phpcompatibility-wp": "^2.1.4"
},
"require-dev": {
"phpunit/phpunit": "^11.2"
},
"autoload-dev": {
"psr-4": {
"Alley\\WP\\Coding_Standards\\Tests\\": "tests/"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"alleyinteractive/composer-wordpress-autoloader": true
}
},
"scripts": {
"phpcbf": "phpcbf --standard=Alley-Interactive tests/*Test.php",
"phpcs": "phpcs --standard=Alley-Interactive tests/*Test.php -vsn",
"phpunit": "phpunit",
"test": [
"@phpcs",
"@phpunit"
]
}
}
14 changes: 14 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
backupGlobals="false"
colors="true"
cacheDirectory=".phpunit.result.cache"
>
<testsuites>
<testsuite name="tests">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
</phpunit>
72 changes: 72 additions & 0 deletions tests/FixtureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* FixtureTest class file
*
* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound
*
* @package Alley\WP\Coding_Standards
*/

namespace Alley\WP\Coding_Standards\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
* Fixture Test
*/
class FixtureTest extends TestCase {
use PhpcsHelper;

/**
* Test that fixtures pass.
*
* @param string $file The file to test.
*/
#[DataProvider( 'passing_fixture_data_provider' )]
public function test_passing_fixtures( string $file ): void {
$this->process_phpcs_output( $this->run_phpcs( $file ) );
}

/**
* Test that fixtures fail.
*
* @param string $file The file to test.
*/
#[DataProvider( 'failing_fixture_data_provider' )]
public function test_failing_fixtures( string $file ): void {
$this->process_phpcs_output( $this->run_phpcs( $file ), expect_to_fail: true );
}

/**
* Returns an array of fixtures that should pass.
*
* @return array<string>
*/
public static function passing_fixture_data_provider(): array {
return array_map( fn ( $file ) => [ $file ], self::get_files_in_directory( __DIR__ . '/fixtures/pass' ) );
}

/**
* Returns an array of fixtures that should fail.
*
* @return array<array<string|array<string>>>
*/
public static function failing_fixture_data_provider(): array {
return array_map( fn ( $file ) => [ $file ], self::get_files_in_directory( __DIR__ . '/fixtures/fail' ) );
}

/**
* Returns an array of fixtures that should fail.
*
* @param string $directory The directory to get files from.
* @return array<string>
*/
protected static function get_files_in_directory( string $directory ): array {
if ( ! is_dir( $directory ) ) {
return [];
}

return glob( $directory . '/*' );
}
}
112 changes: 112 additions & 0 deletions tests/PhpcsHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* PhpcsHelper trait file
*
* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound
*
* @package Alley\WP\Coding_Standards
*/

namespace Alley\WP\Coding_Standards\Tests;

/**
* Fixture Test
*
* @mixin \PHPUnit\Framework\TestCase
*/
trait PhpcsHelper {
/**
* Run PHPCS on a file and get the JSON output.
*
* @param string $file The file to run PHPCS on.
* @return array<mixed>
*/
protected function run_phpcs( string $file ): array {
$this->assertFileExists( $file );

$base_path = dirname( __DIR__ );
$shell = sprintf(
'%s %s %s --standard=%s --no-cache --report=json',
PHP_BINARY,
"{$base_path}/vendor/bin/phpcs",
$file,
"{$base_path}/Alley-Interactive/ruleset.xml"
);

$output = json_decode( shell_exec( $shell ), true );

if ( ! is_array( $output ) ) {
$this->fail( 'Failed to run PHPCS' );
}

return $output;
}

/**
* Process the PHPCS output for a file.
*
* @param array<mixed> $output The PHPCS output.
* @param array<string> $ignored_errors The errors to ignore. If an error is found in this list, it will be ignored and not fail the test if it is not found.
* @param array<string> $expected_errors The errors that are expected to be found. If an error is not found that is in this list, the test will fail.
*/
protected function process_phpcs_output(
array $output,
array $ignored_errors = [],
array $expected_errors = [],
bool $expect_to_fail = false,
): void {
// Add expected errors to the list of ignored errors.
$ignored_errors = array_unique( array_merge( $ignored_errors, $expected_errors ) );

$expected_errors_not_found = $expected_errors;

foreach ( $output['files'] as $file => $data ) {
$errors = array_column( $data['messages'], 'source' );

// Remove any expected errors from the list.
$unexpected_messages = array_filter(
$data['messages'],
fn ( $message ) => ! in_array( $message['source'], $ignored_errors, true ),
);

// Check if the expected errors were found and if so, remove them from the list.
$expected_errors_not_found = array_filter(
$expected_errors_not_found,
fn ( $error ) => ! in_array( $error, $errors, true ),
);

if ( ! empty( $unexpected_messages ) ) {
// Bail if we expect the test to fail and there are errors.
if ( $expect_to_fail ) {
$this->assertTrue( true );

return;
}

$this->fail(
sprintf(
'Unexpected errors found in %s: %s',
substr( $file, strlen( __DIR__ ) ),
print_r( $unexpected_messages, true ),
),
);
} else {
$this->assertEmpty( $unexpected_messages );
}
}

$this->assertEmpty(
$expected_errors_not_found,
sprintf(
'Expected errors not found in %s: %s',
substr( $file, strlen( __DIR__ ) ),
print_r( $expected_errors_not_found, true ),
),
);

if ( $expect_to_fail ) {
$this->fail( 'Test did not fail as expected' );
}
}

}
8 changes: 8 additions & 0 deletions tests/fixtures/fail/array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/**
* Array test file.
*
* @package Alley\WP\Coding_Standards
*/

$ai_another = array( 5, 6, 7, 8 );
8 changes: 8 additions & 0 deletions tests/fixtures/pass/array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/**
* Array test file.
*
* @package Alley\WP\Coding_Standards
*/

$ai_example = [ 1, 2, 3 ];
10 changes: 10 additions & 0 deletions tests/fixtures/pass/capability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Capability test file.
*
* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
*
* @package Alley\WP\Coding_Standards
*/

current_user_can( 'new_capability' );
12 changes: 12 additions & 0 deletions tests/fixtures/pass/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Functions test file.
*
* @package Alley\WP\Coding_Standards
*/

$ai_function = function () {
return 'Hello, World!';
};

echo esc_html( $ai_function() );
11 changes: 11 additions & 0 deletions tests/fixtures/pass/ternary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Ternary test file.
*
* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
*
* @package Alley\WP\Coding_Standards
*/

$ai_initial = true;
$ai_result = $ai_initial ?: 'value';