packages/requirements/tests/Fake/CountingRunner.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Fake;
6
7use Override;
8use Requirements\Test\RunnerConfig;
9use Requirements\Test\RunnerExtension;
10use Requirements\Test\TestResult;
11
12/**
13 * A runner that records every target it runs in executions.txt of its working directory.
14 *
15 * Every target passes with one test, except "empty", which passes with none.
16 */
17final class CountingRunner implements RunnerExtension
18{
19    /**
20     * Records and passes a target.
21     *
22     * @param RunnerConfig $config The runner configuration
23     * @param string $target The target
24     *
25     * @return TestResult A pass with one test, or with none for "empty"
26     */
27    #[Override]
28    public function run(RunnerConfig $config, string $target): TestResult
29    {
30        file_put_contents($config->directory . '/executions.txt', $target . "\n", FILE_APPEND);
31        return new TestResult('passed', $target === 'empty' ? 0 : 1);
32    }
33}
34