acacha/testinglaravelpackageinstaller
A minimal Laravel package installer test project. Provides a simple skeleton repository used to validate and experiment with Laravel package installation workflows, composer configuration, and installer behavior in a lightweight environment.
Installation
composer require --dev acacha/testinglaravelpackageinstaller
Add to composer.json under require-dev:
"extra": {
"laravel": {
"package-installer": {
"composer": {
"repositories": {
"custom-repo": {
"type": "vcs",
"url": "https://github.com/your-repo/package.git"
}
}
}
}
}
}
First Use Case In a test class, use the trait to install a package dynamically:
use Acacha\TestingLaravelPackageInstaller\TestingLaravelPackageInstaller;
class ExampleTest extends TestCase
{
use TestingLaravelPackageInstaller;
public function test_package_installation()
{
$this->installPackage('vendor/package', '1.0.0');
// Test logic here...
}
}
Dynamic Package Installation Install packages conditionally in tests:
if ($this->shouldTestFeatureX()) {
$this->installPackage('vendor/feature-x', 'dev-main');
}
Integration with setUp()
Centralize package installation in test setup:
public function setUp(): void
{
parent::setUp();
$this->installPackage('vendor/core', '2.1.*');
}
Composer Configuration Overrides
Extend composer.json dynamically:
$this->installPackage('vendor/package', '1.0.0', [
'config': {
'extra': {
'laravel': {
'env': ['APP_DEBUG=true']
}
}
}
]);
Leverage with refreshDatabase()
Combine with Laravel’s database testing:
public function test_with_package_and_db()
{
$this->installPackage('vendor/db-utils', '1.2.*');
$this->refreshDatabase();
// Test DB interactions...
}
Use with withFactories()
Install packages before factory loading:
public function test_with_factories()
{
$this->installPackage('vendor/factories', 'dev-master');
$this->withFactories(__DIR__.'/factories');
// Test factory usage...
}
Composer Cache Issues Clear Composer cache if packages fail to install:
composer clear-cache
Or programmatically:
$this->artisan('cache:clear')->expectsOutput('Cache cleared!');
Version Constraints
Avoid overly broad constraints (e.g., *) in tests—use exact versions or ranges (e.g., 1.0.0, ^2.0).
Dependency Conflicts Install packages in a specific order to avoid conflicts:
$this->installPackage('vendor/base', '1.0.0');
$this->installPackage('vendor/extension', '1.0.0'); // Depends on base
Check Installed Packages Verify installation via Composer:
$this->artisan('composer:dump-autoload')
->expectsOutput('Generating autoload files')
->assertExitCode(0);
Inspect composer.json
Temporarily dump the modified composer.json for debugging:
file_put_contents(
storage_path('app/composer.json'),
json_encode($this->getComposerConfig(), JSON_PRETTY_PRINT)
);
Custom Repositories Add VCS or package repositories dynamically:
$this->addComposerRepository('git@github.com:vendor/repo.git');
$this->installPackage('vendor/repo', 'dev-branch');
Post-Install Scripts Run scripts after installation:
$this->installPackage('vendor/package', '1.0.0', [
'scripts': {
'post-install-cmd': [
'php artisan package:discover'
]
}
]);
Environment-Specific Config
Use getenv() to conditionally install packages:
if (getenv('TEST_FEATURE_X')) {
$this->installPackage('vendor/feature-x', '1.0.0');
}
How can I help you explore Laravel packages today?