Installation:
composer require hiqdev/hidev-phpunit
Ensure hiqdev/hidev is also installed (this plugin is a HiDev extension).
First Use Case: Run HiDev’s PHPUnit integration via CLI:
hidev phpunit
This executes PHPUnit with default configurations (generates phpunit.xml.dist and tests/_bootstrap.php if missing).
Key Files to Check:
hidev.json (HiDev config) – Look for phpunit section.tests/_bootstrap.php – Auto-generated bootstrap file for test setup.phpunit.xml.dist – Auto-generated PHPUnit config.Test Execution:
hidev phpunit # Run all tests
hidev phpunit --filter TestClass # Filter tests
hidev phpunit --coverage-html # Generate HTML coverage
--group, --exclude-group).Test Generation:
hidev gentest Model # Generate skeleton test for `Model`
hidev genfake Model # Generate fake test data for `Model`
phpunit-skeleton-generator under the hood. Outputs to tests/.Configuration:
Define PHPUnit settings in hidev.json:
{
"phpunit": {
"colors": true,
"bootstrap": "src/_bootstrap.php",
"config": "phpunit.xml.dist"
}
}
colors: Enable colored output (auto-detected for PHPUnit ≥6).bootstrap: Custom bootstrap file path (defaults to tests/_bootstrap.php).config: Custom PHPUnit config file (defaults to phpunit.xml.dist).Coverage Reports:
hidev phpunit --coverage-clover=coverage.clover
clover/html formats.Integration with HiDev:
hidev commands in CI/CD pipelines (e.g., Travis, GitHub Actions):
# .github/workflows/test.yml
- run: hidev phpunit --coverage-text
Custom Test Suites:
Extend phpunit.xml.dist to include multiple suites:
<phpunit>
<testsuites>
<testsuite name="Unit">
<directory>./tests/unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>./tests/feature</directory>
</testsuite>
</testsuites>
</phpunit>
Trigger via:
hidev phpunit --testsuite Unit
Dynamic Bootstrap:
Reference Laravel’s createTestingEnvironment() in tests/_bootstrap.php:
<?php
require __DIR__.'/../vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__.'/../');
$dotenv->load();
Parallel Testing:
Use PHPUnit’s --parallel flag:
hidev phpunit --parallel --threads=4
Deprecated PHPUnit 6 Support:
composer require --dev phpunit/phpunit:^9
Missing phpunit-skelgen:
hidev gentest fails, manually install the generator:
composer require --dev sebastianbergmann/phpunit-skeleton-generator
Bootstrap File Paths:
tests/_bootstrap.php or src/_bootstrap.php. Custom paths in hidev.json may break if files are missing.hidev phpunit --debug
HiDev Configuration Conflicts:
hidev.json has duplicate phpunit keys (e.g., from multiple plugins), the last defined key wins.hidev config:dump to inspect active configs.Coverage Reporting:
xdebug is misconfigured. Ensure:
; php.ini
xdebug.mode=coverage
xdebug.output_dir=/tmp
Verbose Output:
hidev phpunit --verbose
Reveals underlying PHPUnit commands and file paths.
Dry Run: Simulate test generation without writing files:
hidev gentest --dry-run Model
HiDev Logs:
Enable debug mode in hidev.json:
{
"debug": true
}
Custom Templates:
Override default templates (e.g., phpunit.xml.dist) by placing custom files in:
config/hidev-phpunit/
Example: config/hidev-phpunit/phpunit.xml.dist.
Pre/Post Hooks:
Use HiDev’s onRun events to extend functionality. Add to hidev.json:
{
"scripts": {
"pre-phpunit": "php artisan migrate --env=testing",
"post-phpunit": "php artisan optimize"
}
}
CI-Specific Configs: Use environment variables to toggle settings:
{
"phpunit": {
"colors": "${CI:-false}"
}
}
Cache Test Results:
Add to phpunit.xml.dist:
<phpunit>
<cacheResultFile>~/.phpunit.result.cache</cacheResultFile>
</phpunit>
Reduces execution time on repeated runs.
Exclude Slow Tests:
Use groups in phpunit.xml.dist:
<phpunit>
<exclude>@slow</exclude>
</phpunit>
Tag slow tests in your test classes:
/** @group slow */
class LongRunningTest extends TestCase { ... }
How can I help you explore Laravel packages today?