1. Go to this page and download the library: Download nullbio/hegel-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nullbio / hegel-php example snippets
hegel('array reverse is an involution', function (TestCase $tc) {
$arr = $tc->draw(Generators::arrays(Generators::integers()));
expect(array_reverse(array_reverse($arr)))->toBe($arr);
});
hegel('sort preserves length', function (TestCase $tc) {
$arr = $tc->draw(Generators::arrays(Generators::integers()));
$original = count($arr);
sort($arr);
expect(count($arr))->toBe($original);
});
hegel('parse never crashes on arbitrary input', function (TestCase $tc) {
$input = $tc->draw(Generators::text());
// Just verify it doesn't throw — any result is fine
MyParser::parse($input);
});
hegel('shuffle preserves contents', function (TestCase $tc) {
$random = $tc->randomizer();
$values = $tc->draw(Generators::arrays(Generators::integers()));
$shuffled = $random->shuffleArray($values);
sort($values);
sort($shuffled);
expect($shuffled)->toBe($values);
});
final readonly class UserData
{
public function __construct(
public string $email,
public int $age,
) {
}
}
$user = $tc->draw(Generators::default(UserData::class));
$strictUser = $tc->draw(
Generators::object(UserData::class)
->with('email', Generators::emails())
->with('age', Generators::integers()->minValue(18)->maxValue(99))
);
$money = $tc->draw(
Generators::fixedDicts()
->field('amount', Generators::integers()->minValue(1))
->field('currency', Generators::sampledFrom(['USD', 'EUR']))
->into(Money::class)
);
hegel('intensive test', function (TestCase $tc) {
// ...
})->testCases(500)->seed(42)->verbosity('verbose');
use Hegel\Generators;
use Hegel\Settings;
use Hegel\Testing\InteractsWithHegel;
use Hegel\Verbosity;
final class CartTest extends Tests\TestCase
{
use InteractsWithHegel;
public function test_add_item_is_stable(): void
{
$settings = (new Settings())
->testCases(200)
->verbosity(Verbosity::Debug);
$this->hegel(function (): void {
$quantity = $this->draw(Generators::integers()->minValue(1)->maxValue(10));
expect($quantity)->toBeGreaterThan(0);
}, $settings);
}
}
use Hegel\Stateful\Attributes\Invariant;
use Hegel\Stateful\Attributes\Rule;
use function Hegel\Stateful\run as runStateMachine;
use function Hegel\Stateful\variables;
hegel('queue model stays consistent', function (TestCase $tc) {
$machine = new class ($tc) {
private \Hegel\Stateful\Variables $items;
private array $model = [];
public function __construct(TestCase $tc)
{
$this->items = variables($tc);
}
#[Rule]
public function enqueue(TestCase $tc): void
{
$value = $tc->draw(Generators::integers());
$this->items->add($value);
$this->model[] = $value;
}
#[Rule]
public function dequeue(): void
{
if ($this->items->empty()) {
return;
}
expect($this->items->consume())->toBe(array_shift($this->model));
}
#[Invariant('model length stays nonnegative')]
public function checkModelLength(): void
{
expect(count($this->model))->toBeGreaterThanOrEqual(0);
}
};
runStateMachine($machine, $tc);
});
use Hegel\Stateful\Invariant;
use Hegel\Stateful\Rule;
use Hegel\Stateful\StateMachine;
use function Hegel\Stateful\run as runStateMachine;
use function Hegel\Stateful\variables;
hegel('queue model stays consistent', function (TestCase $tc) {
$machine = new class ($tc) implements StateMachine {
private \Hegel\Stateful\Variables $items;
private array $model = [];
public function __construct(TestCase $tc)
{
$this->items = variables($tc);
}
public function rules(): array
{
return [
Rule::new('enqueue', function (TestCase $tc): void {
$value = $tc->draw(Generators::integers());
$this->items->add($value);
$this->model[] = $value;
}),
Rule::new('dequeue', function (): void {
if ($this->items->empty()) {
return;
}
expect($this->items->consume())->toBe(array_shift($this->model));
}),
];
}
public function invariants(): array
{
return [
Invariant::new('model length stays nonnegative', function (): void {
expect(count($this->model))->toBeGreaterThanOrEqual(0);
}),
];
}
};
runStateMachine($machine, $tc);
});
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.