A simple boilerplate for getting PHPunit up and running for your projects. Travis included for continuous integration on GitHub. Be sure to read the documentation for PHPUnit and Travis CI.
- Clone this repository:
git clone https://github.com/ulricaskarin/phpunit-travis-boilerplate.git
- In terminal: cd to folder:
phpunit-travis-boilerplate
- Install composer:
composer install
- Check further configuration and instructions on how to run tests
Project-root
โ
โโโโ src
โ โ___ Foo.php
โ
โโโโ tests
โ โ___ FooTest.php
โ
โโโโ vendor
โ โ___ /bin
โ โ___ /composer etc...
โ
โ .gitignore
โ .travis.yml
โ composer.json
โ composer.lock
โ phpunit.xml
โ README.md
Composer is a PHP dependency manager, handling dependencies required to your project. Read more about Composer here.
- Create a file in the root of your project named: composer.json. It should look something like the below example. Read more about how to write a valid composer-schema here.
{
"name": "VendorName/ProjectName",
"description": "Short description of package",
"keywords": ["phpunit, Travis"],
"license": "MIT",
"authors": [
{
"name": "Your name",
"email": "[email protected]"
}
],
"require": {},
"require-dev": {
"phpunit/phpunit": "5.6.*"
},
"autoload": {
"psr-4": {
"model\\": "src"
}
}
}
require-dev
: requires our dev dependency of phpunit version 5.6autoload
: specifies that the package should be autoloaded using psr-4.
- In cmd terminal:
composer install
- A new directory named
vendor
is created (holding all dependencies required) and acomposer.lock
file. - Make sure to add vendor folder to your .gitignore!
- The file phpunit.xml file is where all settings for PHPUnit are defined. Should be located in the root directory.
bootstrap="vendor/autoload.php"
: defines where autoload file generated by composer will be located.- phpunit.xml file will add all test-classes with suffix .php found in the folder
/tests
to the test-suite. - Read more here how the phpunit.xml file may be configured.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="FooBar Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
<?php
namespace model;
class Foo
{
// code here
}
- Note: this class requires / uses the PHPUnit framework.
<?php
use PHPUnit\Framework\TestCase;
use model\Foo;
class FooTest extends TestCase {
// code here
}
In cmd terminal (git bash for example)
- No configuration needed
- Write
vendor/bin/phpunit
and hit enter. Test result will show up.
In PHPStorm IDE
- 1. Configuration:
Run\Edit Configurations\
: Press ( + ) sign to the left in the window. Scroll down and choosePHPUnit
- Name: "Name of your choice"
- Test Runner: Check "Defined in the configuration file"
- Check "Use alternative configuration file" , browse
...
your project and point to yourphpunit.xml
file. - Click on the small "config" symbol to the right of the browsing dots
...
This will open a new window for the PHPUnit library. - Now check
Use composer autoloader
and browse your project pointing path to script to:\vendor\autoload.php
- Hit Apply and OK.
(There might be an error showing up here "Error: Custom loader is not specified or invalid. Press Fix to edit project configuration".
If this is so - just hit the Fix-button and then Apply and OK once again.)
- 2. Run:
- Run the test-suite by pressing
Run
in IDE.
- Run the test-suite by pressing