g1a/composer-test-scenarios
Install the package:
composer require --dev g1a/composer-test-scenarios
Define scenarios in composer.json:
"extra": {
"scenarios": {
"symfony4": {
"require": {
"symfony/console": "^4.0"
}
},
"phpunit6": {
"require-dev": {
"phpunit/phpunit": "^6.5"
}
}
}
}
Update scenarios:
composer scenario:update
This generates .scenarios.lock/ with lockfiles for each scenario.
Test a scenario locally:
composer scenario symfony4 && composer test
Use this to test your project against multiple dependency versions (e.g., Symfony 4 vs. 5) without maintaining separate branches or composer.lock files. Ideal for CI/CD pipelines (e.g., Travis/GitHub Actions) to validate compatibility.
CI/CD Integration:
SCENARIO, HIGHEST_LOWEST) to dynamically select scenarios and dependency resolutions..travis.yml snippet:
matrix:
include:
- env: SCENARIO=symfony4 HIGHEST_LOWEST=highest
- env: SCENARIO=phpunit6 HIGHEST_LOWEST=lowest
before_script:
- composer scenario:update
- composer scenario $SCENARIO
- if [ "$HIGHEST_LOWEST" = "highest" ]; then composer update; fi
- if [ "$HIGHEST_LOWEST" = "lowest" ]; then composer update --prefer-lowest; fi
Local Development:
composer scenario <name>.composer install or composer scenario default.License Compliance:
LICENSE with dependency licenses:
composer dependency-licenses
.scenarios.lock/ to ensure reproducible builds.config.platform in scenarios to mock PHP versions (e.g., php: "7.4")."create-lockfile": "false" for scenarios where lockfiles aren’t needed (e.g., dev-only dependencies).Lockfile Conflicts:
.scenarios.lock/ manually. Use composer scenario:update to regenerate.require constraints in composer.json.CI Cache Issues:
.scenarios.lock/ in CI to avoid redundant updates. Example (GitHub Actions):
cache:
key: scenarios-${{ hashFiles('composer.json') }}
paths:
- .scenarios.lock
Platform Overrides:
php: "7.1") in scenarios may override global composer.json config. Test thoroughly.Verify Scenario Installation:
composer scenario <name> && composer show
Check installed versions match expectations.
Dry Runs:
Use composer update --dry-run to preview changes before applying.
Custom Commands:
Extend the plugin by creating custom commands in composer.json:
"scripts": {
"post-scenario-update": "php scripts/post-scenario.php"
}
Pre/Post Hooks:
Use post-scenario-install or post-scenario-update scripts to run validation or migrations.
Dynamic Scenarios:
Generate scenarios programmatically by extending the plugin’s ScenarioManager class (advanced).
Travis Matrix Optimization: Combine scenarios with PHP versions for efficient testing:
matrix:
include:
- php: 7.4
env: SCENARIO=symfony4 HIGHEST_LOWEST=highest
- php: 8.0
env: SCENARIO=phpunit6 HIGHEST_LOWEST=lowest
Exclude Dev Dependencies:
Use "scenario-options": {"exclude-dev": true} to skip dev dependencies in a scenario.
Partial Updates: Update only specific scenarios with:
composer scenario:update symfony4
How can I help you explore Laravel packages today?