PHPStorm में PHPUnit testing framework सेटअप करने का चरण-दर-चरण गाइड।
पूर्वापेक्षाएँ
- PHPStorm IDE
- PHP interpreter configured
- Composer installed और project में initialized
चरण 1: PHPUnit Install करें
# Composer के माध्यम से (अनुशंसित) composer require --dev phpunit/phpunit ^10.0 # या specific version composer require --dev phpunit/phpunit:^9.5 # PHP 7.3+ के लिए # Verify ./vendor/bin/phpunit --version
चरण 2: phpunit.xml.dist Configuration
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/|VERSION|/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true">
<testsuites>
<testsuite name="Unit Tests">
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>
<testsuite name="Integration Tests">
<directory suffix="Test.php">tests/Integration</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src</directory>
</include>
</source>
<php>
<env name="APP_ENV" value="test"/>
<env name="DATABASE_URL" value="sqlite:///:memory:"/>
</php>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
चरण 3: PHPStorm में PHPUnit Configure करें
- Settings → Languages & Frameworks → PHP → Test Frameworks
- + → PHPUnit by Remote Interpreter / Local
- Path to phpunit.phar:
vendor/phpunit/phpunit/phpunit(याvendor/bin/phpunit) - Default configuration file:
phpunit.xml.distselect करें - Apply/OK
चरण 4: Test Class Generate करें
<?php
// src/Calculator.php
class Calculator {
public function add(float $a, float $b): float {
return $a + $b;
}
public function divide(float $a, float $b): float {
if ($b === 0.0) {
throw new InvalidArgumentException('Cannot divide by zero');
}
return $a / $b;
}
}
PHPStorm में: Calculator class पर Right-click → Generate → PHPUnit Test
<?php
// tests/Unit/CalculatorTest.php
declare(strict_types=1);
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use App\Calculator;
class CalculatorTest extends TestCase {
private Calculator $calculator;
protected function setUp(): void {
$this->calculator = new Calculator();
}
public function testAdd(): void {
$this->assertEquals(5.0, $this->calculator->add(2.0, 3.0));
$this->assertEquals(0.0, $this->calculator->add(-1.0, 1.0));
}
public function testDivide(): void {
$this->assertEquals(2.0, $this->calculator->divide(10.0, 5.0));
}
public function testDivideByZeroThrowsException(): void {
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot divide by zero');
$this->calculator->divide(10.0, 0.0);
}
// Data provider के साथ
public function provideAddCases(): array {
return [
'positive' => [2, 3, 5],
'negative' => [-1, -2, -3],
'zero' => [0, 5, 5],
'float' => [1.5, 2.5, 4.0],
];
}
/**
* @dataProvider provideAddCases
*/
public function testAddDataProvider(float $a, float $b, float $expected): void {
$this->assertEquals($expected, $this->calculator->add($a, $b));
}
}
चरण 5: Run और Debug करें
# Command line से ./vendor/bin/phpunit # सभी tests ./vendor/bin/phpunit --testsuite "Unit Tests" ./vendor/bin/phpunit tests/Unit/CalculatorTest.php ./vendor/bin/phpunit --filter testAdd # Specific test # Coverage के साथ ./vendor/bin/phpunit --coverage-html coverage-report ./vendor/bin/phpunit --coverage-text
PHPStorm में:
- Test class/file पर Right-click → Run 'CalculatorTest'
- Debug icon से Debug करें breakpoints के साथ
- Run Dashboard (View → Tool Windows → Run) से results देखें
Best Practices
- AAA Pattern: Arrange, Act, Assert
- One assertion per test (ideally)
- Descriptive names:
testMethodName_Scenario_ExpectedResult - Isolation: Tests independent होने चाहिए
- Fast: Unit tests < 100ms each