Skip to content

divide 0.10.0

Choose a tag to compare

@github-actions github-actions released this 26 Nov 14:16

divide 0.10.0 adds a Mock Simulation of a single Runner Function. You give it a context and doubles for the dependencies it declares, divide runs the Function once and records every PHP line it executes, and you replay the run line by line in the Function's own code.

MOCK SIMULATION: real PHP runs; only declared dependencies are replaced. The Function's own PHP code runs for real, in a PHP process of its own. Only what it asks its context for is replaced: other Functions ($context['_importer']), Runner's logger ($context['_logger']) and service objects under context keys. PHP's own functions (files, databases, curl, mail, time()), the file's top-level code and, if you opt in, the project's Composer classes run for real. It is not a sandbox and not side-effect-free. Simulate only code you trust.

Not a Flow Simulation

Each way divide runs a Function does something different, and each keeps its own keys:

What runs What you get
Run a Flow (Ctrl+F9) the Flow and its Functions, for real the context after it
Test Function (T, Ctrl+F9 in the Functions window) one Function, for real, with its real dependencies its report in Output
Debug (Test Function > Debug) the same call, for real, under the debugger breakpoints, F7 / F8, watches
Flow Simulation (Shift+F9) Runner's simulation of the Flow: no Function code runs the Flow's events on its board
Mock Simulation (Shift+T), new one Function's real PHP, its declared dependencies doubled its PHP lines, replayed in its code

A Mock Simulation never reads or writes a Flow's checkpoints (.div/runner/) or a Flow simulation (.div/simulation/), and the Flow board never shows PHP lines.

Mock Simulation of a Function

Open it with:

  • Shift+T on a Step in the Flow Designer: its activity, else its condition;
  • Function... > Simulate... in the Step dialog;
  • Alt+F10 > Simulate... in the Functions window;
  • Run > Mock Simulation of a Function.

The dialog holds a scenario:

  • Context: the JSON context the Function gets, by reference.
  • Doubles: importer for other Functions and services for objects under context keys. Each double is one of Runner's --simulate outcomes: returns, throws or sequence.
  • Expectations: an optional expected return and context keys.
  • Limits: lines, seconds and output bytes.
  • Options: the line trace, the pin, a tripwire and Composer autoload.

A scenario can be saved in .div/function-simulations/, for the team in Git.

The doubles are filled in for you. divide reads the Function's file, without running it, and finds what it asks its context for. Each dependency it finds gets a placeholder that answers null. Simulate and Save ask before a placeholder is used: Keep null when null is the answer you mean, or Back to give it a value.

Refused before anything runs. A scenario is refused, and nothing runs, when:

  • a field is not valid JSON;
  • an outcome is malformed;
  • a limit is out of range;
  • a dependency the file uses has no double.

A dependency with no double is never the real one: the simulation throws when the Function asks for it.

The pin. A new scenario is pinned to the SHA-256 of the Function's file.

  • If the file changed since, the scenario is STALE and nothing runs. It is re-pinned only when you choose Re-pin, or Re-pin and simulate, and a saved scenario changes only when you Save it.
  • A scenario without a pin is always labelled NOT pinned to a source version.

Opt-in. The first Mock Simulation in a project asks you to allow it, after saying what it runs. Your settings keep only that answer, the options and the last scenario's name, never the context or the doubles.

The playback

With Xdebug, divide steps through the Function with its own Xdebug session, separate from the debugger and your breakpoints. The playback opens the Function's file read-only with the line about to run marked. Beside it are:

  • the line's locals and the context so far;
  • what changed and what the doubles answered, written as calls: mailer->send("a", 1) #1: returns;
  • the logs and the output;
  • the result, with each expectation [OK] or [!!].
Key Does
F7 / Shift+F7 next / previous line
F8 / Shift+F8 next / previous line at the same depth
F4 the next time the cursor's line ran
Ctrl+Home / Ctrl+End first / last line
X the whole context before the line, in Output
S save the run as a scenario

Moving only moves a cursor: nothing runs again and no file changes. Traced runs are kept in .div/function-simulation/, ignored by Git, with the same retention as Flow simulations, and Replay reopens the last one without running PHP.

Without Xdebug, divide asks whether to simulate without the line trace. You then get the result and no lines, and the PHP still runs for real.

Also

  • The help has a new page, Function Mock Simulation (F1 in its dialog or playback), in English and Spanish. The Keyboard Reference, Flow Designer, Flow Functions and Flow Simulation pages link it.
  • The design, its protocols (divide.function-simulation v1 scenarios, divide.function-trace v1 traces) and its limits are in docs/function-simulation.md.
  • The Spanish interface covers all of the above.
  • The replace prompt's All button ends the prompt again.

Known limits

  • Not a sandbox. The optional tripwire disables PHP's process, network and mail functions and the commonest file writes, and fences off files outside the project. Other side effects (fopen, PDO, static state) still happen. Stronger isolation is a separate investigation (#24).
  • Only declared dependencies are doubled. A Function that opens files, makes new services or calls static methods itself runs those parts for real.
  • Not controlled: time(), rand() and uniqid().
  • Flow-driving Functions are out of scope. A Function that drives the Flow (_flow_state, _update_context) belongs to Flow Simulation.
  • Speed. The line trace runs at about 2,000 lines a second, bounded by the scenario's line limit.

Install

Ready to run, with nothing else to install:

  • macOS and Linux: brew install divengine/tap/divide
  • Windows: scoop install https://github.com/divengine/divide/releases/latest/download/divide.json, or the .msi below
  • Linux: the .deb, .rpm, .apk and Arch packages, the AppImage or the binary alone, below
  • macOS: the archive for Apple silicon or Intel, below

Requirements:

  • Rendering and checking includes: PHP 8 and Div (composer require divengine/div).
  • Runner Flows: Runner (composer require divengine/runner, 0.3 or newer to simulate a Flow).
  • Mock Simulation: PHP 8.1 or newer, and Xdebug for the line trace. Runner is not needed for it.
  • The Git menu: git.

divide is free software under the GPL, version 3 or later. The binaries are not signed yet: see Installation.