Skip to content

group

The group attribute links code blocks together so they share state within a single PHP process. Variables defined in one block are accessible in subsequent blocks of the same group.

Syntax

markdown
```php group="name"
// Blocks with the same group name share state
```

How It Works

  1. All blocks with the same group value are collected
  2. They execute in document order within a single PHP process
  3. Variables, functions, and classes defined in earlier blocks are available in later ones
  4. Each block's assertions are evaluated independently

Example

php
$numbers = [1, 2, 3, 4, 5];
$sum = array_sum($numbers);
echo $sum;
php
// $numbers and $sum are still available
$average = $sum / count($numbers);
echo $average;

With Setup and Teardown

Groups are commonly used with setup and teardown blocks:

php
$pdo = new PDO('sqlite::memory:');
$pdo->exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');
php
$pdo->exec("INSERT INTO users (name) VALUES ('Alice')");
$count = $pdo->query('SELECT COUNT(*) FROM users')->fetchColumn();
echo $count;
php
$pdo->exec("INSERT INTO users (name) VALUES ('Bob')");
$count = $pdo->query('SELECT COUNT(*) FROM users')->fetchColumn();
echo $count;
php
$pdo->exec('DROP TABLE users');

Execution Order

Within a group:

  1. setup blocks run first (in document order)
  2. Regular group blocks run in document order
  3. teardown blocks run last (in document order)

Multiple Groups

You can have multiple independent groups in the same file:

php
$greeting = 'Hello';
php
$count = 42;
php
echo $greeting . ', World!';
php
echo $count * 2;

Each group runs in its own process.

Released under the MIT License.