1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
<?php
/**
* Test: Nette\Utils\Type::with()
*/
declare(strict_types=1);
use Nette\Utils\Type;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
class Foo
{
}
class FooChild extends Foo
{
}
function testTypeHint(string $expected, Type $type): void
{
Assert::same($expected, (string) $type);
eval("function($expected \$_) {};");
}
testTypeHint('string|int', Type::fromString('string')->with('int'));
testTypeHint('string|int|null', Type::fromString('string')->with('?int'));
testTypeHint('string|int|null', Type::fromString('?string')->with('int'));
testTypeHint('string|int|null', Type::fromString('?string')->with('?int'));
testTypeHint('string|int|bool', Type::fromString('string|int')->with('bool'));
testTypeHint('string|int|bool', Type::fromString('string|int')->with('bool|int'));
testTypeHint('(Foo&Bar)|string', Type::fromString('Foo&Bar')->with('string'));
testTypeHint('Foo', Type::fromString('Foo&Bar')->with('Foo'));
testTypeHint('string|(Foo&Bar)', Type::fromString('string')->with('Foo&Bar'));
testTypeHint('(Foo&Bar)|(Foo&FooChild)', Type::fromString('Foo&Bar')->with('Foo&FooChild'));
testTypeHint('(Foo&Bar)|string|int', Type::fromString('(Foo&Bar)|string')->with('int'));
// with Type object
testTypeHint('string|int', Type::fromString('string')->with(Type::fromString('int')));
// mixed
testTypeHint('mixed', Type::fromString('string')->with('mixed'));
testTypeHint('mixed', Type::fromString('mixed')->with('null'));
// Already allows - returns same instance
$type = Type::fromString('string');
Assert::same($type, $type->with('string'));
$type = Type::fromString('string|int|bool');
Assert::same($type, $type->with('int'));
$type = Type::fromString('?string');
Assert::same($type, $type->with('string'));
Assert::same($type, $type->with('null'));
$with = Type::fromString('mixed');
Assert::same($with, Type::fromString('string')->with($with));
$type = Type::fromString('Foo|Bar');
Assert::same($type, $type->with('FooChild'));
$with = Type::fromString('Foo');
Assert::same($with, Type::fromString('Foo&Bar')->with($with));
|