Modern PHP Development Tools: Composer, PHPUnit, & Xdebug
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
- Automate Everything: Use scripts in composer.json
- Continuous Checking: Integrate tools into CI/CD
- Unified Standards: Follow PSR and team rules
- Comprehensive Testing: Cover 80%+ of code
- Document Tools: Explain usage to team
Next Step
Learn Docker and Kubernetes for containerization and advanced deployment.
📩 Need help setting up advanced development environment?
Article Category
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.
Consultation & Communication
Direct communication via WhatsApp or phone to understand your project needs precisely.
Planning & Scheduling
Creating clear work plan with specific timeline for each project phase.
Development & Coding
Building projects with latest technologies ensuring high performance and security.
Testing & Delivery
Comprehensive testing and thorough review before final project delivery.