Skip to content

Commit 5b05a97

Browse files
committed
Modernize skeleton to PHP 8.2+, PSR-12, and PHPUnit 11/12
- Bump PHPUnit from the long-EOL 3.7.* to ^11.0 || ^12.0, dropping the unmaintained phpunit/dbunit and phpunit/phpunit-selenium dev dependencies that were unused by the skeleton and carried unnecessary supply-chain risk. - Switch autoloading from PSR-0 to PSR-4 and flatten lib/Application and tests/Application into lib/ and tests/ so the namespace root maps directly onto the source directory (mirrors DBDiff's src/ layout). - Rewrite Example.php and ExampleTest.php for PHP 8.2+: strict_types, scalar/union type hints, constructor-less typed property, PHPUnit attribute-based #[DataProvider] instead of the removed @dataProvider/ @ExpectedException annotations, and PSR-12 formatting throughout. - Modernize phpunit.xml to the PHPUnit 11 schema with a <source> block replacing the legacy <coverage><include> config. - Add PHP-CS-Fixer with a PSR-12 ruleset plus composer cs-check/cs-fix scripts. - Replace .travis.yml (PHP 5.4/5.5/5.6, long EOL and unpatched) with a GitHub Actions workflow that validates composer.json, checks coding standards, and runs tests across PHP 8.2-8.4. - Remove the committed composer.phar binary (stale, unpatched executable checked into version control) in favor of installing Composer via the CI tool cache / the developer's own install. - Update README for the new install/test/customize workflow. composer audit reports no known vulnerabilities in the new dependency set.
1 parent 9d7627f commit 5b05a97

12 files changed

Lines changed: 247 additions & 129 deletions

File tree

.github/workflows/tests.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
test:
11+
name: PHP ${{ matrix.php-version }}
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
php-version: ['8.2', '8.3', '8.4']
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
22+
- name: Setup PHP
23+
uses: shivammathur/setup-php@v2
24+
with:
25+
php-version: ${{ matrix.php-version }}
26+
coverage: none
27+
tools: composer:v2
28+
29+
- name: Validate composer.json
30+
run: composer validate --strict
31+
32+
- name: Install dependencies
33+
run: composer install --prefer-dist --no-progress
34+
35+
- name: Check coding standards
36+
run: composer cs-check
37+
38+
- name: Run tests
39+
run: composer test

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
vendor/
22
composer.lock
33
.project
4+
.idea/
5+
.vscode/
6+
.DS_Store
7+
.phpunit.cache/
8+
.phpunit.result.cache
9+
.php-cs-fixer.cache

.php-cs-fixer.dist.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$finder = (new PhpCsFixer\Finder())
6+
->in([__DIR__ . '/lib', __DIR__ . '/tests']);
7+
8+
return (new PhpCsFixer\Config())
9+
->setRiskyAllowed(true)
10+
->setRules([
11+
'@PSR12' => true,
12+
'declare_strict_types' => true,
13+
'strict_param' => true,
14+
'array_syntax' => ['syntax' => 'short'],
15+
'ordered_imports' => true,
16+
'no_unused_imports' => true,
17+
'trailing_comma_in_multiline' => true,
18+
])
19+
->setFinder($finder);

.travis.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

README.md

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# PHPUnit Skeleton [![Build Status](https://travis-ci.org/jasdeepkhalsa/phpunit-skeleton.svg)](https://travis-ci.org/jasdeepkhalsa/phpunit-skeleton)
1+
# PHPUnit Skeleton [![Tests](https://github.com/jasdeepkhalsa/phpunit-skeleton/actions/workflows/tests.yml/badge.svg)](https://github.com/jasdeepkhalsa/phpunit-skeleton/actions/workflows/tests.yml)
2+
23
_Get up and running with PHPUnit testing easily for your project with this skeleton_
34

45
* Are you frustrated with trying to install and configure PHPUnit?
@@ -9,60 +10,68 @@ Well, I'm happy to let you know that you just need to `clone` this repo and all
910

1011
We have also included a sample PHP class so you can learn a few tricks in how to use PHPUnit in your projects!
1112

12-
## Simple-tastic 3 Step Installation ##
13-
* Open a Terminal. Download the repo with `git clone https://github.com/jasdeepkhalsa/phpunit-skeleton.git`
14-
* `cd` into where you downloaded your repo and run `php composer.phar self-update`
15-
* Now, run `php composer.phar install --dev`
13+
## Requirements ##
14+
* PHP 8.2 or higher
15+
* [Composer](https://getcomposer.org/)
1616

17-
Note: `php` refers to the location of your php executable, if its not on your system's path
17+
## Simple-tastic 2 Step Installation ##
18+
* Open a Terminal. Download the repo with `git clone https://github.com/jasdeepkhalsa/phpunit-skeleton.git`
19+
* `cd` into where you downloaded your repo and run `composer install`
1820

1921
## Run PHPUnit ##
2022
* Open a Terminal
2123
* `cd` to your project root
22-
* Type `./vendor/bin/phpunit` and...magic! Tests (should) now be running!
24+
* Run `composer test` (or `./vendor/bin/phpunit` directly) and...magic! Tests (should) now be running!
2325

2426
You should see something like...
2527

26-
PHPUnit 3.7.19 by Sebastian Bergmann.
27-
28-
Configuration read from /(directory)/phpunit-skeleton/phpunit.xml
29-
30-
....
31-
32-
Time: 0 seconds, Memory: 3.00Mb
33-
34-
OK (4 tests, 4 assertions)
35-
36-
## Customizing PHPUnit Skeleton ##
28+
```
29+
PHPUnit 11.x by Sebastian Bergmann and contributors.
30+
31+
Runtime: PHP 8.3.x
32+
Configuration: /(directory)/phpunit-skeleton/phpunit.xml
33+
34+
.. 2 / 2 (100%)
35+
36+
Time: 00:00.010, Memory: 8.00 MB
37+
38+
OK (2 tests, 3 assertions)
39+
```
40+
41+
## Checking coding standards ##
42+
This skeleton ships with [PHP-CS-Fixer](https://cs.symfony.com/) configured for the PSR-12 coding standard.
43+
44+
* Check for violations: `composer cs-check`
45+
* Automatically fix violations: `composer cs-fix`
46+
47+
## Customizing PHPUnit Skeleton ##
3748
Once you've successfully installed PHPUnit Skeleton, you'll probably want to customize it to your application.
3849

3950
### How do I change the name of the application? ###
4051
Once you have the name of your application which we shall refer to as `YourApp`, then do the following:
4152

42-
1. First you have to change the entry `Application` in the `composer.json` file to `YourApp` under the object `psr-0`:
43-
44-
> {
45-
> "require-dev": {
46-
> "phpunit/phpunit": "3.7.*",
47-
> "phpunit/dbunit": ">=1.2",
48-
> "phpunit/phpunit-selenium": ">=1.2"
49-
> },
50-
> "autoload": {
51-
> "psr-0": {
52-
> "YourApp": "lib/"
53-
> }
54-
> }
55-
> }
56-
>
57-
58-
2. Next, rename the following two directories from `Application` to `YourApp`:
59-
* Rename `/lib/Application/` to `/lib/YourApp/`
60-
* Rename `/tests/Application/` to `/tests/YourApp/`
61-
3. Finally update the `namespace` inside the following php files:
62-
* Inside `/lib/YourApp/Example.php` update `namespace Application;` to `namespace YourApp;`
63-
* Inside `/tests/YourApp/ExampleTest.php` update `$this->obj = new Application\Example;` to `$this->obj = new YourApp\Example;`
64-
4. Run `php composer.phar update` again to update the sources
65-
5. Run `./vendor/bin/phpunit` again to make sure all tests are passing again
53+
1. First you have to change the entry `Application` in the `composer.json` file to `YourApp` under `autoload.psr-4` (and `autoload-dev.psr-4` if you keep the same pattern for tests):
54+
55+
```json
56+
{
57+
"autoload": {
58+
"psr-4": {
59+
"YourApp\\": "lib/"
60+
}
61+
},
62+
"autoload-dev": {
63+
"psr-4": {
64+
"Tests\\": "tests/"
65+
}
66+
}
67+
}
68+
```
69+
70+
2. Update the `namespace` inside the following PHP files:
71+
* Inside `/lib/Example.php` update `namespace Application;` to `namespace YourApp;`
72+
* Inside `/tests/ExampleTest.php` update `use Application\Example;` to `use YourApp\Example;`
73+
3. Run `composer dump-autoload` to refresh the autoloader
74+
4. Run `composer test` again to make sure all tests are passing again
6675

6776
## Credits ##
6877
* To the wonderful people on [Stack Overflow](http://stackoverflow.com/questions/15710410/autoloading-classes-in-phpunit-using-composer-and-autoload-php) for helping me understand PHPUnit better

composer.json

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
1-
{
2-
"require-dev": {
3-
"phpunit/phpunit": "3.7.*",
4-
"phpunit/dbunit": ">=1.2",
5-
"phpunit/phpunit-selenium": ">=1.2"
6-
},
7-
"autoload": {
8-
"psr-0": {
9-
"Application": "lib/"
10-
}
11-
}
12-
}
1+
{
2+
"name": "jasdeepkhalsa/phpunit-skeleton",
3+
"description": "A minimal, modern skeleton for getting started with PHPUnit testing in your PHP project.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Jasdeep Khalsa"
9+
}
10+
],
11+
"require": {
12+
"php": ">=8.2"
13+
},
14+
"require-dev": {
15+
"phpunit/phpunit": "^11.0 || ^12.0",
16+
"friendsofphp/php-cs-fixer": "^3.75"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Application\\": "lib/"
21+
}
22+
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"Tests\\": "tests/"
26+
}
27+
},
28+
"config": {
29+
"sort-packages": true,
30+
"optimize-autoloader": true
31+
},
32+
"minimum-stability": "stable",
33+
"prefer-stable": true,
34+
"scripts": {
35+
"test": "phpunit",
36+
"cs-check": "php-cs-fixer fix --dry-run --diff",
37+
"cs-fix": "php-cs-fixer fix"
38+
}
39+
}

composer.phar

-782 KB
Binary file not shown.

lib/Application/Example.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

lib/Example.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Application;
6+
7+
class Example
8+
{
9+
private int|float $number = 0;
10+
11+
/**
12+
* @throws \InvalidArgumentException if either argument is not numeric.
13+
*/
14+
public function add(mixed $x, mixed $y): int|float
15+
{
16+
if (!is_numeric($x) || !is_numeric($y)) {
17+
throw new \InvalidArgumentException('Both arguments must be numeric.');
18+
}
19+
20+
$this->number = $x + $y;
21+
22+
return $this->number;
23+
}
24+
}

phpunit.xml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
<?xml version="1.0" encoding="utf-8" ?>
2-
<phpunit bootstrap="./vendor/autoload.php">
3-
<testsuites>
4-
<testsuite name="The project's test suite">
5-
<directory>./tests</directory>
6-
</testsuite>
7-
</testsuites>
8-
</phpunit>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
cacheDirectory=".phpunit.cache"
7+
>
8+
<testsuites>
9+
<testsuite name="Application Test Suite">
10+
<directory suffix="Test.php">./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
14+
<source>
15+
<include>
16+
<directory suffix=".php">./lib</directory>
17+
</include>
18+
</source>
19+
</phpunit>

0 commit comments

Comments
 (0)