Alaa Amer Articles

We offer a comprehensive collection of essential educational articles in web development to turn your ideas into digital reality

Modern PHP Development Tools: Composer, PHPUnit, & Xdebug

PHP 2025-12-31 Alaa Amer

Modern PHP Development Tools: Composer, PHPUnit, & Xdebug

Specialized Guide by Alaa Amer – Professional Web & App Developer

Modern tools in PHP development make work more efficient and high-quality. In this article, we'll explore the most important tools every professional PHP developer needs.

2️⃣ PHPUnit: Unit and Integration Testing

PHPUnit Setup:

<!-- phpunit.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
         testdox="true">

    <testsuites>
        <testsuite name="Unit Tests">
            <directory suffix="Test.php">tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature Tests">
            <directory suffix="Test.php">tests/Feature</directory>
        </testsuite>
    </testsuites>

    <source>
        <include>
            <directory suffix=".php">src</directory>
        </include>
    </source>

    <coverage>
        <report>
            <html outputDirectory="coverage-html"/>
        </report>
    </coverage>
</phpunit>

Unit Test Example:

<?php
// tests/Unit/CalculatorTest.php

use PHPUnit\Framework\TestCase;
use App\Calculator;

class CalculatorTest extends TestCase
{
    private Calculator $calculator;

    protected function setUp(): void
    {
        $this->calculator = new Calculator();
    }

    public function testAddition()
    {
        $result = $this->calculator->add(2, 3);
        $this->assertEquals(5, $result);
    }

    public function testDivisionByZero()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Division by zero is not allowed');

        $this->calculator->divide(10, 0);
    }

    /**
     * @dataProvider additionProvider
     */
    public function testAddWithDataProvider($a, $b, $expected)
    {
        $result = $this->calculator->add($a, $b);
        $this->assertEquals($expected, $result);
    }

    public function additionProvider()
    {
        return [
            [1, 2, 3],
            [5, 5, 10],
            [-2, 3, 1],
            [0, 0, 0]
        ];
    }
}

Integration Test with Database:

<?php
// tests/Feature/UserRepositoryTest.php

use PHPUnit\Framework\TestCase;
use App\Repository\UserRepository;

class UserRepositoryTest extends TestCase
{
    private PDO $pdo;
    private UserRepository $userRepository;

    protected function setUp(): void
    {
        // Create in-memory database for testing
        $this->pdo = new PDO('sqlite::memory:');
        $this->pdo->exec('
            CREATE TABLE users (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL,
                email TEXT UNIQUE NOT NULL
            )
        ');

        $this->userRepository = new UserRepository($this->pdo);
    }

    public function testCreateUser()
    {
        $userData = [
            'name' => 'Alaa Amer',
            'email' => '[email protected]'
        ];

        $user = $this->userRepository->create($userData);

        $this->assertInstanceOf(User::class, $user);
        $this->assertEquals('Alaa Amer', $user->getName());
        $this->assertNotNull($user->getId());
    }
}

4️⃣ PHP_CodeSniffer: Code Quality Checking

Install and Use:

# Install via Composer
composer require --dev squizlabs/php_codesniffer

# Check code
./vendor/bin/phpcs --standard=PSR12 src/

# Auto-fix issues
./vendor/bin/phpcbf --standard=PSR12 src/

phpcs.xml Configuration:

<?xml version="1.0"?>
<ruleset name="Custom Standard">
    <description>Custom coding standards for the project</description>

    <!-- Files and folders to check -->
    <file>src</file>
    <file>tests</file>

    <!-- Exclude certain files -->
    <exclude-pattern>*/vendor/*</exclude-pattern>

    <!-- Use PSR12 as base -->
    <rule ref="PSR12"/>

    <!-- Custom rules -->
    <rule ref="Generic.Files.LineLength">
        <properties>
            <property name="lineLimit" value="120"/>
        </properties>
    </rule>
</ruleset>

6️⃣ GitHub Actions for CI/CD

# .github/workflows/php.yml
name: PHP CI

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        php-version: [8.1, 8.2]

    steps:
      - uses: actions/checkout@v3

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

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

      - name: Run code style check
        run: ./vendor/bin/phpcs --standard=PSR12 src/

      - name: Run static analysis
        run: ./vendor/bin/phpstan analyse src --level=8

      - name: Run tests
        run: ./vendor/bin/phpunit --coverage-clover coverage.xml

💡 Best Practices for Tools

  1. Automate Everything: Use scripts in composer.json
  2. Continuous Checking: Integrate tools into CI/CD
  3. Unified Standards: Follow PSR and team rules
  4. Comprehensive Testing: Cover 80%+ of code
  5. Document Tools: Explain usage to team

Next Step

Learn Docker and Kubernetes for containerization and advanced deployment.

📩 Need help setting up advanced development environment?

PHP Composer PHPUnit Xdebug Development Tools Testing
Article Category
PHP

Modern PHP Development Tools: Composer, PHPUnit, & Xdebug

Comprehensive guide to essential modern PHP development tools and how to use them to improve workflow and quality.

Modern PHP Development Tools: Composer, PHPUnit, & Xdebug
01

Consultation & Communication

Direct communication via WhatsApp or phone to understand your project needs precisely.

02

Planning & Scheduling

Creating clear work plan with specific timeline for each project phase.

03

Development & Coding

Building projects with latest technologies ensuring high performance and security.

04

Testing & Delivery

Comprehensive testing and thorough review before final project delivery.

Alaa Amer
Alaa Amer

Professional web developer with over 10 years of experience in building innovative digital solutions.

Need This Service?

Contact me now for a free consultation and quote

WhatsApp Your satisfaction is our ultimate goal

What We Offer

  • Website Maintenance & Updates

    Keep your website secure updated optimized

  • API Integration

    Connect your systems with powerful APIs

  • Database Design & Optimization

    Faster queries cleaner structure fewer issues

  • Website Security Hardening

    Protect your site from cyber threats

  • Automation & Scripts

    Automate repetitive tasks and save time

Have Questions?

Call Us Now

00201014714795