VOOZH about

URL: https://deepwiki.com/hypervel/devtool/11.4-test-generation

⇱ Test Generation | hypervel/devtool | DeepWiki


Loading...
Menu

Test Generation

Purpose and Scope

This document covers the test generation system in the Hypervel/Devtool package, specifically the make:test command. This command generates PHPUnit test class files for your application, supporting both feature tests (for integration testing) and unit tests (for isolated component testing). The command provides options to control test placement, namespace, and type.

For console command generation, see Console Command Generation. For other code generators, see Additional Generators.


Command Overview

The TestCommand class implements the make:test command, which extends Hyperf's GeneratorCommand base class to generate test files using the stub template pattern.

Key Characteristics

PropertyValue
Command Namemake:test
Base ClassHyperf\Devtool\Generator\GeneratorCommand
Primary Flag--unit (switches between feature and unit tests)
Default Locationtests/Feature (or tests/Unit with --unit)
Default NamespaceTests\Feature (or Tests\Unit with --unit)
File Extension.php

Sources: src/Generator/TestCommand.php10-15


Test Types

The command generates two distinct types of test classes based on the --unit flag:


Diagram: Test Type Selection Flow

Feature Tests

Feature tests are created by default (without the --unit flag) and are intended for integration testing that may involve multiple components, database interactions, HTTP requests, or external services.

AspectValue
Stub Filetest.stub
Default NamespaceTests\Feature
Default Pathtests/Feature
PurposeIntegration/end-to-end testing

Unit Tests

Unit tests are created when the --unit flag is specified and are designed for isolated testing of individual classes or methods without external dependencies.

AspectValue
Stub Filetest.unit.stub
Default NamespaceTests\Unit
Default Pathtests/Unit
PurposeIsolated component testing

Sources: src/Generator/TestCommand.php24-40


Command Options

The TestCommand provides several options to customize test generation:


Diagram: Command Options Structure

Available Options

OptionShortTypeDescription
--unit-uFlagCreate a unit test instead of a feature test
--path-pOptional ValueSpecify custom file system path for the test file
--namespaceN/AOptional ValueSpecify custom namespace for the test class

The --path option from the parent GeneratorCommand is filtered and reimplemented with VALUE_OPTIONAL semantics to allow the command to use default paths when not specified.

Sources: src/Generator/TestCommand.php42-52


File Generation Process

The test generation process follows the standard generator command pattern with specific logic for handling test type distinctions:


Diagram: Test Generation Sequence

Namespace Resolution

The namespace is determined through the following priority:

  1. Custom Namespace: If --namespace option is provided, use that value
  2. Default Namespace: Otherwise, use Tests\Feature or Tests\Unit based on --unit flag
  3. Configuration Override: Both defaults can be overridden via configuration array

Sources: src/Generator/TestCommand.php33-40

Path Resolution

The file system path is calculated in the getPath() method with this logic:


Diagram: Path Resolution Logic

The path resolution implementation:

  1. Namespace Processing: Determines the namespace (custom or default)
  2. Filename Calculation: Strips namespace prefix from qualified name, converts backslashes to forward slashes, adds .php extension
  3. Path Selection: Uses --path option if provided, otherwise defaults to tests/Unit or tests/Feature based on --unit flag
  4. Final Path: Concatenates selected path with calculated filename

Sources: src/Generator/TestCommand.php57-71


Stub Templates

The command uses two distinct stub templates located in the stubs directory:

Stub Selection Logic


Diagram: Stub Template Selection

Template Files

Test TypeStub FileLocation
Feature Testtest.stubsrc/Generator/stubs/test.stub
Unit Testtest.unit.stubsrc/Generator/stubs/test.unit.stub

The stub template paths can be overridden via configuration by setting config['stub'] for the test generator.

Sources: src/Generator/TestCommand.php24-31


Configuration and Customization

The TestCommand supports configuration through the getConfig() method inherited from GeneratorCommand:

Configuration Options


Diagram: Configuration Structure

Customization Points

Configuration KeyPurposeDefault Value
stubOverride stub template file pathsrc/Generator/stubs/test.stub or test.unit.stub
namespaceOverride default namespaceTests\Feature or Tests\Unit

These configuration options allow applications to customize test generation to match their project structure and conventions.

Sources: src/Generator/TestCommand.php24-40


Usage Patterns

Basic Feature Test


This generates tests/Feature/UserRegistrationTest.php with namespace Tests\Feature.

Basic Unit Test


This generates tests/Unit/UserServiceTest.php with namespace Tests\Unit.

Custom Path


This generates tests/Integration/Api/AuthenticationTest.php while maintaining the default namespace.

Custom Namespace and Path


This generates app/Tests/Custom/CustomTest.php with namespace App\Tests\Custom.

Short Flag Syntax


The -u short flag is equivalent to --unit.

Sources: src/Generator/TestCommand.php42-52 src/Generator/TestCommand.php57-71


Integration with Testing Framework

The generated test classes are designed to integrate with PHPUnit, the standard testing framework for PHP applications. The stubs contain the necessary class structure and imports to create runnable test cases.

Test Class Structure

Both unit and feature tests extend PHPUnit's test case classes and follow PHPUnit naming conventions:

  • Class name must end with Test (e.g., UserServiceTest)
  • Test methods should be prefixed with test or use the @test annotation
  • Classes extend appropriate PHPUnit base classes

The specific structure and base classes used depend on the stub templates (test.stub and test.unit.stub).

Sources: src/Generator/TestCommand.php24-31


File Path Processing

The TestCommand includes specialized path processing logic that differs from standard generator commands:

Path Calculation Algorithm

  1. Namespace Determination:

    • Check if --namespace option is provided
    • If not, call getDefaultNamespace() which returns Tests\Unit or Tests\Feature
  2. Filename Extraction:

    • Take the fully qualified class name
    • Remove the namespace prefix
    • Replace backslashes with forward slashes
    • Append .php extension
  3. Directory Path Selection:

    • Check if --path option is provided
    • If not, use tests/Unit for unit tests or tests/Feature for feature tests
  4. Final Path Assembly:

    • Concatenate directory path with filename: {path}/{filename}

This approach ensures tests are organized into appropriate directories based on their type while allowing full customization when needed.

Sources: src/Generator/TestCommand.php57-71