GithubHelp home page GithubHelp logo

phpunit-travis-boilerplate's Introduction

PHPUnit boilerplate ๐Ÿ’

Build Status
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.

Table of contents

Installation and automated setup

  1. Clone this repository: git clone https://github.com/ulricaskarin/phpunit-travis-boilerplate.git
  2. In terminal: cd to folder: phpunit-travis-boilerplate
  3. Install composer: composer install
  4. Check further configuration and instructions on how to run tests

Manual setup

Overview of final structure of project

Project-root
โ”‚
โ””โ”€โ”€โ”€ src
โ”‚   โ”‚___ Foo.php
โ”‚  
โ””โ”€โ”€โ”€ tests
โ”‚   โ”‚___ FooTest.php
โ”‚  
โ””โ”€โ”€โ”€ vendor
โ”‚   โ”‚___ /bin
โ”‚   โ”‚___ /composer etc...
โ”‚
โ”‚ .gitignore
โ”‚ .travis.yml
โ”‚ composer.json
โ”‚ composer.lock
โ”‚ phpunit.xml
โ”‚ README.md

1. Create composer.json file

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.6
  • autoload : specifies that the package should be autoloaded using psr-4.

2. Install composer

  • In cmd terminal: composer install
  • A new directory named vendor is created (holding all dependencies required) and a composer.lock file.
  • Make sure to add vendor folder to your .gitignore!

3. Create phpunit.xml file

  • 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>

4. Add a simple class to test in folder /src

<?php

namespace model;

class Foo
{
    // code here
}

5. Add a simple test-class in folder /tests

  • Note: this class requires / uses the PHPUnit framework.
<?php

use PHPUnit\Framework\TestCase;
use model\Foo;

class FooTest extends TestCase {

    // code here
}

How to run tests in bash terminal vs PHPStorm IDE

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 choose PHPUnit
      • Name: "Name of your choice"
      • Test Runner: Check "Defined in the configuration file"
      • Check "Use alternative configuration file" , browse ... your project and point to your phpunit.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.

phpunit-travis-boilerplate's People

Contributors

ulricaskarin avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.