How to create a custom package in PHP

Package

Write Tests (Optional but Recomended)

Install PHPUnit

PowerShell
composer require --dev phpunit/phpunit

Write a Test

PHP
<?php
// File: hello-world-package/tests/HelloWorldPackageTest.php

use PHPUnit\Framework\TestCase;
use Ajeya\HelloWorldPackage\HelloWorldPackage;

class HelloWorldPackageTest extends TestCase
{
    public function testSayHello()
    {
        $helloWorld = new HelloWorldPackage();
        $this->assertEquals('Hello, World!', $helloWorld->sayHello());
    }
}

Configure PHPUnit

Create a phpunit.xml file in the root directory of your hello-world-package to configure PHPUnit:

XML
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
         colors="true"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="HelloWorldPackage Test Suite">
            <directory>./tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

Run the Tests

PowerShell
vendor/bin/phpunit

By Ajeya Paul

Full-Stack Developer from Kolkata, India