Skip to content

Commit

Permalink
Adding some of the most common php tooling (#757)
Browse files Browse the repository at this point in the history
* phpunit for running unittests
* paratest for running the same unitests in parallel
* php-cs-fixer for enforcing formatting. 

php-cs-fixer could be considered to be promoted to being a linter as
soon as I work out how to parse its output and feed it through a
post-processor like phpstan is.

---------

Co-authored-by: Tyler Jang <[email protected]>
  • Loading branch information
matthewbaggett and TylerJang27 authored May 28, 2024
1 parent b447900 commit f228f49
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/actions/linter_tests/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ runs:
sudo rm /var/lib/apt/lists/lock || true
# Install non-hermetic linters
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get -y install libperl-critic-perl perltidy zlib1g-dev software-properties-common
DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC sudo apt-get -y install tzdata
sudo add-apt-repository ppa:ondrej/php
sudo apt install -y php8.0-fpm php8.0-xml php8.0-mbstring php8.0-curl
sudo apt install -y php8.2-fpm php8.2-xml php8.2-mbstring php8.2-curl
;;
macOS)
brew install cpm
Expand Down
14 changes: 13 additions & 1 deletion .github/actions/tool_tests/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,23 @@ runs:
with:
node-version: 18

- name: Specify defaults
- name: Install packages and specify defaults
run: |
echo "CLI_PATH=${{ inputs.cli-path }}" >> "$GITHUB_ENV"
case "$RUNNER_OS" in
Linux)
# Cleanup any existing apt locks
sudo killall apt-get || true
sudo rm /var/lib/apt/lists/lock || true
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt install -y php8.2-fpm php8.2-xml php8.2-mbstring php8.2-curl
;;
macOS)
brew install php gnupg
;;
Windows)
echo "PLATFORM_APPEND_ARGS=--maxWorkers=5" >> "$GITHUB_ENV"
if [[ "${{ inputs.cli-path }}" == "" ]]; then
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ trunk check enable {linter}
| Nix | [nixpkgs-fmt] |
| package.json | [sort-package-json] |
| Perl | [perlcritic], [perltidy] |
| PHP | [phpstan] |
| PHP | [php-cs-fixer], [phpstan] |
| PNG | [oxipng] |
| PowerShell | [psscriptanalyzer] |
| Prisma | [prisma] |
Expand Down Expand Up @@ -147,6 +147,7 @@ trunk check enable {linter}
[oxipng]: https://github.com/shssoichiro/oxipng#readme
[perlcritic]: https://metacpan.org/pod/Perl::Critic
[perltidy]: https://metacpan.org/dist/Perl-Tidy/view/bin/perltidy
[php-cs-fixer]: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer#readme
[phpstan]: https://phpstan.org/
[pmd]: https://pmd.github.io/
[pragma-once]: linters/pragma-once/README.md
Expand Down
7 changes: 7 additions & 0 deletions linters/php-cs-fixer/php-cs-fixer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { linterFmtTest } from "tests";
import { skipOS } from "tests/utils";

linterFmtTest({
linterName: "php-cs-fixer",
skipTestIf: skipOS(["win32"]),
});
36 changes: 36 additions & 0 deletions linters/php-cs-fixer/plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: 0.1
tools:
definitions:
- name: php-cs-fixer
runtime: php
package: friendsofphp/php-cs-fixer
known_good_version: 3.54.0
shims: [php-cs-fixer]
environment:
- name: PATH
list: ["${tool}/vendor/bin"]
health_checks:
- command: php-cs-fixer --version
parse_regex: PHP CS Fixer ${semver}
lint:
definitions:
- name: php-cs-fixer
description: Keeps PHP code up to standards
tools: [php-cs-fixer]
known_good_version: 3.54.0
files: [php]
commands:
# TODO(Tyler): Do we want to define a lint command here?
- name: format
output: rewrite
success_codes: [0]
formatter: true
batch: true
in_place: true
run: php-cs-fixer fix --using-cache=no --show-progress=none ${target}
direct_configs:
# uses ruleset @PSR12 when a config is not present.
- .php-cs-fixer.dist.php
affects_cache:
- .php-cs-fixer.php
suggest_if: config_present
15 changes: 15 additions & 0 deletions linters/php-cs-fixer/test_data/basic.in.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

class HelloWorld
{
private $b;

private $a;


public function sayHello(DateTimeImutable $date): void
{
$c=(ARRAY)array(1,2);
echo 'Hello, ' . $date->format('j. n. Y');
}
}
22 changes: 22 additions & 0 deletions linters/php-cs-fixer/test_data/php_cs_fixer_v3.54.0_basic.fmt.shot
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing formatter php-cs-fixer test basic 1`] = `
"<?php

declare(strict_types=1);

class HelloWorld
{
private $b;

private $a;


public function sayHello(DateTimeImutable $date): void
{
$c = (array)array(1,2);
echo 'Hello, ' . $date->format('j. n. Y');
}
}
"
`;
1 change: 1 addition & 0 deletions tools/diff-so-fancy/diff_so_fancy.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { toolInstallTest } from "tests";

// TODO(Tyler): tool def is missing healthcheck
toolInstallTest({
toolName: "diff-so-fancy",
toolVersion: "1.4.3",
Expand Down
1 change: 1 addition & 0 deletions tools/difft/difft.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { toolInstallTest } from "tests";

// TODO(Tyler): tool def is missing healthcheck
toolInstallTest({
toolName: "difft",
toolVersion: "0.56.1",
Expand Down
1 change: 1 addition & 0 deletions tools/gk/gk.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { toolInstallTest } from "tests";

// TODO(Tyler): tool def is missing healthcheck
toolInstallTest({
toolName: "gk",
toolVersion: "1.2.2",
Expand Down
1 change: 1 addition & 0 deletions tools/goreleaser/goreleaser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { osTimeoutMultiplier } from "tests/utils";
// This install is quite slow on some Linux machines.
jest.setTimeout(600000 * osTimeoutMultiplier);

// TODO(Tyler): tool def is missing healthcheck
toolInstallTest({
toolName: "goreleaser",
toolVersion: "1.25.1",
Expand Down
8 changes: 8 additions & 0 deletions tools/paratest/paratest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { toolInstallTest } from "tests";
import { skipOS } from "tests/utils";

toolInstallTest({
toolName: "paratest",
toolVersion: "7.4.3",
skipTestIf: skipOS(["win32"]),
});
14 changes: 14 additions & 0 deletions tools/paratest/plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 0.1
tools:
definitions:
- name: paratest
runtime: php
package: brianium/paratest
known_good_version: 7.4.3
shims: [paratest]
environment:
- name: PATH
list: ["${tool}/vendor/bin"]
health_checks:
- command: paratest --version
parse_regex: ParaTest v${semver}
8 changes: 8 additions & 0 deletions tools/phpunit/phpunit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { toolInstallTest } from "tests";
import { skipOS } from "tests/utils";

toolInstallTest({
toolName: "phpunit",
toolVersion: "11.1.3",
skipTestIf: skipOS(["win32"]),
});
14 changes: 14 additions & 0 deletions tools/phpunit/plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 0.1
tools:
definitions:
- name: phpunit
runtime: php
package: phpunit/phpunit
known_good_version: 11.1.3
shims: [phpunit]
environment:
- name: PATH
list: ["${tool}/vendor/bin"]
health_checks:
- command: phpunit --version
parse_regex: PHPUnit ${semver}
1 change: 1 addition & 0 deletions tools/tsc/tsc.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { toolInstallTest } from "tests";
import { skipOS } from "tests/utils";

// TODO(Tyler): tool def is missing healthcheck
toolInstallTest({
toolName: "tsc",
toolVersion: "5.2.2",
Expand Down
1 change: 1 addition & 0 deletions tools/yq/yq.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { toolInstallTest } from "tests";

// TODO(Tyler): tool def is missing healthcheck
toolInstallTest({
toolName: "yq",
toolVersion: "4.40.5",
Expand Down

0 comments on commit f228f49

Please sign in to comment.