File: Type.allows.phpt

package info (click to toggle)
php-nette-utils 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,460 kB
  • sloc: php: 4,146; xml: 9; makefile: 4
file content (101 lines) | stat: -rw-r--r-- 2,567 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

/**
 * Test: Nette\Utils\Type::allows()
 */

declare(strict_types=1);

use Nette\Utils\Type;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


class Bar
{
}

class Baz
{
}

class Foo
{
}

class FooChild extends Foo
{
}


$type = Type::fromString('string');
Assert::true($type->allows('string'));
Assert::false($type->allows('null'));
Assert::false($type->allows('string|null'));
Assert::false($type->allows('Foo'));
Assert::false($type->allows('FooChild'));
Assert::false($type->allows('Foo|FooChild'));
Assert::false($type->allows('Foo&Bar'));


$type = Type::fromString('string|null');
Assert::true($type->allows('string'));
Assert::true($type->allows('null'));
Assert::true($type->allows('string|null'));
Assert::false($type->allows('Foo'));
Assert::false($type->allows('FooChild'));
Assert::false($type->allows('Foo|FooChild'));
Assert::false($type->allows('Foo&Bar'));


$type = Type::fromString('string|Foo');
Assert::true($type->allows('string'));
Assert::false($type->allows('null'));
Assert::false($type->allows('string|null'));
Assert::true($type->allows('Foo'));
Assert::true($type->allows('FooChild'));
Assert::true($type->allows('Foo|FooChild'));
Assert::true($type->allows('Foo&Bar'));


$type = Type::fromString('mixed');
Assert::true($type->allows('string'));
Assert::true($type->allows('null'));
Assert::true($type->allows('string|null'));
Assert::true($type->allows('Foo'));
Assert::true($type->allows('FooChild'));
Assert::true($type->allows('Foo|FooChild'));
Assert::true($type->allows('Foo&Bar'));


$type = Type::fromString('Bar&Foo');
Assert::false($type->allows('string'));
Assert::false($type->allows('null'));
Assert::false($type->allows('Foo'));
Assert::false($type->allows('FooChild'));
Assert::true($type->allows('Foo&Bar'));
Assert::true($type->allows('FooChild&Bar'));
Assert::true($type->allows('Foo&Bar&Baz'));


$type = Type::fromString('Bar&FooChild');
Assert::false($type->allows('Foo&Bar'));


$type = Type::fromString('(Bar&Foo)|null');
Assert::false($type->allows('string'));
Assert::true($type->allows('null'));
Assert::false($type->allows('Foo'));
Assert::false($type->allows('FooChild'));
Assert::true($type->allows('Foo&Bar'));
Assert::true($type->allows('FooChild&Bar'));
Assert::true($type->allows('Foo&Bar&Baz'));
Assert::true($type->allows('(Foo&Bar&Baz)|null'));


// allows() with Type object
$type = Type::fromString('string|int');
Assert::true($type->allows(Type::fromString('string')));
Assert::true($type->allows(Type::fromString('int')));
Assert::false($type->allows(Type::fromString('bool')));