VOOZH about

URL: https://deepwiki.com/hypervel/testbench/8.2-models-and-authentication

⇱ Models and Authentication | hypervel/testbench | DeepWiki


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

Models and Authentication

Purpose and Scope

This document covers the models and authentication configuration within the workbench environment. The workbench provides example implementations of Eloquent models and authentication setup that serve as reference patterns for testing authentication-enabled applications. These components are used by the testbench itself for self-testing and can be referenced when writing package tests.

For general database testing patterns, see Database Testing. For workbench structure and discovery, see Workbench Structure and Discovery. For session configuration details, see Session, CORS, and Security Configuration.


The User Model

The workbench provides a basic User model that extends Hypervel's authenticatable base class. This model serves as a reference implementation for authentication testing scenarios.

Model Structure

The User model is defined at workbench/app/Models/User.php1-21 and implements a minimal authenticatable entity:

Workbench\App\Models\User
 extends Hypervel\Foundation\Auth\User (aliased as Authenticatable)
 - Inherits authentication traits
 - Provides authentication contracts

Fillable Attributes

The model defines four mass-assignable attributes workbench/app/Models/User.php14-19:

AttributeTypePurpose
namestringUser's display name
emailstringUser's email address
email_verified_attimestampEmail verification timestamp
passwordstringHashed password

Model Inheritance Diagram


Sources: workbench/app/Models/User.php1-21


Authentication Configuration

The workbench authentication configuration demonstrates a dual-guard setup supporting both session-based and JWT authentication. This configuration is located at workbench/config/auth.php1-27

Configuration Structure


Sources: workbench/config/auth.php1-27

Defaults Configuration

The default authentication settings workbench/config/auth.php6-9:

SettingValueDescription
defaults.guard'session'Default authentication guard for requests
defaults.provider'users'Default user provider for authentication

Guards Configuration

Two authentication guards are defined workbench/config/auth.php10-19:

Session Guard

  • Driver: 'session' - Uses session-based authentication
  • Provider: 'users' - References the users provider
  • Used for traditional web authentication with cookies and sessions

JWT Guard

  • Driver: 'jwt' - Uses JSON Web Token authentication
  • Provider: 'users' - References the same users provider
  • Used for stateless API authentication

Providers Configuration

A single user provider is configured workbench/config/auth.php20-25:

KeyValueDescription
driver'eloquent'Uses Eloquent ORM for user retrieval
model'MockedUser'Model class name (configured as string)

Note: The model is specified as 'MockedUser' rather than the fully qualified Workbench\App\Models\User class name. This string-based configuration allows for runtime model substitution in test scenarios.

Sources: workbench/config/auth.php1-27


Authentication Architecture

The authentication system demonstrates how guards, providers, and models integrate to provide authentication functionality.


Sources: workbench/config/auth.php1-27 workbench/app/Models/User.php1-21

Authentication Flow

  1. Request Processing: An incoming HTTP request requires authentication
  2. Guard Resolution: The system resolves the appropriate guard (session by default)
  3. Provider Delegation: The guard delegates user retrieval to the configured provider
  4. Model Resolution: The provider uses the configured model class to query the database
  5. User Retrieval: The model returns an authenticated user instance or null

Integration with Database Migrations

The User model corresponds to the users table created by the workbench migration. The migration creates a table supporting the model's attributes:

Model AttributeDatabase ColumnMigration Reference
namenameSee Database Migrations
emailemailSee Database Migrations
email_verified_atemail_verified_atSee Database Migrations
passwordpasswordSee Database Migrations

The UserFactory can be used to generate test data for this model. See Model Factories for factory usage patterns.

Sources: workbench/app/Models/User.php14-19


Usage in Tests

The workbench authentication components can be used in package tests that require authenticated users:

Example Testing Scenarios

ScenarioComponents UsedPurpose
Session Authentication TestingSession guard + User modelTest web routes requiring authentication
JWT Authentication TestingJWT guard + User modelTest API endpoints with token authentication
User Factory TestingUserFactory + User modelGenerate test users for seeding
Authorization TestingUser model + policiesTest permission-based access control

Configuration Override Pattern

Tests can override the authentication configuration by implementing the defineEnvironment callback:

protected function defineEnvironment($app): void
{
 // Override default guard
 $app['config']->set('auth.defaults.guard', 'jwt');
 
 // Override model class
 $app['config']->set('auth.providers.users.model', CustomUser::class);
}

This pattern allows tests to customize authentication behavior without modifying the workbench files directly.

Sources: workbench/config/auth.php1-27 workbench/app/Models/User.php1-21


Model Configuration Considerations

String-Based Model Configuration

The authentication provider uses a string-based model reference ('MockedUser') at workbench/config/auth.php23 This design choice enables:

  • Runtime Substitution: Tests can override the model class without modifying configuration files
  • Mocking: The string can be resolved to different model implementations in test scenarios
  • Flexibility: Supports dynamic model resolution based on test requirements

Fully Qualified Class Names

In production applications, it's recommended to use fully qualified class names:


The workbench uses a string to demonstrate configuration flexibility for testing purposes.

Sources: workbench/config/auth.php20-25


Summary

The workbench authentication system provides:

  1. User Model: A basic authenticatable model extending Hypervel\Foundation\Auth\User with standard user attributes
  2. Dual Guard Configuration: Support for both session-based and JWT authentication patterns
  3. Eloquent Provider: Database-backed user provider using the Eloquent ORM
  4. Testing Flexibility: String-based configuration allowing runtime model substitution

These components serve as reference implementations for testing authentication in Hypervel applications and can be customized via the defineEnvironment callback pattern for package-specific authentication testing scenarios.

Sources: workbench/app/Models/User.php1-21 workbench/config/auth.php1-27