Skip to content
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
39 changes: 39 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Tests

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
test:
name: PHP ${{ matrix.php-version }}
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php-version: ['8.2', '8.3', '8.4']

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: none
tools: composer:v2

- name: Validate composer.json
run: composer validate --strict

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Check coding standards
run: composer cs-check

- name: Run tests
run: composer test
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
vendor/
composer.lock
.project
.idea/
.vscode/
.DS_Store
.phpunit.cache/
.phpunit.result.cache
.php-cs-fixer.cache
19 changes: 19 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

$finder = (new PhpCsFixer\Finder())
->in([__DIR__ . '/lib', __DIR__ . '/tests']);

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@PSR12' => true,
'declare_strict_types' => true,
'strict_param' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => true,
'no_unused_imports' => true,
'trailing_comma_in_multiline' => true,
])
->setFinder($finder);
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

93 changes: 51 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# PHPUnit Skeleton [![Build Status](https://travis-ci.org/jasdeepkhalsa/phpunit-skeleton.svg)](https://travis-ci.org/jasdeepkhalsa/phpunit-skeleton)
# 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)

_Get up and running with PHPUnit testing easily for your project with this skeleton_

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

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

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

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

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

You should see something like...

PHPUnit 3.7.19 by Sebastian Bergmann.

Configuration read from /(directory)/phpunit-skeleton/phpunit.xml

....

Time: 0 seconds, Memory: 3.00Mb

OK (4 tests, 4 assertions)

## Customizing PHPUnit Skeleton ##
```
PHPUnit 11.x by Sebastian Bergmann and contributors.

Runtime: PHP 8.3.x
Configuration: /(directory)/phpunit-skeleton/phpunit.xml

.. 2 / 2 (100%)

Time: 00:00.010, Memory: 8.00 MB

OK (2 tests, 3 assertions)
```

## Checking coding standards ##
This skeleton ships with [PHP-CS-Fixer](https://cs.symfony.com/) configured for the PSR-12 coding standard.

* Check for violations: `composer cs-check`
* Automatically fix violations: `composer cs-fix`

## Customizing PHPUnit Skeleton ##
Once you've successfully installed PHPUnit Skeleton, you'll probably want to customize it to your application.

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

1. First you have to change the entry `Application` in the `composer.json` file to `YourApp` under the object `psr-0`:

> {
> "require-dev": {
> "phpunit/phpunit": "3.7.*",
> "phpunit/dbunit": ">=1.2",
> "phpunit/phpunit-selenium": ">=1.2"
> },
> "autoload": {
> "psr-0": {
> "YourApp": "lib/"
> }
> }
> }
>

2. Next, rename the following two directories from `Application` to `YourApp`:
* Rename `/lib/Application/` to `/lib/YourApp/`
* Rename `/tests/Application/` to `/tests/YourApp/`
3. Finally update the `namespace` inside the following php files:
* Inside `/lib/YourApp/Example.php` update `namespace Application;` to `namespace YourApp;`
* Inside `/tests/YourApp/ExampleTest.php` update `$this->obj = new Application\Example;` to `$this->obj = new YourApp\Example;`
4. Run `php composer.phar update` again to update the sources
5. Run `./vendor/bin/phpunit` again to make sure all tests are passing again
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):

```json
{
"autoload": {
"psr-4": {
"YourApp\\": "lib/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
}
```

2. Update the `namespace` inside the following PHP files:
* Inside `/lib/Example.php` update `namespace Application;` to `namespace YourApp;`
* Inside `/tests/ExampleTest.php` update `use Application\Example;` to `use YourApp\Example;`
3. Run `composer dump-autoload` to refresh the autoloader
4. Run `composer test` again to make sure all tests are passing again

## Credits ##
* 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
Expand Down
51 changes: 39 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
{
"require-dev": {
"phpunit/phpunit": "3.7.*",
"phpunit/dbunit": ">=1.2",
"phpunit/phpunit-selenium": ">=1.2"
},
"autoload": {
"psr-0": {
"Application": "lib/"
}
}
}
{
"name": "jasdeepkhalsa/phpunit-skeleton",
"description": "A minimal, modern skeleton for getting started with PHPUnit testing in your PHP project.",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Jasdeep Khalsa"
}
],
"require": {
"php": ">=8.2"
},
"require-dev": {
"phpunit/phpunit": "^11.0 || ^12.0",
"friendsofphp/php-cs-fixer": "^3.75"
},
"autoload": {
"psr-4": {
"Application\\": "lib/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"config": {
"sort-packages": true,
"optimize-autoloader": true
},
"minimum-stability": "stable",
"prefer-stable": true,
"scripts": {
"test": "phpunit",
"cs-check": "php-cs-fixer fix --dry-run --diff",
"cs-fix": "php-cs-fixer fix"
}
}
Binary file removed composer.phar
Binary file not shown.
21 changes: 0 additions & 21 deletions lib/Application/Example.php

This file was deleted.

24 changes: 24 additions & 0 deletions lib/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Application;

class Example
{
private int|float $number = 0;

/**
* @throws \InvalidArgumentException if either argument is not numeric.
*/
public function add(mixed $x, mixed $y): int|float
{
if (!is_numeric($x) || !is_numeric($y)) {
throw new \InvalidArgumentException('Both arguments must be numeric.');
}

$this->number = $x + $y;

return $this->number;
}
}
27 changes: 19 additions & 8 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="The project's test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
cacheDirectory=".phpunit.cache"
>
<testsuites>
<testsuite name="Application Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>

<source>
<include>
<directory suffix=".php">./lib</directory>
</include>
</source>
</phpunit>
36 changes: 0 additions & 36 deletions tests/Application/ExampleTest.php

This file was deleted.

Loading
Loading