summaryrefslogtreecommitdiff
path: root/Zend/tests/type_declarations/scalar_strict_basic.phpt
blob: cb385422bdb0ad25ef387f9db52ddc3ef4342a9f (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
--TEST--
Strict scalar type basics
--FILE--
<?php

declare(strict_types=1);

$functions = [
    'int' => function (int $i) { return $i; },
    'float' => function (float $f) { return $f; },
    'string' => function (string $s) { return $s; },
    'bool' => function (bool $b) { return $b; }
];

$values = [
    1,
    1.0,
    "1",
    TRUE,
    FALSE,
    NULL,
    [],
    new StdClass,
    fopen("data:text/plain,foobar", "r")
];

function type($value) {
    if (is_float($value)) {
        return "float";
    } else if (is_bool($value)) {
        return $value ? "true" : "false";
    } else if (is_null($value)) {
        return "null";
    } else {
        return gettype($value);
    }
}

foreach ($functions as $type => $function) {
    echo PHP_EOL, "Testing '$type' type:", PHP_EOL;
    foreach ($values as $value) {
        $errored = false;
        echo PHP_EOL . "*** Trying ", type($value), " value", PHP_EOL;
        try {
            var_dump($function($value));
        } catch (TypeError $e) {
            echo "*** Caught " . $e->getMessage() . PHP_EOL;
        }
    }
}
echo PHP_EOL . "Done";
?>
--EXPECTF--
Testing 'int' type:

*** Trying integer value
int(1)

*** Trying float value
*** Caught {closure}(): Argument #1 ($i) must be of type int, float given, called in %s on line %d

*** Trying string value
*** Caught {closure}(): Argument #1 ($i) must be of type int, string given, called in %s on line %d

*** Trying true value
*** Caught {closure}(): Argument #1 ($i) must be of type int, bool given, called in %s on line %d

*** Trying false value
*** Caught {closure}(): Argument #1 ($i) must be of type int, bool given, called in %s on line %d

*** Trying null value
*** Caught {closure}(): Argument #1 ($i) must be of type int, null given, called in %s on line %d

*** Trying array value
*** Caught {closure}(): Argument #1 ($i) must be of type int, array given, called in %s on line %d

*** Trying object value
*** Caught {closure}(): Argument #1 ($i) must be of type int, object given, called in %s on line %d

*** Trying resource value
*** Caught {closure}(): Argument #1 ($i) must be of type int, resource given, called in %s on line %d

Testing 'float' type:

*** Trying integer value
float(1)

*** Trying float value
float(1)

*** Trying string value
*** Caught {closure}(): Argument #1 ($f) must be of type float, string given, called in %s on line %d

*** Trying true value
*** Caught {closure}(): Argument #1 ($f) must be of type float, bool given, called in %s on line %d

*** Trying false value
*** Caught {closure}(): Argument #1 ($f) must be of type float, bool given, called in %s on line %d

*** Trying null value
*** Caught {closure}(): Argument #1 ($f) must be of type float, null given, called in %s on line %d

*** Trying array value
*** Caught {closure}(): Argument #1 ($f) must be of type float, array given, called in %s on line %d

*** Trying object value
*** Caught {closure}(): Argument #1 ($f) must be of type float, object given, called in %s on line %d

*** Trying resource value
*** Caught {closure}(): Argument #1 ($f) must be of type float, resource given, called in %s on line %d

Testing 'string' type:

*** Trying integer value
*** Caught {closure}(): Argument #1 ($s) must be of type string, int given, called in %s on line %d

*** Trying float value
*** Caught {closure}(): Argument #1 ($s) must be of type string, float given, called in %s on line %d

*** Trying string value
string(1) "1"

*** Trying true value
*** Caught {closure}(): Argument #1 ($s) must be of type string, bool given, called in %s on line %d

*** Trying false value
*** Caught {closure}(): Argument #1 ($s) must be of type string, bool given, called in %s on line %d

*** Trying null value
*** Caught {closure}(): Argument #1 ($s) must be of type string, null given, called in %s on line %d

*** Trying array value
*** Caught {closure}(): Argument #1 ($s) must be of type string, array given, called in %s on line %d

*** Trying object value
*** Caught {closure}(): Argument #1 ($s) must be of type string, object given, called in %s on line %d

*** Trying resource value
*** Caught {closure}(): Argument #1 ($s) must be of type string, resource given, called in %s on line %d

Testing 'bool' type:

*** Trying integer value
*** Caught {closure}(): Argument #1 ($b) must be of type bool, int given, called in %s on line %d

*** Trying float value
*** Caught {closure}(): Argument #1 ($b) must be of type bool, float given, called in %s on line %d

*** Trying string value
*** Caught {closure}(): Argument #1 ($b) must be of type bool, string given, called in %s on line %d

*** Trying true value
bool(true)

*** Trying false value
bool(false)

*** Trying null value
*** Caught {closure}(): Argument #1 ($b) must be of type bool, null given, called in %s on line %d

*** Trying array value
*** Caught {closure}(): Argument #1 ($b) must be of type bool, array given, called in %s on line %d

*** Trying object value
*** Caught {closure}(): Argument #1 ($b) must be of type bool, object given, called in %s on line %d

*** Trying resource value
*** Caught {closure}(): Argument #1 ($b) must be of type bool, resource given, called in %s on line %d

Done