VOOZH about

URL: https://deepwiki.com/hypervel/testbench/5-environment-bootstrapping

⇱ Environment Bootstrapping | hypervel/testbench | DeepWiki


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

Environment Bootstrapper

Purpose and Scope

The Environment Bootstrapper is responsible for initializing the testing environment before test execution begins. It loads configuration from testbench.yaml, defines required constants, generates runtime files, and registers cleanup handlers. This component executes once per test suite to prepare the application environment.

For information about the base test class that invokes the bootstrapper, see TestCase Base Class. For details about the configuration file format, see Testbench Configuration. For information about the ConfigProviderRegister system used by the bootstrapper, see ConfigProviderRegister.

Overview

The Bootstrapper class src/Bootstrapper.php15-146 is a static utility that performs environment setup during test suite initialization. The TestCase base class invokes Bootstrapper::bootstrap() once before running tests, ensuring a consistent environment across all test methods.

Bootstrap Process Flow


Sources: src/Bootstrapper.php21-40

Configuration Loading

The bootstrapper loads configuration from a YAML file using a fallback mechanism that searches for multiple filename patterns.

YAML File Discovery

The loadConfigFromYaml() method src/Bootstrapper.php80-96 searches for configuration files in the following order:

  1. testbench.yaml
  2. testbench.yaml.example
  3. testbench.yaml.dist

The search begins at the working path, which is determined by:

  • The TESTBENCH_WORKING_PATH constant if defined
  • Otherwise, the package directory (dirname(__DIR__))

The configuration is stored in the static $config property src/Bootstrapper.php17 and can be retrieved via getConfig() src/Bootstrapper.php42-45

Sources: src/Bootstrapper.php21-24 src/Bootstrapper.php80-96

Environment Initialization

Constant Definition

The bootstrap process defines two critical constants src/Bootstrapper.php27-33:

ConstantPurposeDefault Value
BASE_PATHRoot directory for the application{workingPath}/workbench
SWOOLE_HOOK_FLAGSSwoole coroutine hook configurationSWOOLE_HOOK_ALL

The BASE_PATH can be overridden via the hypervel key in testbench.yaml src/Bootstrapper.php28-30:


If not specified, BASE_PATH defaults to the workbench subdirectory within the working path.

ClassLoader Initialization

After setting up the environment, the bootstrapper initializes the Hypervel ClassLoader with a TestScanHandler src/Bootstrapper.php39:


This configures the framework's class loading and scanning behavior for the test environment.

Sources: src/Bootstrapper.php27-40

File Generation

The bootstrapper generates two synthetic files that are required for the application runtime.

Environment File (.env)

The generateEnv() method src/Bootstrapper.php98-108 creates a .env file from the env array in testbench.yaml:


Example from testbench.yaml:


This generates a .env file at BASE_PATH/.env with the content:

APP_NAME="Hypervel Testbench"

Sources: src/Bootstrapper.php98-108 testbench.yaml4-5

Composer Lock File

The generateComposerLock() method src/Bootstrapper.php56-78 creates a synthetic composer.lock file that contains provider configuration. This file mimics a real Composer lock file but exists solely to inform the Hypervel framework about available service providers.

Structure


Content Mapping

FieldSourceMethod
configConfigProviderRegistergetConfigProviders()
providerstestbench.yaml$config['providers']
dont-discovertestbench.yaml$config['dont-discover']

The getConfigProviders() method src/Bootstrapper.php110-117 integrates with ConfigProviderRegister to build the complete provider list:


Example from testbench.yaml:


This provider is included in the generated composer.lock file.

Sources: src/Bootstrapper.php56-78 src/Bootstrapper.php110-117 testbench.yaml1-2

Cleanup Management

The bootstrapper implements automatic cleanup of generated files and directories using PHP shutdown handlers.

Purge Configuration

The registerPurgeFiles() method src/Bootstrapper.php119-145 reads the purge section from testbench.yaml and registers a shutdown function to delete specified resources:


Cleanup Process


All paths are resolved relative to BASE_PATH src/Bootstrapper.php132-139:


This ensures that generated test artifacts do not persist between test suite executions, maintaining a clean environment.

Sources: src/Bootstrapper.php119-145 testbench.yaml13-19

Filesystem Abstraction

The bootstrapper uses the Hypervel\Filesystem\Filesystem class for all file operations. A singleton instance is maintained via the getFilesystem() method src/Bootstrapper.php47-54:


This filesystem instance provides methods used throughout the bootstrapper:

MethodUsage
replace()Write/overwrite files atomically
exists()Check file/directory existence
delete()Remove individual files
deleteDirectory()Remove directories recursively

Sources: src/Bootstrapper.php8 src/Bootstrapper.php19 src/Bootstrapper.php47-54

Integration Points

Relationship with Other Components


Execution Context

The bootstrapper is designed to be called exactly once per test suite execution. The TestCase base class ensures this through static checks. The bootstrapper's static configuration storage persists across all test methods within a suite, providing consistent environment settings.

Sources: src/Bootstrapper.php15-146

Key Methods Reference

MethodVisibilityPurposeReturns
bootstrap()public staticMain entry point; initializes environmentvoid
getConfig()public staticRetrieves loaded configurationarray
loadConfigFromYaml()protected staticLoads YAML configuration filevoid
generateEnv()protected staticCreates .env file from configvoid
generateComposerLock()protected staticCreates composer.lock with providersvoid
getConfigProviders()protected staticGets providers from registerarray
registerPurgeFiles()protected staticSets up cleanup handlersvoid
getFilesystem()protected staticReturns singleton Filesystem instanceFilesystem

Sources: src/Bootstrapper.php15-146