Done!

Creating PHP project boilerplate


Setting up Project

Initialize Compose

> composer init

Generates: After selecting options
{
    "name": "rosha/code-katas",
    "description": "PHP Boilerplate",
    "authors": [
        {
            "name": "Rosha",
            "email": "[email protected]"
        }
    ],
    "require": {}
}

Add Autoloader, phpunit

{
     "name": "rosha/code-katas",
    "description": "PHP Boilerplate",
    "authors": [
        {
            "name": "Rosha",
            "email": "[email protected]"
        }
    ],
    "require": {}
    "autoload": {
        "psr-4": {
            "PHPBoilerplate\\": "src/"
        }
    },
    "require-dev": {
        "phpunit/phpunit": "^6"
    }
}

Now running composer installon the terminal will install phpunit inside a vendor folder along with composer-lock file. It generates an autoload.php file. This file is used by composer to automatically load files based on the PSR-4 spec we stated earlier. Incase, autoload name is changed from say "PHPBoildeplate" to something else, composer dumpautoload needs to run on the terminal.

Create a src folder, test folder and phpunit.xml that is going to tell phpunit to look for the test folder to run the test

> mkdir src 
> mkdir test
> touch phpunit.xml

// Add the following content in the phpunit.xml that tells phpunit to look into test folder to run the text

<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./test/</directory>
        </testsuite>
    </testsuites>
</phpunit>

Now add a Greetings class in the src/Greetings folder

> cd src
> mkdir Greetings
> cd Greetings
> touch Greetings.php

//inside the Greetings.php

namespace PHPBoilerplate\Greetings;

class Greetings {
    public static function sayHelloWorld() {
        return 'Hello World';
    }
}

Now create a index.php file on the src folder that makes a uses Greetings

> cd src
> touch index.php

// inside index.php

require_once '../vendor/autoload.php';

use PHPBoilerplate\Greetings\Greetings;

echo Greetings::sayHelloWorld();

Now, to run the file from the command line:

> cd src
> php index.php

Running the Unit test

Withing the test folder create GreetingsTest.php file

> cd test
> touch GreetingsTest.php

//Content of GreetingsTest.php
namespace test;

use PHPUnit\Framework\TestCase;
use PHPBoilerplate\Greetings\Greetings;

class GreetingsTest extends TestCase
{
    /**
     * @test
     */
    public function displays_hello_world_message()
    {
        $this->assertEquals(
            'Hello World',
            Greetings::sayHelloWorld()
        );
    } 
}

Now, to run the php Unit

> phpunit --bootstrap vendor/autoload.php test

// or if phpunit not installed
> vendor/bin/phpunit

Setting up Git

> git inti

// Add vendor the the .gitignor
> echo "vendor" >> .gitignore

// Add & commit the change.
> git add .
> git commit -m 'Initializing Project'

// Add the repository
> git remote add origin https://github.com/rajsamb/PHPBoilerplate.git
> git push -u origin master