Skip to content

Core\Test

The methods you call inside a #[Test] method. They check a result, move the fixed clock and give fake replies to the web requests your code sends. nvs test runs the test and reports each check that fails.

Core\Test is the assertion surface: every member is static, takes the subject first (assertEquals($actual, $expected)), and is generic, so comparing an int with a string is a compile error rather than a failed test. A failed assertion throws Core\Test\Failure, an ordinary Throwable a catch may take — but the runner keeps its own ledger of what held and what failed, and a catch does not erase an entry; expectFailure is the one member that discharges one. A test is a public, non-static method returning void and marked #[Core\Test] (or #[Test] after use Core\Test;), run by nvs test; a test that asserts nothing fails. Marking, fixtures, data rows and the runner's report belong to testing.

A test can also run your whole program. request sends one HTTP request to it and returns the response. run runs it as a command and returns the exit code and the output. Both open no network connection and start no process. logs returns the log records the test wrote, and sentMail returns the mail it sent. Under nvs test, Core\Mail::send keeps each message and sends nothing.

<?nvs
final class Cart
{
private array<int> $prices = [];
public function add(int $price): void
{
if ($price < 0) {
throw new LogicError('a price is never negative');
}
$this->prices = Core\Arr::append($this->prices, $price);
}
public function total(): int
{
return Core\Arr::sum($this->prices) as int;
}
public function lines(): array<int>
{
return $this->prices;
}
}
final class CartTest
{
#[Core\Test]
public function sumsWhatWasAdded(): void
{
var $cart = new Cart();
$cart->add(3);
$cart->add(4);
Core\Test::assertSame($cart->total(), 7);
Core\Test::assertCount($cart->lines(), 2, { message: 'two lines were added' });
}
#[Core\Test]
public function refusesANegativePrice(): void
{
var $cart = new Cart();
Core\Test::assertThrows(fn(): void => { $cart->add(-1); }, LogicError::class);
Core\Test::assertTrue($cart->total() == 0);
}
}
Output
CartTest
✓ sumsWhatWasAdded
✓ refusesANegativePrice
0 failed, 2 passed

Members

28 members.

MemberSignature
assertSameassertSame(T $actual, T $expected, { message?: string }): void
assertEqualsassertEquals(T $actual, T $expected, { message?: string }): void
assertEqualsDeepassertEqualsDeep(T $actual, T $expected, { message?: string }): void
assertTrueassertTrue(bool $actual, { message?: string }): void
assertNullassertNull(mixed $actual, { message?: string }): void
assertCountassertCount(array<T> $actual, uint $expected, { message?: string }): void
assertContainsassertContains(array<T> $actual, T $expected, { message?: string }): void
assertMatchesInlineassertMatchesInline(mixed $actual, string $expected, { message?: string }): void
assertThrowsassertThrows(callable(): mixed $body, string $expected, { message?: string }): void
assertDoesNotThrowassertDoesNotThrow(callable(): mixed $body, { message?: string }): void
expectFailureexpectFailure(callable(): mixed $body): void
advanceadvance(Core\Time\Duration $by): void
serverUrlserverUrl(): ?string
answerClianswerCli(array<string> $answers): void
requestrequest(Core\Http\Method $method, string $path, { headers?: array<string>, body?: string|bytes|Core\Http\Part, mount?: string, captures?: array<string>, cookies?: array<string>, clientIp?: string, scheme?: string, json?: mixed, form?: array<string>, contentType?: string, multipart?: array<string|Core\Http\Part> }): Core\Test\Response
runrun(array<string> $arguments, { stdin?: string|bytes }): Core\Test\RunResult
logslogs(): array<Core\Test\LogRecord>
sentMailsentMail(): array<Core\Test\SentMail>
answerHttpanswerHttp(string $url, uint $status, { json?: mixed, body?: string|bytes, headers?: array<string|array<string>>, tls?: Core\Http\TlsInfo }): void
tlsSessiontlsSession({ version?: string, cipher?: string, verified?: bool, subject?: string, issuer?: string, expiry?: Core\Time\Instant }): Core\Http\TlsInfo
sentHttpsentHttp(): array<Core\Test\SentRequest>
answerSocketanswerSocket(string $url, array<string|bytes> $frames, { protocol?: string }): void
sentSocketsentSocket(): array<Core\Socket\Message>
doubledouble<T>(object $answers): T
partialpartial<T>(T $real, object $answers): T
assertCalledassertCalled(object $double, method $method, { times?: uint, with?: array<mixed>, message?: string }): void
assertNeverCalledassertNeverCalled(object $double, method $method, { message?: string }): void
assertCompletesassertCompletes(callable(): mixed $body, { within: Core\Time\Duration } $settings, { message?: string }): void