search hotlink icon profile hotlink icon
Re Public | 1.1.x - 1.x.x

On This Page

Mocks are objects used to fake dependencies in code under test. They respond to interactions with predefined behavior and also track whether those interactions actually occur, allowing tests to validate usage as well as results. To create a mock, you must provide a loadable class, trait, or interface. The resulting object acts as a pseudo dependency that extends, uses, or implements the given type. Use only configured mocks in your tests by obtaining a builder via the fake method:

                $example = $this->createMock(Example::class);
$example->fake("<methodToFake>");
            

Note

The entrypoint fake is reserved method name for mocks.

Logical Method Groups

The builder API provides a chainable interface with two logical method groups that define encapsulated interactions. In the prebuilt group, you can validate parameters, set references and return values:

  • expect(<param>: <value>): Validate param by name.
  • set(<param>: <value>): Set value by reference using named params.
  • return: Return value.

Note

Validation checks parameter type and value (===).

For individual operations, use the custom closure group:

  • hook: Execute custom logic.

Note

The custom closure must declare all parameters of the faked method, because Reflex passes all arguments through.

Progressive Interactions

Interactions between the code under test and the mocked dependency are progressive. To handle each call, you can define sequences of logical groups that are applied in order:

                $example->fake("example")

    // first call
    // custom group
    ->hook(function (string &$example) {
        $this->validate($example)
            ->as("##")

        $example = "value"

        return "value";
    })

    // calls 2 - 4
    // prebuilt group
    ->expect(example: "##")
    ->set(example: "value")
    ->return("value")
    ->repeat(2)

    // last call
    // prebuilt group
    // inherits previous set and
    // starts new group by repetition since
    // return already exists
    ->return("other-value");
            

Note

All defined logical groups must be used by the test. If a mock defines three interaction groups but the test triggers only two, the test fails.

Logical Group Types

The first call is handled by a custom logical group. The callback receives the method argument by reference, validates it, assigns a value, and returns a result. The next group uses the prebuilt logical group. It validates the argument, sets it by reference, and returns a fixed value. The repeat call reuses this logical group for two additional calls.

Inheritance & Duplication

Whenever a prebuilt method is duplicated, a new logical group is created. The new group inherits any missing methods from the previous group, allowing you to override only the behavior that changes. In the example above, the final return call duplicates a prebuilt method from the previous group, which starts a new logical group. This group inherits the previously defined argument setup but overrides only the return value for the next interaction.

Note

Duplicated prebuilt methods create new logical groups, inheriting missing methods from the previous group automatically.