packages/requirements/src/Test/TestResult.php
1<?php
2
3declare(strict_types=1);
4
5namespace Requirements\Test;
6
7/**
8 * The outcome of running one test target.
9 *
10 * The status is "passed", "failed", "error" or "unverified"; only "passed" with at least one
11 * executed test verifies a specification.
12 *
13 * @visibility public
14 *
15 * @example Reporting two passing data-provider cases
16 * (new \Requirements\Test\TestResult('passed', 2))->toArray() // => ['status' => 'passed', 'tests' => 2, 'message' => '']
17 */
18final class TestResult
19{
20 /**
21 * @param string $status The verdict of the run
22 * @param int $tests The number of executed test cases
23 * @param string $message Why the run did not pass, or an empty string
24 */
25 public function __construct(public readonly string $status, public readonly int $tests, public readonly string $message = '')
26 {
27 }
28
29 /**
30 * Returns the result as a JSON-ready record.
31 *
32 * @return array{status: string, tests: int, message: string} The status, executed tests and message
33 */
34 public function toArray(): array
35 {
36 return ['status' => $this->status, 'tests' => $this->tests, 'message' => $this->message];
37 }
38}
39