Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Zend/tests/cast_operators_chaining.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Cast operators chaining and precedence
--FILE--
<?php

var_dump((?string) (?int) "123");
var_dump((!float) (!int) "456");

var_dump((?int) null ?? 100);
var_dump(((?int) null) ?? 100);

var_dump((!int) "5" + (!int) "10");
var_dump((!int) "20" * 2);

?>
--EXPECT--
string(3) "123"
float(456)
int(100)
int(100)
int(15)
int(40)
41 changes: 41 additions & 0 deletions Zend/tests/cast_operators_expressions.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
Cast operators with complex expressions
--FILE--
<?php

var_dump((?int) (1 + 2));
var_dump((!int) (3 * 4));

function getValue() {
return "123";
}

var_dump((?int) getValue());
var_dump((!int) getValue());

$x = null;
$result = ((?int) $x) ?? 10;
var_dump($result);

$arr = [
(?int) null,
(?int) "789",
(!string) 123
];
var_dump($arr);

?>
--EXPECT--
int(3)
int(12)
int(123)
int(123)
int(10)
array(3) {
[0]=>
NULL
[1]=>
int(789)
[2]=>
string(3) "123"
}
43 changes: 43 additions & 0 deletions Zend/tests/cast_operators_numeric_strings.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Cast operators with numeric strings
--FILE--
<?php

var_dump((?int) "123");
var_dump((!int) "123");
var_dump((?float) "45.67");
var_dump((!float) "45.67");

var_dump((?int) " 89 ");
var_dump((!int) " 89 ");

try {
var_dump((?int) "123abc");
} catch (TypeError $e) {
echo "Caught: " . $e->getMessage() . "\n";
}

try {
var_dump((!float) "12.5xyz");
} catch (TypeError $e) {
echo "Caught: " . $e->getMessage() . "\n";
}

// Completely non-numeric - should fail
try {
var_dump((?int) "hello");
} catch (TypeError $e) {
echo "Caught: " . $e->getMessage() . "\n";
}

?>
--EXPECT--
int(123)
int(123)
float(45.67)
float(45.67)
int(89)
int(89)
Caught: Cannot cast string to int
Caught: Cannot cast string to float
Caught: Cannot cast string to int
37 changes: 37 additions & 0 deletions Zend/tests/cast_operators_strict_types.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Nullable and non-null casts ignore strict_types declaration
--FILE--
<?php
declare(strict_types=1);

var_dump((?int) "123");
var_dump((!int) "456");

try {
var_dump((?int) "123aze");
} catch (TypeError $e) {
echo "Caught: " . $e->getMessage() . "\n";
}

try {
var_dump((!int) "abc");
} catch (TypeError $e) {
echo "Caught: " . $e->getMessage() . "\n";
}

// Null handling
var_dump((?int) null);
try {
var_dump((!int) null);
} catch (TypeError $e) {
echo "Caught: " . $e->getMessage() . "\n";
}

?>
--EXPECT--
int(123)
int(456)
Caught: Cannot cast string to int
Caught: Cannot cast string to int
NULL
Caught: Cannot cast null to int
38 changes: 38 additions & 0 deletions Zend/tests/cast_operators_variables.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
Cast operators with variables
--FILE--
<?php

$nullVar = null;
$intVar = 42;
$stringVar = "123";
$invalidStringVar = "abc";

var_dump((?int) $nullVar);
var_dump((?int) $intVar);
var_dump((?int) $stringVar);

try {
var_dump((!int) $nullVar);
} catch (TypeError $e) {
echo "Caught TypeError for null\n";
}

var_dump((!int) $intVar);
var_dump((!int) $stringVar);

try {
var_dump((!int) $invalidStringVar);
} catch (TypeError $e) {
echo "Caught TypeError for invalid string\n";
}

?>
--EXPECT--
NULL
int(42)
int(123)
Caught TypeError for null
int(42)
int(123)
Caught TypeError for invalid string
26 changes: 26 additions & 0 deletions Zend/tests/nonnull_array_cast_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Non-null array cast: basic functionality
--FILE--
<?php

var_dump((!array) [1, 2, 3]);

class Foo {
public $bar = "baz";
}
var_dump((!array) new Foo());

?>
--EXPECT--
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
array(1) {
["bar"]=>
string(3) "baz"
}
15 changes: 15 additions & 0 deletions Zend/tests/nonnull_array_cast_null.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Non-null array cast: null throws TypeError
--FILE--
<?php

try {
var_dump((!array) null);
echo "Should have thrown TypeError\n";
} catch (TypeError $e) {
echo "TypeError: " . $e->getMessage() . "\n";
}

?>
--EXPECT--
TypeError: Cannot cast null to array
26 changes: 26 additions & 0 deletions Zend/tests/nonnull_bool_cast_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Non-null bool cast: basic functionality
--FILE--
<?php

var_dump((!bool) true);
var_dump((!bool) false);

var_dump((!bool) 0);
var_dump((!bool) 1);
var_dump((!bool) 42);

var_dump((!bool) "");
var_dump((!bool) "0");
var_dump((!bool) "hello");

?>
--EXPECT--
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
15 changes: 15 additions & 0 deletions Zend/tests/nonnull_bool_cast_null.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Non-null bool cast: null throws TypeError
--FILE--
<?php

try {
var_dump((!bool) null);
echo "Should have thrown TypeError\n";
} catch (TypeError $e) {
echo "TypeError: " . $e->getMessage() . "\n";
}

?>
--EXPECT--
TypeError: Cannot cast null to bool
21 changes: 21 additions & 0 deletions Zend/tests/nonnull_float_cast_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Non-null float cast: basic functionality
--FILE--
<?php

var_dump((!float) 12.34);

var_dump((!float) 56);

var_dump((!float) "78.9");

var_dump((!float) true);
var_dump((!float) false);

?>
--EXPECT--
float(12.34)
float(56)
float(78.9)
float(1)
float(0)
15 changes: 15 additions & 0 deletions Zend/tests/nonnull_float_cast_null.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Non-null float cast: null throws TypeError
--FILE--
<?php

try {
var_dump((!float) null);
echo "Should have thrown TypeError\n";
} catch (TypeError $e) {
echo "TypeError: " . $e->getMessage() . "\n";
}

?>
--EXPECT--
TypeError: Cannot cast null to float
23 changes: 23 additions & 0 deletions Zend/tests/nonnull_int_cast_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Non-null int cast: basic functionality
--FILE--
<?php

var_dump((!int) 123);

var_dump((!int) "456");

var_dump((!int) 78.9);

var_dump((!int) true);
var_dump((!int) false);

?>
--EXPECTF--
int(123)
int(456)

Deprecated: Implicit conversion from float 78.9 to int loses precision in %s on line %d
int(78)
int(1)
int(0)
21 changes: 21 additions & 0 deletions Zend/tests/nonnull_int_cast_invalid.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Non-null int cast: invalid string throws TypeError
--FILE--
<?php

try {
var_dump((!int) "123aze");
} catch (TypeError $e) {
echo "TypeError: " . $e->getMessage() . "\n";
}

try {
var_dump((!int) "abc");
} catch (TypeError $e) {
echo "TypeError: " . $e->getMessage() . "\n";
}

?>
--EXPECTF--
TypeError: Cannot cast string to int
TypeError: Cannot cast string to int
14 changes: 14 additions & 0 deletions Zend/tests/nonnull_int_cast_null.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Non-null int cast: null throws TypeError
--FILE--
<?php

try {
var_dump((!int) null);
} catch (TypeError $e) {
echo "TypeError: " . $e->getMessage() . "\n";
}

?>
--EXPECT--
TypeError: Cannot cast null to int
22 changes: 22 additions & 0 deletions Zend/tests/nonnull_object_cast_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Non-null object cast: basic functionality
--FILE--
<?php

class Foo {
public $bar = "baz";
}
var_dump((!object) new Foo());

var_dump((!object) ["key" => "value"]);

?>
--EXPECTF--
object(Foo)#%d (1) {
["bar"]=>
string(3) "baz"
}
object(stdClass)#%d (1) {
["key"]=>
string(5) "value"
}
Loading