Installation
composer require microshop/test
Add the service provider to config/app.php:
'providers' => [
// ...
MicroShop\Test\TestServiceProvider::class,
],
Publish Config
php artisan vendor:publish --provider="MicroShop\Test\TestServiceProvider" --tag="config"
Review config/test.php for default settings.
First Use Case: Basic Test Execution
use MicroShop\Test\Facades\TestRunner;
// Run a simple test
$result = TestRunner::run('UserTest');
dd($result); // Outputs test results (pass/fail, assertions, etc.)
Key Facades/Classes
TestRunner: Execute tests programmatically.TestGenerator: Scaffold new test classes.TestAssert: Custom assertions for MicroShop-specific logic.Generate a Test Class
TestGenerator::create('Feature\\User\\CreateUserTest', [
'uses' => 'UserTestCase',
'description' => 'Tests user creation flow',
]);
Outputs a skeleton test file in tests/Feature/User/.
Integrate with Laravel’s Testing
Extend MicroShop\Test\TestCase for shared setup:
use MicroShop\Test\TestCase;
class UserTestCase extends TestCase {
protected function setUp(): void {
parent::setUp();
$this->user = User::factory()->create();
}
}
Programmatic Test Execution
// Run all tests in a group
$results = TestRunner::runGroup('user');
// Filter by status
$failed = $results->filterByStatus('failed');
Custom Assertions
use MicroShop\Test\Facades\TestAssert;
TestAssert::assertMicroShopResponse($response, 200, 'success');
TestAssert::assertModelExists(User::class, ['email' => 'test@example.com']);
Hook into Application Events
Use TestListener to trigger tests on deployments or CI:
TestRunner::addListener(new SlackNotificationListener(config('test.slack_webhook')));
CI/CD Pipelines
Add to .github/workflows/test.yml:
- name: Run MicroShop Tests
run: php artisan test:run --group=regression
Database Transactions
Enable in config/test.php:
'database' => [
'transactions' => true,
],
Ensures tests run in isolated DB states.
Parallel Testing
Use TestRunner::parallel() for large test suites:
TestRunner::parallel()->runGroup('integration');
Database State Leakage
transactions in config or manually rollback:
$this->artisan('db:rollback')->run();
Facade Caching
TestRunner caches results by default, which can hide flaky tests.TestRunner::setCache(false);
Test Group Conflicts
unit and api) may cause unintended execution.README.md.Custom Assertions Overhead
Verbose Output
Enable debug mode in config/test.php:
'debug' => env('TEST_DEBUG', false),
Logs detailed test execution steps to storage/logs/test.log.
Test Isolation
If tests interfere, use TestRunner::isolate():
TestRunner::isolate()->run('UserTest');
Spins up a fresh Laravel instance for the test.
Mocking MicroShop Services
Use the MicroShopMock helper:
$this->mockMicroShopService('payment', function ($mock) {
$mock->shouldReceive('charge')->andReturn(true);
});
Custom Test Reporters
Implement MicroShop\Test\Contracts\TestReporter:
class CustomReporter implements TestReporter {
public function report(array $results) {
// Send to external system (e.g., Jira, Datadog)
}
}
Register via:
TestRunner::addReporter(new CustomReporter());
Dynamic Test Generation
Extend TestGenerator to auto-generate tests from API specs:
TestGenerator::fromOpenApi('api-spec.yaml');
Pre/Post Test Hooks Add setup/teardown logic:
TestRunner::before(function () {
// Seed test data
});
TestRunner::after(function () {
// Cleanup
});
Timezone Handling
Tests may fail if timezone differs from production. Set in config/test.php:
'timezone' => 'UTC',
Environment Variables
Use .env.testing for test-specific configs. Load via:
TestRunner::loadEnvironment('.env.testing');
Parallel Worker Limits
Adjust in config/test.php:
'parallel' => [
'workers' => 4, // Default: CPU core count
],
How can I help you explore Laravel packages today?