summaryrefslogtreecommitdiff
path: root/Zend/tests/type_declarations/scalar_strict_basic.phpt
blob: 87da5f932807b382655b03bd8295ce269a9afa63 (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
171
172
173
174
175
176
177
178
179
180
181
--TEST--
Strict scalar type basics
--FILE--
<?php

declare(strict_types=1);

$errnames = [
    E_NOTICE => 'E_NOTICE',
    E_WARNING => 'E_WARNING',
    E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR'
];

set_error_handler(function (int $errno, string $errmsg, string $file, int $line) use ($errnames) {
    echo "$errnames[$errno]: $errmsg on line $line\n";
    return true;
});

$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 Argument 1 passed to {closure}() must be of the type int, float given, called in %s on line %d

*** Trying string value
*** Caught Argument 1 passed to {closure}() must be of the type int, string given, called in %s on line %d

*** Trying true value
*** Caught Argument 1 passed to {closure}() must be of the type int, bool given, called in %s on line %d

*** Trying false value
*** Caught Argument 1 passed to {closure}() must be of the type int, bool given, called in %s on line %d

*** Trying null value
*** Caught Argument 1 passed to {closure}() must be of the type int, null given, called in %s on line %d

*** Trying array value
*** Caught Argument 1 passed to {closure}() must be of the type int, array given, called in %s on line %d

*** Trying object value
*** Caught Argument 1 passed to {closure}() must be of the type int, object given, called in %s on line %d

*** Trying resource value
*** Caught Argument 1 passed to {closure}() must be of the 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 Argument 1 passed to {closure}() must be of the type float, string given, called in %s on line %d

*** Trying true value
*** Caught Argument 1 passed to {closure}() must be of the type float, bool given, called in %s on line %d

*** Trying false value
*** Caught Argument 1 passed to {closure}() must be of the type float, bool given, called in %s on line %d

*** Trying null value
*** Caught Argument 1 passed to {closure}() must be of the type float, null given, called in %s on line %d

*** Trying array value
*** Caught Argument 1 passed to {closure}() must be of the type float, array given, called in %s on line %d

*** Trying object value
*** Caught Argument 1 passed to {closure}() must be of the type float, object given, called in %s on line %d

*** Trying resource value
*** Caught Argument 1 passed to {closure}() must be of the type float, resource given, called in %s on line %d

Testing 'string' type:

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

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

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

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

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

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

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

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

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

Testing 'bool' type:

*** Trying integer value
*** Caught Argument 1 passed to {closure}() must be of the type bool, int given, called in %s on line %d

*** Trying float value
*** Caught Argument 1 passed to {closure}() must be of the type bool, float given, called in %s on line %d

*** Trying string value
*** Caught Argument 1 passed to {closure}() must be of the type bool, string given, called in %s on line %d

*** Trying true value
bool(true)

*** Trying false value
bool(false)

*** Trying null value
*** Caught Argument 1 passed to {closure}() must be of the type bool, null given, called in %s on line %d

*** Trying array value
*** Caught Argument 1 passed to {closure}() must be of the type bool, array given, called in %s on line %d

*** Trying object value
*** Caught Argument 1 passed to {closure}() must be of the type bool, object given, called in %s on line %d

*** Trying resource value
*** Caught Argument 1 passed to {closure}() must be of the type bool, resource given, called in %s on line %d

Done