External contributions: When preparing an issue or pull request as an external contributor, read src/docs/contributions.md in full and follow its requirements. This does not apply to maintainer-directed work.
Agent workflow: This guide assumes two agents: an implementer and an independent peer providing second opinions and code review. Without a peer, replace each required second opinion with a concise, plain-language explanation of the issue and recommended solution to the owner, and wait for approval. Replace peer code review with a full self-review after implementation.
Hypervel is a standalone Laravel-style Swoole framework. The public API should stay close to Laravel wherever possible, while the internals are adapted for long-lived Swoole workers, coroutine safety, and high performance.
Laravel is the main API reference. Hyperf is a historical and architectural reference for some lower-level Swoole/coroutine packages, but Hypervel code should follow current Hypervel patterns rather than copying Hyperf structure mechanically.
Most work in this repo today is framework bug fixes and enhancements, or porting Laravel packages and updates. docs/ai/porting-hyperf.md applies only to the rare Hyperf package or update port.
This file is intentionally detailed because agents trained on Laravel will otherwise assume Laravel's request lifecycle and miss Hypervel's Swoole/coroutine constraints.
When working on Hypervel, start from this frame:
- Development monorepo: Applications install individual subtree-split packages, never
hypervel/components. Rootcomposer.jsonandpackage.jsonmetadata is development-only. Determine installed-application behavior from the relevant package manifests and the application's dependencies and discovery settings. - Hypervel is Laravel-shaped at the API level.
- Database connections are driver-neutral.
ConnectionInterfaceandConnectionsupport custom drivers registered throughDB::extend(), including non-PDO transports. - Hypervel is not request-per-process PHP — workers are long-lived, and PHP state does not disappear after each request.
- Singletons, static properties, manager registries, callbacks, config, and cached metadata can persist for the worker lifetime.
- Per-request state must live in coroutine-scoped storage (CoroutineContext), not process-global state.
- Laravel source is the default parity reference, but Laravel internals often assume per-request bootstrap and are not optimized to take advantage of static caching of immutable state.
- Hyperf source can be useful for Swoole/coroutine behavior, but Hyperf container/config/listener patterns are not the target architecture.
- Laravel's rate limiter lives under
Illuminate\Cache; Hypervel's canonical implementation is the dedicatedhypervel/rate-limiterpackage underHypervel\RateLimiter. It uses typed policies and dedicated atomic stores and has noHypervel\Cachealias or primitive counter API. Use this package directly when porting rate-limited Laravel code.
Always run framework commands from the repository root. This is the directory that contains this AGENTS.md, the root composer.json, phpunit.xml.dist, phpstan.neon, src/, and tests/.
Key paths:
| Path | Description |
|---|---|
src/docs/ |
Hypervel documentation and the source for the hypervel/docs package. |
src/framework/ |
Composer metadata for hypervel/framework, the metapackage that installs the standard framework components. Published to its own read-only split repository. |
src/testbench/ |
Hypervel's testbench package (port of orchestra/testbench). Contains TestCase, attributes (WithConfig, WithMigration), and bootstrap logic. Part of the monorepo, not a vendor dependency. |
src/testbench/hypervel/ |
Committed Hypervel app skeleton. On bootstrap, testbench clones this to a disposable temp directory (/tmp/hypervel-components-testbench-{token}-{pid}/) and points BASE_PATH at the clone — tests that write files under BASE_PATH (generated providers, migrations, fixtures, etc.) hit the temp copy, not this committed path. The clone is deleted on shutdown and stale copies from crashed runs are cleaned up. Testbench also exports TESTBENCH_BASE_PATH so subprocesses can locate the active runtime. |
src/testbench/workbench/ |
Committed shared test fixtures (NOT cloned). Subdirs are psr-4-mapped from the monorepo root as Workbench\App\*, Workbench\Database\Factories\*, Workbench\Database\Seeders\* so multiple tests can reuse the same models/factories/seeders without redefining them. Not the runtime app — that's the disposable clone of src/testbench/hypervel/. |
skeletons/ |
Application skeletons and starter kits, each published to its own read-only split repository. hypervel/ is the base application skeleton; react-starter-kit/ is the React starter kit. |
docs/ai/ |
Supplementary agent guides, including porting-hyperf.md (Hyperf conversion mechanics). |
docs/todo.md |
Tracked gaps and improvements worth doing. |
Always use composer test:parallel to run the full components test suite. This runs raw ParaTest with Hypervel's parallel testing flag supplied by phpunit.xml.dist.
Always use composer test:testbench after Testbench changes. This runs the scoped Testbench package-mode contract suite through the real package:test command.
ParaTest defaults to the machine CPU count when no process count is specified. Redis integration runs need REDIS_TEST_DB_MIN / REDIS_TEST_DB_MAX to cover the chosen worker count, or an explicit --processes / -p value that fits the configured range.
TEST_TOKEN is runner-owned worker identity. Tests must use the assigned value unless they explicitly exercise token-dependent behavior; such tests must restore every environment source they mutate or run in an isolated subprocess.
To run a single test class: ./vendor/bin/phpunit --no-progress path/to/TestClass.php.
The analyzer tests in tests/PHPStan run as a separate suite: use composer test:phpstan, or ./vendor/bin/phpunit -c phpunit.phpstan.xml.dist --no-progress path/to/TestClass.php for a single class.
Before editing, identify the work as one of:
- A framework bug fix.
- A framework enhancement.
- An update from an existing package's upstream.
- A new package port.
This determines which upstream source and tests to compare. Incremental updates from an existing upstream follow the workflow below together with the applicable Porting Policy and rules. New package ports follow the full workflow under Porting Packages. Bug fixes and enhancements follow the workflows below. In every case, read the package README for the upstream reference and the relevant Hypervel source and tests before editing.
When bringing an existing upstream feature, fix, or API change into Hypervel:
- Find the originating implementation pull request and any corresponding documentation pull request. Use them to understand the reason for the change and identify the complete set of files changed when it was introduced.
- Inspect every file changed by those pull requests, including source, contracts, tests, fixtures, configuration, package metadata, and documentation. Search the current upstream branch for the added symbols as well, since later changes may have introduced additional consumers or coverage.
- Treat the historical pull-request diffs as discovery and history only. Port the actual source, tests, and documentation from the current checked-out upstream default or development branch. Follow-up fixes and documentation improvements may have changed the final implementation or coverage.
- Compare that current upstream surface with the Hypervel implementation and apply the approved Hypervel adaptations under Porting Packages. If the upstream feature has no user-facing documentation, add proportionate Hypervel documentation at its natural public surface.
Every audit must explicitly check for overengineering, Laravel-style ergonomics, and avoidable performance or scalability costs, especially repeated hot-path work, excessive database or network round trips (e.g. Redis), inefficient query or index design, unnecessary allocation or serialization, unbounded work, and worker-lifetime memory growth.
Modifying code is an implicit assessment of it. Whenever you edit a method, move code, copy a file, or port from upstream, check what you touch for:
- Lifetime classification for any container-buildable class you add, port, or whose mutable state you change — see Container.
- Hardcoded values that should be derived (namespaces, defaults)
- Defensive code that masks bugs
- Conventions that diverge from how the framework actually does it
- Deprecated APIs or dated patterns
- Issues in the code right next to what you're changing
Anything found follows When to Stop and Report — "the task didn't ask me to fix that" and "I copied it verbatim" are not reasons to stay silent. The trigger is modification or code review; files read only for context don't need a line-by-line audit.
- Reproduce the bug with the smallest useful test.
- Trace the exact failing code path and identify the root cause before changing source.
- Compare the matching Laravel or package-upstream behavior when it defines the contract.
- Seek a second opinion before implementing a fix if it changes what callers are entitled to rely on, requires owner approval, or leaves a consequential design choice unresolved.
- Fix the underlying defect — never add a workaround for incorrect framework code.
- Add a regression test for the bug and run that test file immediately.
- Check types, API parity, coroutine isolation, and worker-lifetime state around the changed code.
- Complete the verification workflow below.
- Read the related Hypervel APIs and tests. Check Laravel or the package upstream for an established public API and behavior.
- Decide which state is local, coroutine-scoped, or worker-scoped before writing code — see Coroutine and Worker-Lifetime State.
- Preserve Laravel API parity by default. If parity conflicts with Hypervel's architecture, preserves a verified defect or deprecated upstream API, or would require worse code or a workaround, reach consensus with your peer agent on the cleanest design through a second-opinion loop before planning or editing. Never make Hypervel code worse merely to preserve parity.
- Test the public behavior, failure paths, coroutine isolation, and cleanup the feature needs.
- Complete the verification workflow below.
Run verification commands sequentially — Never overlap formatting, static analysis or tests. Wait for each command to finish before starting another. A tool’s built-in parallelism, such as ParaTest workers, is fine.
During implementation, run each new or changed test file immediately with ./vendor/bin/phpunit --no-progress path/to/TestClass.php (add -c phpunit.phpstan.xml.dist for tests/PHPStan).
Do not routinely run formatting or PHPStan during edits. Run targeted PHPStan earlier only when investigating a typing issue or when its feedback is needed to guide implementation.
At the end of a coherent implementation slice, run composer analyse and any outstanding tests needed to verify the changed behavior and affected callers. Use PHPUnit for one test file or composer test:parallel -- --processes=4 for multiple files, selecting the relevant files or using --filter. Reuse passing results unless subsequent changes invalidate them.
Run composer lint:fix before a scheduled code review or commit checkpoint, not after individual changes within a batch.
Run only the local tests needed to give strong confidence in the change. Leave full suites and CI matrices to CI, unless a specific unresolved issue requires broader local testing.
After a failure or review correction, fix the cause, repeat only checks affected by the correction, and finish any outstanding verification. Do not restart the entire sequence merely because another review round occurred.
The Working rules and the Avoid overengineering rules apply to all work in this repo. The Code conventions apply to newly written code. Laravel package ports preserve upstream naming, structure, and style except for the approved adaptations under Porting Packages. Hyperf ports follow docs/ai/porting-hyperf.md.
Apply approved adaptations when porting or modifying code. Do not expand a change solely to apply style or modernization conventions elsewhere, unless that cleanup was explicitly requested.
- Never use subagents without explicit user consent — Do not spawn or delegate work to subagents unless the user explicitly requests or approves their use.
- One change at a time — Work on one coherent change at a time. Sequence dependent edits, and review the complete affected behavior across files before considering the change finished.
- Never use Write to overwrite files — always use Edit for targeted updates.
- Always use
cpto copy files andmvto move/rename — never read → write new version → delete old version. - Copy before splitting — When a new file or class is primarily extracted from existing code, use
cpto copy the primary source first and then update the copy rather than rebuilding it from individual pieces. Copy any additional blocks, including comments and docblocks, into the destination before removing them from their source. - Grep broadly — never assume a subdir — when searching for any symbol, class, method, or pattern, grep across the whole
src/(ortests/) tree, not a specific package subdir. Assumptions about where something lives produce false negatives. - Read the source before describing behavior — never state how code behaves from memory or Laravel assumptions. Hypervel's coroutine runtime breaks many Laravel assumptions; if you haven't read the relevant source, read it first.
- Swoole references — Use the official Swoole documentation on GitHub. Do not use OpenSwoole documentation: the runtimes have diverged.
- Treat past owner decisions as context, not constraints — Previous owner approvals and completed plans explain history but do not determine the best design today. Never retain or reject a design merely because it was previously approved; decide from current requirements, code, and evidence.
- Revert failed attempts immediately — when a fix doesn't work, revert it before trying another approach. Don't leave experimental code in place.
- Check dependency versions before adding them — Before adding a package dependency to the root
composer.json, check Packagist for the latest compatible stable version. The rootcomposer.lockis intentionally untracked; runcomposer updateafter adding or merging dependency changes, do not treat an outdated local lock as a repository defect, and never commit it. - Declare only real dependencies — Declare packages used directly by the code or needed for a supported installation. Do not add dependencies merely to complete the list. Add a polyfill only when neither the minimum PHP version nor an existing requirement provides the feature.
- Keep Composer metadata functional — Declare an
ext-*requirement only when the extension is not guaranteed by Hypervel's minimum PHP version. Add asuggestentry only when installing that package enables a concrete, documented feature; conditional interoperability, class-string references, tests, or metadata completeness do not qualify.
- Use one source of truth — Put all user documentation in
src/docs/. Package READMEs are intentionally minimal, not a second documentation surface, and must not duplicate user documentation. - Write user documentation in Laravel-docs prose — Use the simple, direct, human-friendly style of first-party Laravel documentation. Before drafting, read and match the surrounding prose; for new pages, use
src/docs/processes.mdas a style reference. Explain how to use the API in natural prose and examples. Only include implementation details when they affect how the reader must use it. Before finishing, remove repeated guidance, unnecessary caveats, and explanations already clear from the example. - Keep the Laravel porting guide current and focused — Whenever a framework change means existing Laravel application or package code, configuration, integrations, or data require adaptation or a compatibility check, update
src/docs/porting-from-laravel.mdin the same change. Before adding an entry, ask: “What existing Laravel usage would someone port incorrectly without this entry?” Purely additive, opt-in enhancements belong in feature documentation, not this guide. An enhancement that also changes compatibility qualifies because of that difference, not because it adds functionality. Do not document bug fixes, internal implementation differences, performance work, incidental source drift, or narrow edge cases unless they change what a porter needs to do. For example, a signature change on a rarely overridden protected method usually needs only an entry in the package README’s Differences From Laravel section. Briefly identify the affected usage and required action or risk, then link to the relevant canonical documentation instead of repeating its detail. The guide is a high-signal starting context for humans and LLMs, not an exhaustive framework diff or feature catalogue. Treat its context size as a design constraint, and remove stale or duplicated guidance whenever editing it.
Use this order, omitting items that do not apply:
- Package header
- Documentation link, when the package has a meaningful user-facing documentation page (
Documentation: https://hypervel.org/docs/{documentation-slug}) Differences From Laravel, for deliberate differences only- Upstream link, when the package is a port or deliberately tracks an upstream package (
Ported from: https://github.com/{vendor}/{package})
Do not add a documentation link merely for completeness. Omit it when there is no meaningful user-facing documentation page for the package.
Do not add upstream links for inspiration or historical lineage. Omit them when the Hypervel package is maintained independently and is not expected to track upstream changes.
Differences From Laravel is only for deliberate, lasting public contract differences that developers must account for. Bug fixes, correctness or safety fixes, performance improvements, and internal implementation differences do not qualify merely because they make Hypervel behave differently from Laravel. Omit the section when none apply.
When porting upstream updates, keep the package’s differences section accurate for the code being examined: add missing public-contract differences, correct stale descriptions, and remove entries that no longer qualify. Briefly explain the affected usage, why it differs, and the Hypervel equivalent where applicable. Apply this equally to first-party and third-party upstreams; link to detailed documentation rather than repeating it.
Documented differences record current decisions, not permanent requirements. Preserve supported enhancements, but reassess limitations when upstream changes or a clean, performant implementation make them unnecessary; follow the existing approval rules for API changes.
Build complete, long-term solutions, not MVPs or local workarounds. A broad change is correct when the root cause is in shared code, but every added mechanism must solve a real problem.
- Do not add behavior to guard against unsupported API use. Before adding defensive code, show that normal supported use actually behaves incorrectly and causes meaningful harm. Behavior that is only possible or surprising when an API is used outside its contract is not a bug.
- Prefer the simplest existing Laravel or Hypervel API, PHP feature, or database constraint. Do not duplicate framework behavior with package-owned machinery.
- Do not add a new mechanism merely because it sounds robust, flexible, or potentially useful — for example, a registry, retry loop, configuration option, or extension point. It must solve a verified problem, meet a clear approved requirement, support a clearly likely need whose shape is understood, or remove greater complexity elsewhere.
- Do not add machinery to preserve invariants across deliberate Laravel-style escape hatches such as
withoutEvents(), quiet methods, raw builders, raw SQL, disabled middleware, or direct transport access unless the public contract explicitly promises that behavior. - For a suspected coroutine race, identify the shared state, which supported calls can overlap in the same worker, and what goes wrong when they do. A
parallel()test can demonstrate the failure. Before adding synchronization, establish how an application reaches that overlap. For other worker-lifetime concerns, identify what persists between operations and what harm that causes. - Fix related symptoms in the lowest shared layer that owns the behavior instead of adding separate defensive paths. The size of the resulting change is not a reason to patch around the defect.
- A capability may be worth adding before its first use when the need is clearly likely and its design is understood. Do not add generic flexibility for hypothetical consumers.
- Avoiding overengineering never justifies an incomplete fix, weaker correctness, missing security or isolation safeguards, or deferring a worthwhile improvement.
- New Hypervel-owned code and packages must be Laravel-style — Design new packages and public surfaces as if they were first-party Laravel packages ported to and enhanced for Hypervel. APIs, naming, class responsibilities, code patterns, and directory structure must be ergonomic, intuitive, and immediately familiar to Laravel developers, while internals remain coroutine-safe and optimized for Hypervel's long-lived Swoole runtime and high-performance requirements. Apply the requirements under Audit changes during modification and code review from initial design onward.
- Reserve
assert*methods for testing APIs — Use prefixes such asensure*orvalidate*for runtime guards. - Modern PHP 8.4+ with full typing — use constructor property promotion, readonly properties, enums, match expressions, named arguments, and attributes where they fit. Every file declares
strict_types=1; parameters, return types, properties, and class constants are natively typed wherever PHP and the inherited API permit (e.g.resourcecannot be represented as a native PHP type). PHP does not allow return types on__construct()or__destruct(). - Keep contracts complete — A contract should declare the documented public API that every conforming implementation must provide, so code that type-hints the contract can use it. Add missing methods when found, even when the omission is inherited from Laravel. Put optional driver capabilities on separate capability interfaces, such as LockProvider. Implementation-specific methods and internal helpers do not belong on contracts. Follow the existing approval rules for Laravel API changes.
- Contract signature dependencies are lazy — contract signatures may natively reference types from optional split packages without a reverse Composer dependency. Do not remove these types or add cyclic dependencies solely for split-package isolation.
- Dependency access in new packages — Inject framework service contracts through constructors or methods rather than using helpers, facades, or direct instantiation. Where injection is impractical, such as static contexts or framework-constructed objects, use
Container::getInstance()->make(...). Helpers are fine in route and config files. - Dependency access within existing packages — Follow the established style of surrounding classes with the same role, including when adding new classes to a ported package. Preserve upstream’s helper, facade and instantiation style where it keeps updates straightforward. A class being newly written is not, by itself, a reason to use a different style. Change dependency access only for a concrete correctness, lifetime, testability or maintainability benefit, and keep related code internally consistent without rewriting unrelated classes.
- Use imported short class names — applies to new and ported code, including PHPDoc annotations. Replace fully or partially qualified class references with imported short names; use aliases for naming collisions. Classes in the current namespace need no import. Keep fully qualified names where genuinely clearer, such as middleware arrays and similar config-style identifier lists.
- Group traits in
Concerns/— follow the package's existing convention if it already has aConcerns/orTraits/directory; never mix both in one package. New Hypervel-original packages always useConcerns/; a newly ported package keeps its upstream directory name. - Use Laravel observer conventions — place Eloquent observers in a top-level
Observers/directory. Register model-specific observers with#[ObservedBy(...)]; useobserve()only for dynamic registration or observers supplied automatically by a reusable concern. - Use attributed local scopes — define local Eloquent query scopes as protected methods marked with
#[Scope], rather than legacy publicscopeFoo()methods. Use separate scope classes only for genuine global scopes. - Use attributed Eloquent trait hooks — In new model traits, put
#[Boot]on a public or protected static method and#[Initialize]on a public or protected instance method, so the hook keeps working if the trait is renamed. Use theboot{Trait}orinitialize{Trait}name instead when models are expected to override or wrap the hook by name. - No class docblocks unless warranted — only add a class-level docblock if something unusual or complex needs explanation: purpose, architectural role, usage patterns. Never write one that inventories the class — trait lists, method summaries, "registers X, configures Y" — that duplicates the members' own docblocks and goes stale. Method docblocks (title only, Laravel-style, imperative mood: "Return", not "Returns") are always added, except on test methods, whether identified by a test name prefix or PHPUnit’s
#[Test]attribute. A body can accompany the title for complex methods that need further explanation. - Add comments only where they're genuinely useful (required Laravel-style method title docblocks and config section comment blocks still apply) — a short WHY for logic that isn't obvious from reading the code, the reason behind a bug fix, or logic that's coupled to code in other files and hard to understand in isolation. Avoid unnecessary code comments; don't comment what the code does, and don't annotate routine casts or type normalizations. When reconciling upstream changes, add missing explanations for non-obvious Hypervel behavior being retained, so future syncs can understand why it exists.
- Explain intentional differences from upstream — Where a future sync could misunderstand, revert, or overwrite an intentional difference, add a concise comment beside the relevant code explaining why it differs. Do not annotate obvious or mechanical porting changes.
- Don't make classes final by default — keep classes open; add
finalonly when it protects a real invariant or avoids a concrete framework/API problem, e.g. immutability, coroutine-safety, or a security guarantee. - Place methods logically, not at the end — group new methods with related ones (getters with getters, setters with setters). Two exceptions: preserve upstream order when merging ported code (see Porting rules), and
flushState()has its own placement rule (see Static state and test cleanup). - Only extract methods when justified — extract only when the logic is complex enough to benefit from a name, it's likely to be reused, or two or more methods call it. Don't extract a simple one-liner with a single caller.
- Never abbreviate variable names —
$attributesnot$attrs,$connectionnot$conn. - Enum cases use PascalCase by default —
case Pendingnotcase pending,case OauthTokennotcase OAUTH_TOKEN. Applies to both backed and unit enums. Exception: when->nameis used as an external identifier (cache keys, cookie names, filesystem disks, rate limiter names, timezone strings) or appears in serialized output (e.g.,toArray()returning'name' => $this->name), match the consuming system's convention (typically lowercase or snake_case). - Strict comparisons by default — Use
===and!==in newly written code unless the supported API requires loose comparison. When converting an upstream comparison, verify operand types ($value === 0.0for a float, not=== 0) and preserve its behavior, including coercion and object equality. Keep loose comparisons where their semantics are intentional and needed to preserve supported behavior; do not replace them solely for consistency. - Prefer union types over
mixedwhen all types are known —mixedis only for truly unconstrained values or cases that cannot be safely narrowed after control-flow analysis. - Type decisions must be evidence-based — check corresponding Laravel/Hyperf signatures and docblocks as a reference, then trace the real control flow through method bodies across all callers and callees to confirm the types are correct.
- Fail fast with framework and PHP exceptions — don't add guards, wrapping, or defensive checks unless they handle a reachable invalid case or materially improve the error, and never swallow exceptions. If code would fail anyway (e.g. null passed to a typed parameter), let it fail naturally instead of adding a check that throws a custom exception — the stack trace is enough to diagnose.
- Mark intentionally unsupported methods — when a public method must be retained but intentionally rejects every call, mark it with
#[Unsupported]fromHypervel\Support\Attributes, passing the supported alternative as its message when one exists. Exclude base defaults that supporting implementations override, extension hooks, deliberate control-flow throws, and conditional rejections. PHPStan reports calls that reach a marked method. - Use semantic column types in migrations —
uuid()/foreignUuid()/uuidMorphs()for UUIDs,ipAddress()/macAddress()for network addresses, andjsonb()for JSON the database queries, indexes or validates. Usetext()for opaque serialized values that are only read back whole. PostgreSQL gets the real types (jsonb,uuid,inet,macaddr); the other supported databases fall back automatically to compatible types. - No arbitrary string lengths — use
->string('name'), not->string('name', 100). Don't invent limits; specify a length only when the domain or protocol defines one — exact (UUID: 36, ULID: 26, sha-256 hex token: 64) or a defined maximum (IPv6 address: 45). - No database enums — use string columns plus PHP enums. Adding a value to a database enum requires a migration.
- Use
dateTimefor arbitrary dates — usedateTimeinstead oftimestampfor scheduled dates, expiry dates, historical dates, and similar arbitrary dates. Keeptimestampfor dates recorded when an action happens, such ascreated_at,updated_at, anddeleted_at. On MySQL and MariaDB,timestamponly supports 1970–2038. - Prefer
timestampovertimestampTzin migrations — store times in UTC with plaintimestampcolumns, matching the normal convention in Laravel and Hypervel first-party migrations. ReservetimestampTzfor columns that genuinely need database-level timezone semantics, such as existing timezone-aware schemas or columns written under different session timezones. The schema API supports both — this is a column-choice convention, not an API restriction. - Guard optional event dispatches with
hasListeners()— before constructing and dispatching framework events, guard them withhasListeners()so hot paths skip event overhead when nobody is listening. Bare*listeners are passive observers and do not count; targeted wildcards do. Do not guard dispatches where dispatching is the side effect, such as jobs, broadcasts, webhooks, or command bus calls. - Use
Sleep::usleep()/Sleep::sleep()for delays in source code —Sleepis fakeable in tests. Use rawsleep()/usleep()only where real time must pass, such as test harnesses and external-process polling. - Leave native function imports to php-cs-fixer —
composer lint:fixaddsuse functionimports for the native functions inbin/native-functions/functions.php, so PHP resolves them at compile time while call sites stay identical to upstream code. Write native calls unqualified and do not add or remove these imports by hand. Never import or qualify a function listed inbin/native-functions/excluded.phpin the namespace that replaces it; that bypasses the namespaced function or test replacement the exclusion exists for.composer check:native-functionsreports such calls. - Use
xxh128for internal non-cryptographic hashing — cache and context keys, content checksums, and change detection. It is faster thansha256, which is reserved for trust boundaries: stored credential digests, signatures, and anything an attacker gains by forging. Seed it when the hashed value comes from user input, asSwooleStoredoes for its physical table keys. - Use immutable dates — For application-configurable dates, use
Date,now()ortoday(). They createCarbon\CarbonImmutableinstances. UseHypervel\Support\CarbonImmutablefor framework-owned dates. Keep the result when changing the date, time or timezone. AcceptCarbonInterfacefor Carbon inputs andDateTimeInterfacefor native dates. UsetoMutable()when an integration requires a mutable date. - Always use American English spelling — E.g., "behavior" vs "behaviour", "utilize" vs "utilise".
These rules apply to all code, including ported code.
- Laravel-style config files — Group related settings under Laravel's standard section comment blocks, with concise, user-facing explanations.
- Choose the getter from the setting's supported values. Use the matching nullable getter for one type plus null. Reserve
get()for unions no getter covers, arbitrary values, or diagnostics that inspect malformed input. Omit the getter's default for a required key and supply an explicit default (including null) when omission is allowed. Add a test for supported null behavior. - Cast environment-backed booleans and numbers in config files; if
nullis supported, cast only non-null values. Consumers must not repeat those casts. When a factory accepts raw configuration records, normalize types and documented optional defaults once at that boundary; never supply missing required members. - Required settings live in shipped config and must not have a code-level fallback, so missing or misspelled keys fail loudly.
- Intentionally optional settings keep one owning fallback and remain discoverable in config or feature documentation. Document what omission or null means.
- Framework and package defaults are shallow-merged with application config.
mergeableOptions()is only for named groups such as connections or stores: application entries replace matching defaults, while other default entries remain. Other nested arrays are replaced as a whole. - Replaceable named and nested records must apply documented optional defaults at their owning boundary; missing required members must still fail.
- A non-obvious fallback value shared by multiple source paths must have an owning constant. If user-facing config exposes the same default, keep its concrete value readable and add a focused test asserting that it matches the constant. Do not create constants for obvious source fallbacks such as
true,false,null,[], or'default'. - Env var naming — Ported config keeps upstream names. New Hypervel-specific settings should use the established prefix for the package or subsystem that owns the value (
SERVER_,CACHE_,REDIS_, etc.). Determine ownership semantically, not from the config filename: aggregate files such asapp.phpcontain multiple domains, andAPP_is for genuinely application-wide settings. If a value mirrors another config key, reuse that key's environment variable instead of defining a duplicate. - Use
resolve...Usingfor Hypervel-owned config resolvers — prefer this naming for callbacks that resolve config-derived values, unless an established Laravel domain convention already exists, such asredirectUsing().
Hypervel's container keeps Laravel's named API surface — bind(), singleton(), scoped(), instance(), aliases, contextual bindings — with resolution adapted for long-lived Swoole workers. Container ArrayAccess and dynamic service properties are intentionally unsupported. make() and get() resolve identically; get() is the PSR-compliant exception wrapper. Container::getInstance() auto-creates via ??= new static(), so it always returns a container.
The critical difference: unbound concrete classes are auto-singletoned. In Laravel, make() on a class with no binding builds a fresh instance every call. In Hypervel, the first resolution caches the instance (in $autoSingletons) for the worker lifetime. Worker-safe services can reuse initialized state, avoiding repeated construction cost and allocation. Explicit bindings override this (bound classes follow their binding type), and SelfBuilding and Transient classes are excluded.
| Registration | Laravel | Hypervel |
|---|---|---|
| Unbound concrete class | Fresh instance every make() |
Auto-singletoned on first make(); cached for the worker lifetime |
singleton() / #[Singleton] |
Cached in $instances |
Same — but the worker serves many requests, so the instance is shared across all of them |
scoped() / #[Scoped] |
A worker-lifetime singleton the runtime flushes between requests (forgetScopedInstances()) |
Cached per coroutine via CoroutineContext — isolated between the concurrent requests in one worker |
bind() |
Fresh instance every make() |
Same |
make($abstract, $params) / makeWith() |
Contextual build — never cached | Same — parameters bypass the singleton, scoped, and auto-singleton caches |
build($concrete) |
Constructs the concrete directly, bypassing bindings and caches for the top-level class | Same; Hypervel adds buildWith($concrete, $params). Nested constructor dependencies still resolve through the container |
implements SelfBuilding |
Container calls the class's static newInstance() |
Same, and it also skips auto-singletoning |
implements Transient |
No equivalent marker | Unbound resolutions are always fresh; explicit bindings still determine the lifetime. Eloquent models inherit this marker from Model |
Rules that follow from this:
- Ownership of mutable state decides the lifetime. Most services, middleware, listeners, factories, and formatters are safe to auto-singleton because they are stateless or hold only worker-owned state. State a service computes and keeps for reuse — caches, registries, resolved configuration, callbacks, or connections — is intended worker-lifetime state, so those classes remain auto-singletons. State owned by a caller, request, or operation must not be stored on a worker-shared object: keep invocation state in
CoroutineContext, usescoped()for one instance per coroutine/request,bind()for a fresh bound instance, orTransientonly when freshness is intrinsic to the whole hierarchy. - A class that captures request state in its constructor and is neither scoped nor execution-scoped is a coroutine-safety bug — reach consensus on the fix through a second-opinion loop with your peer agent before implementing. Contextual attributes such as
RouteParameterandCurrentUseralready make their containing class execution-scoped. - Do not infer
Transientfrom mutable properties, setters, or a no-argument constructor. Remove incidental mutation instead of marking the class asTransient, and verify every subclass before marking a base class. For example,Collectionholds caller-owned values and isTransient, whileManager::$driversis a service-owned cache and remains auto-singletoned.
- Unbound concrete service whose state is safe to share for the worker lifetime → do not bind it; auto-singletoning is the default.
- Interface or canonical service key shared for the worker lifetime →
singleton(). - Existing object intentionally shared for the worker lifetime →
instance()during boot, or in tests for a swap. - Unbound class whose constructor uses an
ExecutionScopedAttribute→ do not bind it; the container scopes it automatically to the current execution. - One instance per coroutine / request →
scoped(). - Fresh instance required by a specific binding →
bind(). - Freshness intrinsic to the whole class hierarchy →
Transient. - One resolution should ignore the implicit auto-singleton and constructor-derived execution scope while retaining aliases, bindings, extenders, and resolving callbacks →
makeTransient(). - Class-controlled construction through
newInstance()→SelfBuilding. - One contextual resolution with explicit constructor parameters →
make()/makeWith()with parameters. Aliases, binding definitions, and resolving callbacks still apply, but cached lifetimes are bypassed. - Direct construction that intentionally bypasses top-level bindings and caches →
build()/buildWith(). Do not use these when aliases, test swaps, or resolving callbacks must be honored.
1. Canonical string key — use a closure with new:
// The concrete (AuthManager) is listed as an alias for 'auth', so
// singleton('auth', AuthManager::class) would create a circular resolution cycle:
// 'auth' -> build AuthManager -> getAlias(AuthManager) -> 'auth' -> infinite loop
$this->app->singleton('auth', fn ($app) => new AuthManager($app));2. Abstract is not in the alias table — use string concrete:
// Neither FormatterInterface nor DefaultFormatter are aliases for anything,
// so the container can resolve this directly without cycles.
$this->app->singleton(FormatterInterface::class, DefaultFormatter::class);3. Abstract and concrete are the same class — do not bind merely to share it. Unbound concrete classes without a different declared lifetime are auto-singletons. Explicit registration can change lifetimes, bound(), optional constructor injection and rebinding; retain it when those effects are intended.
Before binding core framework services, check Application::registerCoreContainerAliases().
If the abstract or concrete participates in the alias table, choose the binding key carefully. bind() and singleton() store bindings under the exact abstract key passed; resolve() resolves aliases before lookup. Binding an alias removes its mapping to the canonical key; the two keys can then resolve differently.
When adding a core alias, use the string key as the canonical abstract:
// Wrong: contract is canonical.
\Hypervel\Contracts\Auth\Factory::class => [
'auth',
\Hypervel\Auth\AuthManager::class,
],
// Correct: string key is canonical.
'auth' => [
\Hypervel\Auth\AuthManager::class,
\Hypervel\Contracts\Auth\Factory::class,
],For canonical string keys such as 'auth', 'cache', 'db', 'request', and similar framework services, prefer closure bindings:
$this->app->singleton('auth', fn ($app) => new AuthManager($app));Facades for these services should resolve the canonical container key instead of the concrete manager class.
Do not add new core aliases without need. Only add an alias when Hypervel needs it as part of its public container surface.
When adding a package provider, register the provider through the package Composer metadata and the root Composer metadata, as described in the package skeleton workflow under Porting Packages. Add a provider to DefaultProviders only if every Hypervel application needs it at framework startup, such as auth, cache, database, session, validation, view, or low-level Swoole infrastructure. Optional packages such as Reverb, Scout, Telescope, Sentry, and Watcher should rely on package discovery instead.
Before changing registerBaseServiceProviders(), reach consensus with your peer agent through a second-opinion loop that the service is needed during bootstrap, before normal providers are registered. Reserve this method for framework infrastructure needed by the boot process itself.
It is safe to have the same provider listed in both registerBaseServiceProviders() and extra.hypervel.providers when early loading is genuinely needed. Application::register() deduplicates providers by class name, and the discovery entry ensures standalone installs still load the provider.
For callbacks and resolvers registered during provider boot, inject worker-safe dependencies into boot() and capture them in the closure. Do not repeatedly resolve them from the container when the callback runs. Event listeners are the exception described below: resolve listeners at event time so application and test rebindings are honored.
Register listeners in the service provider's boot() method using closures that resolve the listener from the container:
public function boot(): void
{
$events = $this->app->make('events');
$events->listen(AfterWorkerStart::class, function (AfterWorkerStart $event) {
$this->app->make(AfterWorkerStartListener::class)->handle($event);
});
}Resolve from the container ($this->app->make(...)) rather than injecting or instantiating directly — this ensures constructor dependencies are resolved and the listener benefits from auto-singleton caching.
Decide where state lives before writing code:
| State | Storage |
|---|---|
| Immutable metadata shared by all requests | Static property cache or worker-lifetime singleton |
| Stateless service shared by all requests | singleton() or auto-singleton (see Container) |
| Mutable state for one request, operation, or coroutine | CoroutineContext or a scoped() binding |
| Fresh mutable object per resolution | bind(), contextual parameters, or Transient for an intrinsically fresh class hierarchy |
-
Use
Hypervel\Context\CoroutineContextfor invocation-scoped state — anything that must not be visible to other concurrent coroutines in the same worker. Static properties and singleton fields leak across coroutines: whatever one coroutine sets becomes visible to all others in the worker. Use the established key-naming convention:__<package>.<key>value prefix,_CONTEXT_KEY/_CONTEXT_KEY_PREFIXconstant suffixes, public only when other classes or tests reference the constant. Do not useHypervel\Support\Facades\Contextas the low-level coroutine store; it provides Laravel-style application context instead. -
Group related coroutine state in
…Stateclasses when useful. Store the object inCoroutineContextso callers can retrieve it once and read or update several properties without repeated context lookups or array copying. Use named properties for known fields and arrays for keyed collections. When copied child coroutines need independent state, implementReplicableContextwith the appropriate copying behavior. SeeInertiaStateandContainerResolutionState. -
Configure process-global values only during worker boot — config is a process-global singleton, so
Config::set()during request handling changes behavior for every concurrent request in the worker. Never mutate config for request-specific behavior; useCoroutineContextor middleware instead. Provider boot-time configuration is fine — it runs once per worker. -
Create Swoole tables from a
BeforeServerStartlistener, never inregister()—register()runs in every process, including ones that never use the table. Managers that create tables by name alsoseal()there. Swoole frees table memory only ondestroy(), which only the creating process may call — never from a destructor, since forked workers run those too. -
Name static cache properties for what they store — not with a
Cachesuffix; static properties in Swoole workers are caches by nature. Exception: matching an existing Laravel-ported pattern in the same class (e.g.$classCastCache,$attributeCastCacheonHasAttributes). -
Bound worker-lifetime lookup caches — any internal lookup cache retained across requests in a worker—for example, in static properties or singleton instances—must have a naturally limited set of keys or discard entries that are safe to recompute. Do not add a size limit merely to hide growth from request- or user-derived keys. This governs framework-derived caches, not application-owned cache stores such as the
worker-arraydriver. -
Review worker-lifetime state explicitly — before introducing or modifying static properties/caches, singletons or other long-lived state, assess the Swoole persistence impact (memory leaks, cross-request behavior) and reach consensus on the design through a second-opinion loop with your peer agent. Routine boot- or test-only static config or callback slots marked with
#[BootOnly],#[TestsOnly], or#[BootOrTestsOnly]and reset influshState()do not need a second opinion. -
Mark restricted worker-state mutators — when adding or changing a public mutator intended only for startup or tests, use an attribute from
Hypervel\Support\Attributes:#[BootOnly]— startup configuration#[TestsOnly]— test fakes, swaps, and overrides#[BootOrTestsOnly]— configuration or cleanup allowed in either context
PHPStan checks marked calls in recognized runtime contexts. Use
whenNotDefault: 'parameter'only when the restriction applies to values differing from that parameter's declared default,whenProvided: 'parameter'when it applies whenever the argument is passed, andwhenShared: truewhen it applies only to a worker-shared receiver. Keep a concise docblock explaining the runtime harm. If an API is intended for runtime use, fix unsafe shared-state behavior through a peer second opinion rather than restricting it to boot or tests. -
Assess static caching opportunities — if a path repeatedly computes expensive stable metadata and worker-lifetime static caching would be a clear win, reach consensus with your peer agent through a second-opinion loop on what to cache, the expected benefit, bounded memory use, and correctness across requests before implementing.
Classes that use static caching need a flushState() method for test cleanup — see "Static state and test cleanup" under Writing Tests.
- Release idle pooled connections before unrelated long waits — Holding connections while waiting for external services or child coroutines can exhaust pools and limit concurrency. Release them at the appropriate shared boundary, preserving active operations and session ownership; consider reacquisition costs rather than adding release calls indiscriminately. For database sessions, reuse
ConnectionResolver::releaseIdleConnections(), which preserves transactions and explicit pins. SeeMailer::sendSymfonyMessage()andConcurrentImportRunner::wait()for examples. - Before returning a cURL handle to a pool, clear request callbacks installed by your code or its SDK. Set their options to
nullin onecurl_setopt_array()call beforecurl_reset(), which can retain the objects those callbacks capture.
See Coroutine Cancellation for handling patterns and examples.
- Treat coroutine cancellation separately from errors — Where cancellation needs different handling and the operation runs callbacks or listeners—including
releaseIdleConnections()—useCoroutine::causedByCancellation($exception). Cleanup errors can carry cancellation in their exception chain. Preserve the original exception, perform owned cleanup, and propagate cancellation instead of retrying or continuing normal work. Report genuine cleanup errors without swallowing cancellation. Add this only where cancellation can actually happen and mishandling it causes a real bug. Keep plain checks for native-only operations. - Preserve cancellation ownership — If shared work can safely be retried, allow another waiter to retry after its owner is canceled. Otherwise, share the failure behind
ForeignCancellationException, orChildCancellationExceptionfor an independently canceled child, preserving the original cause. Store the shared result or failure before waking waiters, including failures that later callers read; canceled work must never look like a successful empty result. The canceled owner propagates its own exception unchanged; waiters must not mistake it for their own cancellation. - Contain cancellation in direct engine coroutines — Callbacks passed directly to
Engine\Coroutinemust catch cancellation after cleanup so it cannot terminate the worker. The higher-levelCoroutineclass already provides this protection. - Test cancellation through cleanup — When adding or changing cancellation-sensitive retry, child-management or shared-result handling, extend relevant regression tests with throwing cleanup during cancellation. Verify the resulting behavior, not just the exception type.
For bug fixes, follow step 4 under Framework bug fixes. Ask the owner only for unapproved public API departures or capability removals; changes to migrations or schemas that applications run, or to how existing data is stored; meaningful performance regressions or weakened guarantees; consequential tradeoffs the requirements do not settle; unresolved peer disagreements; or actions requiring owner authorization.
- Investigate anything unusual — missing dependencies, logic needing special consideration, things that don't make sense for Hypervel, etc. Investigate the cause and impact before acting. Do not dismiss concerns as theoretical without investigation. If no supported, realistic path or meaningful harm is found, do not present it as a defect or add machinery to address it.
- Do not defer work without owner authorization — do not remove functionality or substitute stubs or TODOs for implementation unless the owner authorizes it. Adding an item to
docs/todo.mdshould be rare and requires explicit owner approval for that item. Preserve existing TODO comments unless their underlying work is verified complete or the owner directs otherwise. If blocked, reach consensus on a complete solution through a second-opinion loop with your peer agent. - Investigate source code bugs — trace the root cause of bugs found in Hypervel or ported upstream code, including bugs exposed by PHPStan or tests. Upstream bugs must be fixed, not ported as-is.
- Trace upstream differences before calling them bugs — a difference from Laravel or an upstream package is not proof that Hypervel is wrong, and matching upstream is not proof that Hypervel is correct. A verified Hypervel defect remains a defect when upstream has the same problem.
- Do not work around incorrect existing code to avoid churn — if work exposes incorrect types, wrong logic, missing methods/classes, or other real defects in existing Hypervel code, fix the underlying code instead of adding compatibility hacks or local workarounds to sidestep the problem. Prioritize correctness and code quality over keeping the change small.
- Never weaken or drop tests to work around source issues — never delete, skip, loosen assertions, or alter tests to make them pass against flawed source code. The test is the spec; the source gets fixed. For type errors specifically, "When tests expose source code type errors" under Writing Tests covers how to identify the correct type.
- Never dismiss issues as "out of scope" or "pre-existing" — when any issue surfaces (bugs, unexplained divergences or API gaps, incorrect visibility, type inconsistencies, naming mismatches, etc.), investigate it and fix any confirmed defect. Never use phrases like "out of scope", "pre-existing", "not part of this work", "separate concern", or "unrelated" to justify ignoring an issue. Only the owner may authorize leaving a confirmed issue unresolved.
Worker-lifetime state has additional second-opinion requirements — static/singleton state changes, unsafe public mutators, static caching opportunities, and per-request state captured by auto-singletons — listed under Container and Coroutine and Worker-Lifetime State.
- Investigate tests broken by your changes before updating them. When an implementation change causes a previously passing behavioral test to fail, treat the test as evidence of a contract or missed edge case. Trace what it protects through the tested code, callers, upstream source/tests, and relevant history. Before changing the test, reach consensus with your peer agent through a second-opinion loop that the behavior change is correct, including its compatibility and edge-case consequences.
- Easy fixes (namespace typos, missing return types, a missed namespace update, etc.) — fix and continue.
- Non-trivial failures (behavioral changes, test logic issues, unclear root causes) — trace the failure to its cause before changing the code or test. Investigate all failures thoroughly — don't assume a failure is caused by your change without confirming it.
- Do not skip or remove tests except under the rules below. Never comment out, skip, or avoid porting a test because the required functionality is missing. If Hypervel should support it, follow Framework bug fixes or Framework enhancements. If support is unclear, reach agreement with your peer first. Keep every applicable upstream test, even if redundant or seemingly unnecessary; exclusions require justified Hypervel architectural or API differences. Remove tests for the approved unsupported features listed under Porting Laravel Tests without asking. For any other exclusion or removal, reach consensus through a second-opinion loop on what the test covers and why removal is justified.
These rules apply to all tests — new tests for framework work and ported tests alike.
Test supported public behavior, meaningful branches, verified regressions, and realistic coroutine or worker-lifetime failures. Do not add production APIs, branches, or defensive machinery solely to make speculative states testable. Do not require invariants to survive deliberate framework escape hatches unless the public contract promises that behavior.
In any test, PHPUnit assertion failures, skips, and incomplete markers extend AssertionFailedError, which extends RuntimeException; a catch must not swallow one and let the test pass. An exception test must fail unless its intended behavior and exception path occurred. Before rewriting an existing or ported exception test, demonstrate a concrete violation; a broad catch alone is not a defect. When writing a new test that controls the exception, prefer prebuilding it and asserting its identity after the catch.
Laravel-ported tests follow upstream placement; the rules below apply to Hypervel-only test files. All tests live in tests/{PackageName}/ (PascalCase). Tests that require external services go in tests/Integration/{PackageName}/ — see Integration tests below. When only some integration tests for a package require one service, group them in tests/Integration/{PackageName}/{ServiceName}/. When every integration test for the package requires that service, keep them directly in the package directory.
Use these paths for service-specific test directories under tests/Integration/{PackageName}/. CI discovers them automatically.
| Path | Runs on |
|---|---|
Database/*Test.php |
MySQL, MariaDB, PostgreSQL, and SQLite |
Database/{MySql|MariaDb|Postgres|Sqlite}/ |
The matching database |
Redis/ |
Redis, Valkey, and Redis Cluster |
Put a test class shared by multiple databases directly in Database/. Use #[RequiresDatabase(...)] on the class or individual methods when only some drivers apply. Use a driver directory for a class specific to one database.
Never extend PHPUnit\Framework\TestCase directly. Always use one of these:
| Class | Use When |
|---|---|
Hypervel\Tests\TestCase |
Unit tests, mocks only, no container needed. Tests that only need an isolated scratch directory stay here — use ParallelTesting::tempDir() (see Temp directories below). |
Hypervel\Testbench\TestCase |
Integration tests (needs container for facades, config, DB, etc.) or any test that needs a full app skeleton / writes through BASE_PATH — testbench clones a disposable runtime skeleton per run and exposes its path via BASE_PATH (and TESTBENCH_BASE_PATH for subprocesses), deleted on shutdown. Committed source is never mutated. |
PHPStan\Testing\RuleTestCase |
PHPStan rule tests in tests/PHPStan. |
PHPStan\Testing\TypeInferenceTestCase |
PHPStan type-inference tests in tests/PHPStan. |
PHPStan\Testing\PHPStanTestCase |
PHPStan tests in tests/PHPStan that need PHPStan's service container. |
Always call parent::setUp() in your setUp method.
Put application environment configuration in defineEnvironment() or a #[DefineEnvironment] method. Do not mutate application config in setUp(), because providers or resolved services may already have consumed it.
Within test methods, use config() directly to read or mutate configuration values. Do not use no-argument config() as a dependency source; resolve the Repository contract from the container when constructing a test subject manually.
All standalone test support files — PHP classes, non-class PHP files, and non-PHP files (JSON, SQL, images, templates, etc.) — go in a single Fixtures/ directory (capital F). This matches Laravel's predominant convention. PHP classes in Fixtures/ are PSR-4 autoloaded like any other test file.
Put independently reusable fixture classes and enums in separate PSR-4 files. Keep single-test helpers inline; never share inline helpers between test files. Keep Integration-only fixtures under Integration, and share fixtures across unit and Integration tests only when it makes the tests clearer. Preserve combined files when their structure is itself part of what is being tested.
Tests often define helper classes (models, stubs) with generic names like User, Post, or Comment. When multiple test files use the same namespace and define classes with the same name, PHP throws "Cannot redeclare class" errors.
Use test-specific namespaces only for collision-prone helper classes (matching Laravel's pattern):
// WRONG - shared namespace causes conflicts for generic helper names
namespace Hypervel\Tests\Integration\Database;
class EloquentDeleteTest extends DatabaseTestCase { ... }
class Comment extends Model {} // Conflicts with Comment in other files!
// CORRECT - test-specific namespace isolates generic helper names
namespace Hypervel\Tests\Integration\Database\EloquentDeleteTest;
class EloquentDeleteTest extends DatabaseTestCase { ... }
class Comment extends Model {} // No conflict - different namespaceUse this when helper classes have generic names likely to appear in other test files. Do not add extra namespaces for helper classes whose names already include the tested feature or package context, such as FailingHorizonInstallCommand or MissingProviderTelescopeInstallCommand.
When a test-specific namespace is needed, the namespace includes the test class name as the final segment. This means:
- Each affected test file has its own namespace
- Generic helper classes can use simple names (
Comment,Post,User) - No
$tableproperties needed (Eloquent derivescommentsfromComment) - No explicit foreign keys needed (Eloquent derives
user_idfromUser)
PHPUnit loads test files directly (not via autoloading), so the namespace doesn't need to match the directory structure.
Tests that write files to disk must never write to the committed tests/ directory. For tests needing a full app skeleton, Testbench\TestCase handles this automatically (see testbench entry in the paths table above). For any test that only needs an isolated scratch directory, use ParallelTesting::tempDir('TestName') — store it as a property, delete any leftover copy and create it fresh in setUp, then delete it again via Filesystem::deleteDirectory() in tearDown. Use sys_get_temp_dir() directly only when the system temporary path itself is the behavior under test. See FoundationViteTest or OptionTest for the pattern.
The Testbench skeleton clone is shared for the whole worker, so tests that write under BASE_PATH must restore or delete the exact files they touch in tearDown(). For .env files, prefer useEnvironmentPath() with an isolated ParallelTesting::tempDir() directory.
Tests using Hypervel's base classes run inside coroutines by default. The RunTestsInCoroutine trait is on both base test cases (Hypervel\Tests\TestCase and Hypervel\Foundation\Testing\TestCase / Testbench), so each test method automatically runs in a fresh coroutine. Context is destroyed when the coroutine ends — no manual cleanup needed.
Never add use RunTestsInCoroutine; to individual test classes. It's inherited from the base class. If you encounter a test extending raw PHPUnit\Framework\TestCase, change it to extend Hypervel\Tests\TestCase instead.
Opting out of coroutines: Set protected bool $runTestsInCoroutine = false; on the test class. This is needed when:
- Tests call
run()directly to create their own coroutines (e.g., pool management tests, parallel HTTP tests) - Tests explicitly verify non-coroutine → coroutine transitions
PHPUnit constraint: setUp() and tearDown() run outside the test method's coroutine (PHPUnit 13's runBare() is final). For DB operations in setUp/tearDown, Foundation TestCase provides runInCoroutine() which creates temporary coroutines and bridges transaction state via preserveTransactionContext().
Optional hooks for code that must run inside the test's coroutine:
setUpInCoroutine()— runs inside the coroutine before the test methodtearDownInCoroutine()— runs inside the coroutine after the test method
These are primarily useful for DB operations or external service setup that needs coroutine context. Most ported Laravel tests won't need them.
To prove state is per-coroutine (not shared on a worker-lifetime singleton), spawn concurrent coroutines via parallel() from Hypervel\Coroutine and usleep() between mutation and read — the sleep forces the runtime to interleave them; without it tasks may complete sequentially and the leak won't reproduce.
use function Hypervel\Coroutine\parallel;
[$a, $b] = parallel([
function () use ($service) { $service->set('A'); usleep(5000); return $service->get(); },
function () use ($service) { $service->set('B'); usleep(5000); return $service->get(); },
]);Examples: tests/Inertia/CoroutineIsolationTest.php, tests/Container/CoroutineSafetyTest.php. Name new tests CoroutineIsolationTest / CoroutineSafetyTest for discoverability.
request() resolves from RequestContext — when no request exists in context (tests that don't make HTTP requests), each request() call creates a throwaway fallback instance. This means request()->merge() has no effect on subsequent request() calls. Replace request()->merge(['key' => 'value']) with RequestContext::set(Request::create('/?key=value')) to seed a stable request in context.
Seed application requests with RequestContext::set(); replacing the 'request' binding with instance() bypasses coroutine-local behavior.
AfterEachTestSubscriber handles framework-global cleanup between tests. It calls flushState() on framework classes that hold static state, and resets the container itself — Container::flushState() + setInstance(null) — plus CoroutineContext::flush(), with each test in a fresh coroutine. So container singleton/auto-singleton instance state and coroutine context don't leak between tests; only static/process-global state and live external resources need package cleanup, not mutable state on a container-cached instance. Do not duplicate framework-static resets in tearDown(); AfterEachTestSubscriber is their one authoritative registry. Tests still own resources they create, such as child coroutines, subscribers, processes, sockets, and temporary files, and must close or join them through exception-safe cleanup.
Tests that create a Swoole\Table pass it to trackSwooleTable() from InteractsWithSwooleTables; otherwise its memory stays allocated until the PHPUnit worker exits.
When writing or porting source classes that use static properties for caching (e.g., $booted, $globalScopes, resolved config values, compiled formats):
- Add a
public static function flushState(): voidmethod that resets the static properties to their initial values - Check whether the subscriber (
src/testing/src/PHPUnit/AfterEachTestSubscriber.php) should call it — if the cached state could leak between tests and cause failures, add the call
Framework-owned classes go in AfterEachTestSubscriber. Use callIfLoaded() when changing the state requires loading the listed class. If a wrapper resets another class that can be used independently, cover that path too. For example, when Avatar is loaded and Telescope is not, reset Avatar directly.
If the state can change while the listed class is unloaded, run the reset even when the test never loaded that class. For example, DumpWatcher resets VarDumper's global handler, which other code can set. If the reset method belongs to an optional package, use callIfExists() so cleanup runs whenever that package is installed. Keep first-party optional package resets grouped at the bottom of the subscriber.
Third-party packages, private packages, and applications should register cleanup for process-local state that survives application teardown through extra.hypervel.test-state and a TestState registrar instead of hardcoding their classes into the framework subscriber. These callbacks run after the test application is destroyed, so they must not resolve container services; external resources remain owned by their test traits.
Do not add Hypervel\Testing\PHPUnit\AfterEachTestCleanup itself to AfterEachTestSubscriber. Its callbacks are suite-level registrations that must persist for the PHPUnit worker lifetime.
Place flushState() at the end of the class. The only exception is when the class has trailing magic dispatch/lifecycle methods (__call, __callStatic, __get, __set, __isset, __unset, __destruct) at the end; in that case, place flushState() immediately before that trailing magic-method block. __invoke() is not a placement anchor.
Use the standard title docblock for flushState() methods:
/**
* Flush all static state.
*/Leave flushState() unmarked; PHPStan recognizes this cleanup hook by name.
Keep the docblock to the title only — no extra paragraphs. If the method body has a non-obvious WHY worth explaining (ordering constraints, late-static-binding subtleties, etc.), put it as an inline comment above the relevant line inside the method, not as an extra paragraph under the title.
When the property's initial value and flushState()'s reset value share a literal (a number, string, class name, etc.), extract it to a DEFAULT_* class constant and reference it from both sides — this prevents drift if the default ever changes. Make the constant public only if tests or external callers reference it; otherwise protected. Nullable lazy caches and callback slots are exempt: null there is the structural "not yet computed" sentinel required by the ??= pattern, not a configurable default — initialize and reset with a literal null, no constant.
public const DEFAULT_TRUNCATE_AT = 120;
public static false|int $truncateAt = self::DEFAULT_TRUNCATE_AT;
public static function flushState(): void
{
static::$truncateAt = self::DEFAULT_TRUNCATE_AT;
}Do not create per-package abstract test case classes (e.g., EngineTestCase, CoroutineTestCase) just for coroutine support — it's already on the base class.
A per-package base class is only justified when there is shared setUp logic — e.g., shared container mock setup, shared helpers, or shared test fixtures that multiple test classes in the package need.
Use Double for test doubles in new tests. When porting, convert direct Mockery mocks and spies to Double regardless of the upstream library; keep upstream PHPUnit doubles. Retain Mockery where required to use or test framework APIs backed by it, such as $this->mock(), facade expectations and Sanctum::actingAs(). Add JMac\Testing\Integrations\PHPUnit\VerifiesDoubles to each test class using Double; it owns verification and cleanup.
Double is lenient by default. Use expects('method')->never() to forbid a call, or unused() after exercising the code to assert no calls occurred. allows()->with(...) neither requires a matching call nor rejects other arguments. expects()->with(...) requires the call and rejects unmatched arguments for that method. Use strict() unless the test deliberately ignores unrelated calls; this applies equally to tests ported from upstream. For an intentionally lenient double, use @phpstan-ignore hypervel.implicitDoubleMode (reason) for the factory call.
with() compares objects, including doubles, using ==. Use Argument::same($object) when the exact instance matters or to preserve Mockery’s default identity check.
Keep matcher predicates free of side effects, including assertions. Use Argument::capture() to save an argument or resolves() to record a matched call.
Do not overlap allows() stubs with expects() constraints when enforcing maximum call counts or never(); a matching stub can absorb those calls regardless of registration order.
Where Mockery is required by a framework integration, import it as m.
Framework base test cases own Mockery verification in tearDown() so unmet
expectations are attributed to the test that created them. The global
AfterEachTestSubscriber remains a fallback for tests using another base case
and always resets framework state even when Mockery verification fails. Tests
must not add their own Mockery::close() calls.
Prefer an injected collaborator or a protected method that a test can override. When a test must replace a native function by declaring it in the source namespace, such as in a child-process fixture, run composer sync:native-functions to exclude the name from imports, then remove each use function import or leading backslash for that name in the replacement's namespace. The sync reports each one as a bypassed replacement, because composer lint:fix never removes them; until they are gone, the replacement never runs. The exclusion applies to every call of that function in the repository, so never add one for a function the compiler optimizes, such as count(), strlen() or function_exists(). See bin/native-functions/README.md.
Hypervel uses stricter types than Laravel. Laravel-trained habits produce incomplete test mocks that loose typing silently accepts — Hypervel's types reject them. This applies to new tests and ported tests alike.
Model properties require type declarations:
// Laravel
protected $table = 'users';
protected $fillable = ['name'];
public $timestamps = false;
// Hypervel
protected ?string $table = 'users';
protected array $fillable = ['name'];
public bool $timestamps = false;Mock return types must match:
// Use the native type required by the consumer.
$connection = Double::for(PDO::class);
$query = Double::for(QueryBuilder::class);Fluent return values:
Double's type-based defaults return the double itself for non-nullable self, static, or declaring-class return types, and null for nullable types. These defaults apply to configured calls without an explicit return and unconfigured calls in lenient mode; unconfigured passthru calls run the real method.
Mocking methods with static return type:
Methods like newInstance() have static return type, meaning they must return the same class (or subclass) as the object they're called on. Doubles are generated subclasses, so returning the parent class fails:
// Returning the parent class violates the generated subclass's static return type.
$this->related = Double::for(MyModel::class);
$this->related->expects('newInstance')->returns(new MyModel);
// Initialize real model behavior and intentionally return the double itself.
$this->related = Double::for(new MyModel)->passthru();
$this->related->expects('newInstance');
// Test attributes on the double itself (passthru keeps real Model behavior)
$result = $relation->getResults();
$this->assertSame('taylor', $result->username);When distinct instances are required:
If a test needs to verify distinct instances (e.g., makeMany() returns different objects), use a concrete test stub instead of mocks:
class EloquentHasManyRelatedStub extends Model
{
public static bool $saveCalled = false;
public function newInstance(mixed $attributes = [], mixed $exists = false): static
{
$instance = new static;
$instance->setRawAttributes((array) $attributes, true);
return $instance;
}
public function save(array $options = []): bool
{
static::$saveCalled = true;
return true;
}
}
// Test verifies real behavior, not mock expectations
$this->assertNotSame($instances[0], $instances[1]);
$this->assertFalse(EloquentHasManyRelatedStub::$saveCalled);If a test fails with a type error, the source code type may be wrong — not the test. Types should be correct, not just strict. A narrow type that doesn't cover all valid cases is incorrect.
How to identify:
- Test returns/passes a type that the source code should accept but doesn't
- The type is a parent class of what's currently declared (e.g.,
Support\CollectionvsEloquent\Collection)
How to fix:
- Identify all valid types the method can accept/return
- Use the common base type that covers all cases without being unnecessarily loose
- Fix the source code, not the test
Example: A method returns Eloquent\Collection normally, but an afterQuery callback can return Support\Collection. Since Eloquent\Collection extends Support\Collection, the correct return type is Support\Collection — it covers both cases precisely.
Wrong approach: Removing types, using mixed, or modifying tests to avoid the type check. These hide the real issue.
- Add
declare(strict_types=1);at the top of every file - Add
: voidreturn types to test methods. This keeps tests consistent with the repo's full-typing rule. - Use PHPUnit attributes instead of docblock annotations — prefer
#[DataProvider('...')],#[Depends('...')], etc. over their@dataProvider/@dependsdocblock equivalents. Do not add@internal/@coversNothingdocblocks or#[CoversNothing]/#[CoversClass(…)]attributes — Hyperf uses both forms but Laravel doesn't, and they serve no purpose outside strict coverage modes
Tests requiring separately configured services or public APIs go in tests/Integration/{PackageName}/. Self-contained loopback server fixtures may stay in tests/{PackageName}/ if they need no external setup and use automatically assigned ports, bounded waits, unconditional cleanup, and parallel isolation.
Optimize integration tests for parallel testing — ParaTest runs tests concurrently. Use RefreshDatabase by default and DatabaseTruncation when transaction depth zero is required; reserve DatabaseMigrations for migration behavior or a genuinely fresh migrated schema/baseline. Set up only the tables and services each test needs. Keep database cases in one data-provider test when splitting them would make schema setup compete. Load the full schema only when testing migrations or schema parity; do not hide avoidable slowness with longer timeouts.
For service suites outside the directory conventions above, register new test directories in the matching workflow or the runner script it calls. The service trait handles isolation and skips. CI discovers tests by their paths.
Integration tests that use an external service, directly or through the code under test, must use the trait listed for it under External Service Tests.
These traits are required for external-service tests to work under ParaTest. Parallel workers share external services unless the trait isolates them. Tests that bypass the trait will leak state across workers and fail depending on timing.
The traits handle service-specific setup and cleanup. For example, InteractsWithRedis assigns each ParaTest worker its own Redis database and flushes it before and after each test. Redis Cluster only supports database zero, so Cluster suites run serially and flush that database between tests. Both approaches isolate the test keyspace without changing the Redis behavior being tested.
If a service is not configured, the trait skips the test before connecting. If the service is configured but unreachable or misconfigured, the test fails.
If the service has no trait yet, create one following this pattern and add it to External Service Tests.
tests/Integration/ is not excluded from phpunit.xml.dist. The skip traits handle graceful skipping when service env vars are not configured. When services are explicitly enabled (CI or local with .env), the tests run normally.
Check .github/workflows/ to determine which workflow runs the affected integration tests.
When adding integration tests that need a new service, either add them to an existing workflow or create a new one. The workflow must start the service and set the appropriate env vars.
Add env vars for new integration tests to both:
.env.example— commented out, as reference for what's available.env— with sensible local defaults so developers can uncomment and run locally
See the existing entries for database, Redis, Meilisearch, and Typesense as examples.
Components is analyzed with its own hypervel/phpstan extension. Static Analysis is the source of truth for each diagnostic's intended fix and legitimate exceptions; the rules below add Components-specific requirements.
Components enables noUntypedConfigReads in production analysis. When no typed getter covers the accepted values or a diagnostic must inspect malformed input, keep the generic read and add a local @phpstan-ignore hypervel.untypedConfigRead (reason) explaining that contract.
Keep hypervel/phpstan evolving — Codify framework contracts and useful conventions. Prefer a simple framework fix when it prevents the mistake. Add rules for mistakes likely to recur in packages or apps, even when existing cases are already fixed. Check each proposed rule against real mistakes and realistic correct usage. Reject rules that flag correct code or need a growing list of exceptions to avoid doing so. Keep inference limits explicit. Reach peer consensus, then seek owner approval for a new rule. If an existing rule flags correct code, fix the analyzer and leave the correct code alone.
The main PHPStan configuration excludes tests/. Use composer analyse:tests for test-only rules.
Run full PHPStan checks with composer analyse. During implementation, use targeted PHPStan only when investigating or validating a specific type issue.
phpstan.types.neon.dist validates only the committed types/ fixtures. Never pass source or test paths to it.
When fixing phpstan errors:
- Investigate before coding. For each error: read the code, check the Laravel equivalent's types (native and docblock), trace through callers and dependents. Report findings with the single, most correct fix.
- Don't make the code worse or more convoluted just to satisfy PHPStan. Fix real issues in the code, but don't add awkward wrappers, fake branches, casts, or wider types just to silence PHPStan. A type-only fix must not change runtime behavior, add overhead, or introduce new edge cases. A genuine runtime bug that PHPStan exposes is fixed through the normal bug-fix workflow. If you cannot satisfy PHPStan within those constraints, reach consensus on a compliant solution through a second-opinion loop with your peer agent. If the code is correct and PHPStan cannot understand it, follow the source-first rule below.
- Check the supported runtime contract before removing guards. Preserve necessary validation of caller input, consumer configuration and external data. Remove checks that only repeat guarantees the code already enforces; PHPDoc types and facts inferred from the booted application are not such guarantees.
- Don't change contract/concrete boundaries to fix phpstan. Swapping a contract for a concrete (or vice versa) to satisfy a type check diverges from Laravel's API. Only do this when Laravel's typing is genuinely incorrect.
- Type decisions must be evidence-based. See Development Conventions — check Laravel/Hyperf signatures and docblocks, then trace real control flow. Don't guess.
- Fix types at their source. Correct signatures, PHPDoc, generics, stubs or extensions. Use
assert()only for genuine invariants; assertions may be disabled and cannot replace required validation. A truthful inline@varis a last resort. For verified analysis limitations, use@phpstan-ignore identifier (reason), always including a meaningful reason; never use identifier-less ignores. For a confirmed PHPStan bug, start the reason with@TODO: Remove once PHPStanand describe the needed fix; do not add adocs/todo.mdentry. NEON ignores require approval under the global-ignore rule. - Don't add patterns to
phpstan.neon.diston your own. The neon file's global ignores cover fundamental framework patterns (Eloquent magic, generics,new static). Fix new phpstan errors at the source, not by masking them with new neon rules. Global suppression is a rare exception. First, reach consensus through a second-opinion loop with your peer agent that the error cannot be fixed at the source or narrowed locally, and that the pattern is frequent enough to justify global suppression. Then STOP, explain the reason and recommended suppression to the owner, and wait for approval before adding it. - Environment reads outside config files. Framework source reads the environment outside config files only for a deliberate infrastructure need, such as reads before configuration loads, process transport like
__HYPERVEL_CONTEXT, or test infrastructure like worker tokens. UseEnv::get()with an inline@phpstan-ignore hypervel.noEnvCallsOutsideOfConfig (reason)stating that need.Env::get()is the convention for these intentional reads; switching fromenv()does not fix a read in the wrong place. Do not add directory, path or class-wide exemptions.
When porting Laravel packages, whether first-party or third-party, keep them as close to 1:1 with upstream as possible so future changes are easy to merge. The exceptions are:
- Modernizing PHP types, including native parameter, return, property, and class-constant types, plus other appropriate PHP 8.4+ features, strict types, and strict comparisons
- Applying the class-import convention, including its exceptions
- Converting direct Mockery test doubles to Double under Test doubles
- Applying Hypervel's immutable date conventions to Laravel date construction and modifiers
- Converting container array access (
$app['events']) and dynamic service-property access ($app->events) in ported code to named container methods, and applying the Configuration rules to ported config and its consumers - Adding Laravel-style title docblocks to methods (not classes — see Development Conventions)
- Formatting JavaScript, TypeScript, Vue and frontend CSS sources with the repository's Vite+ formatter configuration, preserving upstream logic, naming, structure and comments
- For ported Laravel packages: making them coroutine-safe, adding Swoole performance enhancements (e.g., static property caching), making them pass PHPStan
- Not porting upstream framework-specific integrations that only make sense in the source framework (for example packages, drivers) unless Hypervel intentionally has an equivalent surface
- Not porting upstream mechanisms that do not make sense in Hypervel's stateful Swoole architecture (for example Laravel's deferred service provider machinery, where the upstream optimization only matters in a per-request bootstrap model)
- Not porting deprecated upstream code or backwards-compatibility shims for versions/features Hypervel does not support — Hypervel is a new framework without Laravel's backwards-compatibility burden, so deprecated APIs and compatibility code that exist only to support older versions should be omitted rather than ported. Before changing or removing a deprecated public Laravel API, reach consensus on the recommended approach through a second-opinion loop with your peer agent. Then STOP, explain the proposed difference and its rationale to the owner, and wait for approval. Here, "upstream" means the framework or package being ported, not one of its dependencies — a Symfony deprecation does not make a Laravel API deprecated while Laravel still retains it. If a deprecated upstream surface still contains behavior that Hypervel actively needs, keep the behavior but move it onto the correct non-deprecated Hypervel-owned surface instead of porting the deprecated alias/wrapper as-is.
- Do not retain dead branches or redundant guards for upstream parity. Verify they are unnecessary in supported Hypervel use from native types, real callers and framework guarantees. Check sibling occurrences independently; existing implementations and tests of unsupported states do not justify keeping them.
- General performance improvements — reach consensus on the expected benefit, correctness, and complexity tradeoffs through a second-opinion loop with your peer agent before implementing.
Hypervel has no obligation to preserve Hypervel-specific behavior from earlier versions, but supported Laravel APIs—including named arguments and protected extension points—must remain compatible unless the user approves a difference. Compatibility means preserving supported behavior, not merely matching names and signatures. Before recommending a departure, investigate the simplest compatible adaptation and reasonable alternatives, and establish the concrete obstacle. Resolve uncertainty through focused tests or prototypes; an assumed architectural conflict or peer agreement is not sufficient evidence. If preserving compatibility would compromise correctness, performance or scalability, or require disproportionate complexity, reach consensus on the cleanest design through a second-opinion loop with your peer agent. Then STOP, explain the proposed difference, supporting evidence and rationale to the owner, and wait for approval before changing it.
When Laravel or a tracked upstream introduces an equivalent to a Hypervel-specific enhancement or fix, compare their capabilities, APIs, coroutine safety and performance, then reach consensus on how to reconcile them through a second-opinion loop with your peer agent. Normally prefer upstream when it meets Hypervel’s requirements. Before deprecating or removing the Hypervel-specific API, STOP, explain the recommendation to the owner, and wait for approval.
Approved adaptations take precedence over upstream fidelity. Preserve Laravel upstream naming, structure, and style everywhere else.
Hyperf is a historical reference rather than an ongoing merge target. For the rare Hyperf port, follow docs/ai/porting-hyperf.md.
When working on a package, check its README for the upstream reference before making changes. Most Hypervel packages are ports of Laravel first-party or third-party ecosystem packages, such as Spatie packages. Most low-level Swoole infrastructure packages were originally ported from Hyperf, and a few packages are Hypervel-specific.
Read docs/ai/porting-hyperf.md only when porting a Hyperf package or update.
Owner-approved exclusions that apply across packages. Package-specific differences belong in the package README.
- Laravel Boost and package-provided Boost resources, including skills and guidelines.
- Pest integration, including generator options and stubs, test-runner selection and Testbench helpers.
- Upstream branches, tests and documentation that apply only when PHP runs on native Windows.
If the Hypervel version of the package doesn't exist yet, create the skeleton using an existing package as a template:
- Porting a Laravel first-party package: Use the
cachepackage as reference - Porting a Hyperf package: Use the
websocket-serverpackage as reference - Porting a Laravel-ecosystem third-party package: Use the
permissionpackage as a reference
Read the reference package's composer.json, LICENSE.md, and README.md and create equivalents for the new package. Every package must be wired in both places: its own src/{package}/composer.json for the subtree split, and the root composer.json for monorepo development. Update autoloading, replace, and Hypervel provider discovery metadata as needed, and add root dependencies with composer require — see Providers and Listeners for where providers should be registered. Create the README using the Package READMEs format under Development Conventions.
Check the source package to see what classes exist. Create a comprehensive todo list with a separate entry for each file to port. The porting process is:
- Copy the file using
cp— never read the source first and write a new version. Copying first then reading the copy avoids reading the file twice, which wastes context. - Read the ENTIRE copied file to understand context. For large files, read them in chunks.
- Update namespaces and apply the Porting Policy's approved adaptations (modernized types, method docblocks, etc.). For Laravel ports, do not make unrelated naming, structural, or style changes; preserve upstream naming, structure, and style. For Hyperf ports, follow
docs/ai/porting-hyperf.md.
For very large files where even reading in chunks is impractical Update the file in chunks from top to bottom — read a chunk, update, read next chunk, update. Do NOT try to search for patterns and update scattered bits.
Search both src/ and tests/ for any use statements or references to the old namespace (e.g., Illuminate\Database\) and update them to the new Hypervel namespace. Verify zero remaining references before proceeding.
After porting is complete, run phpstan on the newly ported package and fix errors. Investigate each error properly — don't reach for ignores without thinking it through. See the Static Analysis section.
Complete the verification workflow under Change Workflow. For failures, follow Handling failing tests.
When ported code adds a provider or listener, wire providers in both Composer metadata locations and register listeners in the service provider's boot() method through closures that resolve them from the container. Follow the full rules under Providers and Listeners.
- Preserve source constant/property/method order in Laravel ports — when porting or merging methods into an existing Hypervel class, insert them in the same relative order as upstream. This keeps diffs meaningful and makes future merges easier.
- Preserve existing comments — use the following rules for upstream code comments and docblocks:
Do not remove or modify upstream code comments unless they are incorrect.
Only remove
@paramand@returnannotations where the description adds nothing beyond what the native type hint and parameter/method name already convey. Examples of removable:@param string $name The name of the cookie(just restatesstring $name),@param int $offset Stream offset(just restatesint $offset). Examples to keep:@param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection,@param int $whence Specifies how the cursor position will be calculated...,@return resource|null(when the native type ismixedbecauseresourceisn't a valid PHP type hint). Keep everything else: behavioral descriptions,@seelinks,@throwsannotations, warnings, contract explanations, usage notes. Modernize the title line to imperative form ("Returns" → "Return", "Retrieves" → "Retrieve") but do not remove or rewrite the body content beneath it. Translate non-English comments to English and fix grammar errors. - Record intentionally omitted upstream features where future syncs will look — When a Laravel feature is intentionally not ported because it does not fit Hypervel's Swoole/coroutine architecture, or because Hypervel has a better native equivalent, record it in three places so a future port cannot miss it: (1) the package README under
Differences From Laravel, following the Package READMEs rules above and explaining what to use instead; (2) a concise source comment at the natural insertion point where the skipped method/class would otherwise sit; (3) a conciseREMOVED:comment at the matching upstream test location when tests are skipped. This three-location requirement applies only to intentionally omitted methods or features, not ordinary ported-and-adapted code. Closed decisions only — real gaps still worth doing go indocs/todo.md. - Replace framework names in code — any occurrence of the word
laravelorhyperfin ported code (string literals, comments, prefixes, identifiers, etc.) must be replaced withhypervel, preserving the original casing. For example:laravel_reserved_→hypervel_reserved_,LaravelExcelExporter→HypervelExcelExporter,HYPERF_VERSION→HYPERVEL_VERSION. This does not apply to namespaces (which have their own conversion rules) or to references that describe the upstream source (e.g., docblock@seelinks to Laravel/Hyperf source). - Don't copy Laravel/Hyperf-specific framework details just to stay 1:1 — preserve public APIs and behavior while translating upstream-specific packages, providers, bootstrap mechanisms, and architecture to Hypervel equivalents. If no equivalent exists, reach consensus on the appropriate adaptation through a second-opinion loop with your peer agent before implementing.
Follow the same cp-then-edit process as source files. This workflow applies to both Hyperf and Laravel test porting. Laravel-specific conversions are covered in Porting Laravel Tests below; Hyperf-specific conversions (namespaces, license headers, container and error-handler mocking, NonCoroutine tests) are covered in docs/ai/porting-hyperf.md.
Test file names and directory structure should mirror the source for both Laravel and Hyperf ports, providing a 1:1 class-to-test mapping. For Laravel ports, this also enables automated porting of upstream PRs. When both Hyperf and Laravel have tests covering the same class, merge them into one file — take the more comprehensive version as the base and add unique tests from the other. Consolidate overlapping Hypervel-specific tests under upstream test names and placement, preserving stronger assertions and distinct coverage. Keep every applicable test from the tracked upstream suite, even if redundant.
List all test files in the source package's tests/ directory. For Laravel packages, also check tests/Integration/{PackageName}/ — that's where Laravel puts its integration tests for each package. Note what each file covers.
Read all files in the existing Hypervel test directory for this package. Categorise them:
- Custom tests (Hypervel-specific, no Hyperf/Laravel equivalent): Keep as-is
- Ported tests (already ported from source): Keep — new source tests must be merged in
One entry per test file. Note the strategy:
- Copy and update — no existing Hypervel test for this
- Merge — Merge upstream tests into the existing Hypervel test file, preserving distinct Hypervel-specific coverage
- Integration — needs external service, goes in
tests/Integration/{PackageName}/ - Investigate — exposes functionality Hypervel may not support, an unsupported feature, or an architectural difference. Reach consensus through a second-opinion loop with your peer agent on what the test covers, whether Hypervel should support it, and the appropriate fix or justified exclusion.
For newly copied files (copy and update):
- Copy the file using
cpto the correct location - Read the ENTIRE copied file to understand context
- Update namespaces, base class, imports, types, docblocks, etc.
For merged files:
- Read BOTH the source file AND the existing Hypervel file
- Merge source tests into the Hypervel file, preserving distinct Hypervel-specific coverage
- Update namespaces, types, docblocks, etc.
For stub/helper files: Copy Stub/ directory files the same way.
Use this exact cadence for each test class:
- Port the test class.
- Run that test class immediately (
./vendor/bin/phpunit --no-progress path/to/TestClass.php). - Fix all straightforward failures.
- If a failure exposes a source bug, or behavior missing from an API Hypervel already supports, follow Framework bug fixes. If it's unclear what Hypervel should support, reach agreement with your peer before proceeding.
- Once the test class is green, move to the next test class. Work serially on one test class at a time.
After all test files are ported, complete the verification workflow under Change Workflow.
Most conversion work is applying general rules from Writing Tests — especially "Stricter typing", "When tests expose source code type errors", and "Helper class namespacing", since Laravel's loose typing and shared test namespaces hide problems that Hypervel exposes. The subsections below cover what is specific to Laravel ports.
- Change
Illuminate\Tests\{Package}toHypervel\Tests\{Package} - Change all
Illuminate\source imports toHypervel\
If Laravel's namespace includes the test class name, keep it. Stripping it causes "Cannot redeclare class" errors.
Some test files reference classes defined in other test files. Laravel gets away with this due to test suite load order. Make tests self-contained by defining required classes locally.
Laravel packages sometimes ship a workbench/ directory with controllers, models, middleware, and a routes/web.php. Hypervel's testbench workbench is shared across every package's tests, so port these into the package-scoped pattern:
- Controllers, models, middleware →
tests/{Package}/Fixtures/..., namespaceHypervel\Tests\{Package}\Fixtures\.... - Routes →
tests/{Package}/Fixtures/routes.php. Load only from tests that need them (test setUp, or a small bootstrap script for CLI subprocesses). Never always-load.
Update upstream test imports to point at the new Fixtures namespace.
Tests for these features should be removed (not commented out) without asking — they will never be supported:
- Built-in database drivers: SQL Server, MongoDB, DynamoDB are not provided. Hypervel ships MySQL, MariaDB, PostgreSQL, and SQLite drivers.
- Cache drivers: Memcached, APC / APCu, DynamoDB, MongoDB
- Dynamic connections:
DB::build(),DB::connectUsing()— incompatible with Swoole connection pooling - Container access: ArrayAccess and dynamic service properties
- Update namespace to
Hypervel\Tests\{Package} - Add
declare(strict_types=1); - Change
Illuminate\imports toHypervel\ - Extend the appropriate test base listed under Base classes
- Ensure
parent::setUp()is called - Add type declarations to model properties
- Convert direct Mockery doubles to Double (see Test doubles) and double the correct native types (PDO, QueryBuilder, Grammar, etc.)
- Ensure fluent doubles return the intended object, accounting for Double's automatic self-return
- Use a test-specific namespace only when helper classes have generic, collision-prone names — already-specific helper names do not need extra namespace ceremony.
- Remove tests only under the rules in Handling failing tests
- Run tests and fix any remaining type errors