respect-validation/tests/feature/Validators/PropertyTest.php
Alexandre Gomes Gaigalas d9cdc118b2 Introduce REUSE compliance
This commit introduces REUSE compliance by annotating all files
with SPDX information and placing the reused licences in the
LICENSES folder.

We additionally removed the docheader tool which is made obsolete
by this change.

The main LICENSE and copyright text of the project is now not under
my personal name anymore, and it belongs to "The Respect Project
Contributors" instead.

This change restores author names to several files, giving the
appropriate attribution for contributions.
2026-01-21 06:28:11 +00:00

119 lines
6 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
test('Missing property', catchAll(
fn() => v::property('foo', v::intType())->assert(new stdClass()),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` must be present')
->and($fullMessage)->toBe('- `.foo` must be present')
->and($messages)->toBe(['foo' => '`.foo` must be present']),
));
test('Default', catchAll(
fn() => v::property('foo', v::intType())->assert((object) ['foo' => 'string']),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` must be an integer')
->and($fullMessage)->toBe('- `.foo` must be an integer')
->and($messages)->toBe(['foo' => '`.foo` must be an integer']),
));
test('Inverted', catchAll(
fn() => v::not(v::property('foo', v::intType()))->assert((object) ['foo' => 12]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` must not be an integer')
->and($fullMessage)->toBe('- `.foo` must not be an integer')
->and($messages)->toBe(['foo' => '`.foo` must not be an integer']),
));
test('Double-inverted with missing property', catchAll(
fn() => v::not(v::not(v::property('foo', v::intType())))->assert(new stdClass()),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` must be present')
->and($fullMessage)->toBe('- `.foo` must be present')
->and($messages)->toBe(['foo' => '`.foo` must be present']),
));
test('With wrapped name, missing property', catchAll(
fn() => v::property('foo', v::named('Wrapped', v::intType()))->assert(new stdClass()),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Wrapped must be present')
->and($fullMessage)->toBe('- Wrapped must be present')
->and($messages)->toBe(['foo' => 'Wrapped must be present']),
));
test('With wrapped name, default', catchAll(
fn() => v::named('Wrapper', v::property('foo', v::named('Wrapped', v::intType())))->assert((object) ['foo' => 'string']),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Wrapped must be an integer')
->and($fullMessage)->toBe('- Wrapped must be an integer')
->and($messages)->toBe(['foo' => 'Wrapped must be an integer']),
));
test('With wrapped name, inverted', catchAll(
fn() => v::named('Not', v::not(
v::named('Wrapper', v::property('foo', v::named('Wrapped', v::intType()))),
))
->assert((object) ['foo' => 12]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Wrapped must not be an integer')
->and($fullMessage)->toBe('- Wrapped must not be an integer')
->and($messages)->toBe(['foo' => 'Wrapped must not be an integer']),
));
test('With wrapper name, default', catchAll(
fn() => v::named('Wrapper', v::property('foo', v::intType()))->assert((object) ['foo' => 'string']),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` (<- Wrapper) must be an integer')
->and($fullMessage)->toBe('- `.foo` (<- Wrapper) must be an integer')
->and($messages)->toBe(['foo' => '`.foo` (<- Wrapper) must be an integer']),
));
test('With wrapper name, missing property', catchAll(
fn() => v::named('Wrapper', v::property('foo', v::intType()))->assert(new stdClass()),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` (<- Wrapper) must be present')
->and($fullMessage)->toBe('- `.foo` (<- Wrapper) must be present')
->and($messages)->toBe(['foo' => '`.foo` (<- Wrapper) must be present']),
));
test('With wrapper name, inverted', catchAll(
fn() => v::named('Not', v::not(v::named('Wrapper', v::property('foo', v::intType()))))
->assert((object) ['foo' => 12]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` (<- Wrapper) must not be an integer')
->and($fullMessage)->toBe('- `.foo` (<- Wrapper) must not be an integer')
->and($messages)->toBe(['foo' => '`.foo` (<- Wrapper) must not be an integer']),
));
test('With "Not" name, inverted', catchAll(
fn() => v::named('Not', v::not(v::property('foo', v::intType())))->assert((object) ['foo' => 12]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` (<- Not) must not be an integer')
->and($fullMessage)->toBe('- `.foo` (<- Not) must not be an integer')
->and($messages)->toBe(['foo' => '`.foo` (<- Not) must not be an integer']),
));
test('With template, default', catchAll(
fn() => v::property('foo', v::intType())
->assert((object) ['foo' => 'string'], 'Particularly precautions perplexing property'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Particularly precautions perplexing property')
->and($fullMessage)->toBe('- Particularly precautions perplexing property')
->and($messages)->toBe(['foo' => 'Particularly precautions perplexing property']),
));
test('With template, inverted', catchAll(
fn() => v::not(v::property('foo', v::intType()))
->assert((object) ['foo' => 12], 'Not a prompt prospect of a particularly primitive property'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Not a prompt prospect of a particularly primitive property')
->and($fullMessage)->toBe('- Not a prompt prospect of a particularly primitive property')
->and($messages)->toBe(['foo' => 'Not a prompt prospect of a particularly primitive property']),
));