VOOZH about

URL: https://deepwiki.com/hypervel/testbench/2.4-reloading-and-environment-management

⇱ Reloading and Environment Management | hypervel/testbench | DeepWiki


Loading...
Last indexed: 7 February 2026 (93289f)
Menu

Reloading and Environment Management

Purpose and Scope

This document covers the mechanisms for managing application environment configuration and state reloading in TestCase. It explains three key capabilities:

  1. Environment Definition: The defineEnvironment() method for configuring the application before it boots
  2. Post-Creation Callbacks: The afterApplicationCreated() callback system for executing code after the application is created
  3. Application Reloading: The reloadApplication() method for recreating the application instance mid-test

For information about the broader test lifecycle including setUp() and tearDown(), see Test Lifecycle and Hooks. For details on how the application instance is created and the container is configured, see Application Creation and Container Setup.


Environment Definition Overview

The defineEnvironment() method provides a hook for configuring the application environment before service providers are booted. This is the correct place to register package-specific service providers, aliases, and configuration values.

Method Signature and Execution Context

The defineEnvironment() method is called during the application creation process in the parent BaseTestCase class. It receives the application instance as a parameter and is executed before providers are registered.

src/TestCase.php63-67


The default implementation delegates to two methods from the CreatesApplication trait:

  • registerPackageProviders(): Registers service providers returned by getPackageProviders()
  • registerPackageAliases(): Merges class aliases returned by getPackageAliases() into the application configuration

Sources: src/TestCase.php63-67


Environment Configuration Flow

The following diagram shows how environment configuration flows from override methods through to application registration:


Sources: src/TestCase.php63-67 src/Concerns/CreatesApplication.php19-58


Registering Package Providers

To register service providers for the package being tested, override the getPackageProviders() method:

src/Concerns/CreatesApplication.php19-22


Implementation Pattern


The providers are registered by registerPackageProviders() which iterates over the array and calls $app->register() for each provider:

src/Concerns/CreatesApplication.php37-42

Sources: src/Concerns/CreatesApplication.php19-22 src/Concerns/CreatesApplication.php37-42


Registering Package Aliases

To register class aliases (facades) for the package being tested, override the getPackageAliases() method:

src/Concerns/CreatesApplication.php29-32


Implementation Pattern


The aliases are merged into the application configuration by registerPackageAliases():

src/Concerns/CreatesApplication.php47-58

This method:

  1. Retrieves existing aliases from config->get('app.aliases', [])
  2. Merges the package aliases with existing aliases
  3. Sets the merged array back via config->set('app.aliases', ...)

Sources: src/Concerns/CreatesApplication.php29-32 src/Concerns/CreatesApplication.php47-58


Override Points for Environment Configuration

Override MethodReturn TypePurposeCalled By
getPackageProviders()array<int, class-string>Return service provider class names to registerregisterPackageProviders()
getPackageAliases()array<string, class-string>Return alias => class mappingsregisterPackageAliases()
defineEnvironment()voidConfigure application before bootParent setUp() during app creation

Sources: src/Concerns/CreatesApplication.php19-58 src/TestCase.php63-67


Advanced Environment Customization

For more complex environment configuration beyond providers and aliases, override defineEnvironment() entirely:


This pattern allows:

  • Modifying configuration values before providers boot
  • Setting environment-specific settings
  • Overriding default configuration from workbench/config/

Sources: src/TestCase.php63-67


After Application Created Callbacks

The afterApplicationCreated() method provides a mechanism to execute code after the application has been created and all providers have been registered, but before the test method runs.

Usage in setUp()

src/TestCase.php45-51


The callback is executed after:

  1. The application instance is created via createApplication()
  2. defineEnvironment() is called to configure the application
  3. Service providers are registered and booted
  4. The container is fully populated

The callback runs before:

  • The test method executes
  • Any database migrations are run

Multiple Callbacks

Multiple afterApplicationCreated() callbacks can be registered and they will be executed in registration order:


Sources: src/TestCase.php45-51


Application Creation Lifecycle with Callbacks

The following diagram shows the complete lifecycle of application creation including when defineEnvironment() and afterApplicationCreated() callbacks are executed:


Sources: src/TestCase.php38-58 src/TestCase.php63-67


Application Reloading

The reloadApplication() method provides a way to completely recreate the application instance during a test, resetting all state:

src/TestCase.php93-97


When to Use Application Reloading

Application reloading is useful in scenarios where:

  1. Configuration Changes: You need to test different configuration values in the same test method
  2. State Reset: The application state has been modified and needs to be reset to defaults
  3. Provider Re-registration: You need to re-register providers with different bindings
  4. Multiple Test Phases: A single test method needs to test behavior with different application setups

Reload Process

The reload process executes a full teardown and setup cycle:

PhaseMethod CalledActions
TeardowntearDown()Executes AfterEach attributes in coroutine context
Calls parent teardown
Resets Queue::createPayloadUsing()
SetupsetUp()Skips bootstrap (already done)
Registers new afterApplicationCreated callbacks
Creates new application via createApplication()
Calls defineEnvironment()
Executes BeforeEach attributes

Sources: src/TestCase.php93-97 src/TestCase.php38-58 src/TestCase.php80-88


Application Reload Lifecycle


Sources: src/TestCase.php93-97 src/TestCase.php38-58 src/TestCase.php80-88


Practical Usage Patterns

Pattern 1: Testing with Different Providers


Pattern 2: Testing Configuration Changes


Pattern 3: Environment Configuration with Dependencies


Sources: src/TestCase.php63-67 src/TestCase.php93-97


State Management Considerations

What Reloading Resets

When reloadApplication() is called, the following are reset:

ComponentReset Behavior
Application ContainerCompletely new instance created
Service ProvidersRe-registered and re-booted
ConfigurationReloaded from files and defineEnvironment()
RoutesRe-registered via setUpApplicationRoutes()
Database MigrationsRe-run via defineDatabaseMigrations()
Queue CallbacksReset via Queue::createPayloadUsing(null)
Swoole TimersCleared via Timer::clearAll()
CoordinatorsReset via CoordinatorManager::until()->resume()

What Reloading Does NOT Reset

  • Static properties in test classes
  • Global variables
  • static::$hasBootstrappedTestbench flag (remains true)
  • External resources (files, network connections, etc.)
  • Swoole server state

Sources: src/TestCase.php80-97 src/TestCase.php45-51


Integration with Test Lifecycle Hooks

The environment management methods integrate with the broader test lifecycle:


Sources: src/TestCase.php38-110


Summary

Key Methods Reference

MethodLocationPurposeOverride?
defineEnvironment()src/TestCase.php63-67Configure application before bootOverride to customize
getPackageProviders()src/Concerns/CreatesApplication.php19-22Return service providers to registerOverride to add providers
getPackageAliases()src/Concerns/CreatesApplication.php29-32Return aliases to registerOverride to add aliases
afterApplicationCreated()src/TestCase.php45-51Execute code after app creationCall with closure
reloadApplication()src/TestCase.php93-97Recreate application instanceCall when needed
registerPackageProviders()src/Concerns/CreatesApplication.php37-42Internal registration logicDo not override
registerPackageAliases()src/Concerns/CreatesApplication.php47-58Internal alias merging logicDo not override

When to Use Each Approach

  • Use getPackageProviders()/getPackageAliases() when you need to register providers/aliases for all tests in a class
  • Use defineEnvironment() when you need to modify configuration before providers boot
  • Use afterApplicationCreated() when you need to execute code after providers have booted
  • Use reloadApplication() when you need to reset application state mid-test

Sources: src/TestCase.php38-110 src/Concerns/CreatesApplication.php19-58

Refresh this wiki

On this page