|
| 1 | +--TEST-- |
| 2 | +Zend: Test object_init_with_constructor() API |
| 3 | +--EXTENSIONS-- |
| 4 | +zend_test |
| 5 | +sysvmsg |
| 6 | +--FILE-- |
| 7 | +<?php |
| 8 | + |
| 9 | +class PrivateUser { |
| 10 | + private function __construct() { |
| 11 | + return new stdClass(); |
| 12 | + } |
| 13 | + public function __destruct() { |
| 14 | + echo 'Destructor for ', __CLASS__, PHP_EOL; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +class ThrowingUser { |
| 19 | + public function __construct() { |
| 20 | + throw new Exception("Don't construct"); |
| 21 | + } |
| 22 | + public function __destruct() { |
| 23 | + echo 'Destructor for ', __CLASS__, PHP_EOL; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +abstract class AbstractClass { |
| 28 | + public function __construct() { |
| 29 | + return new stdClass(); |
| 30 | + } |
| 31 | + public function __destruct() { |
| 32 | + echo 'Destructor for ', __CLASS__, PHP_EOL; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +class TestUserWithConstructorArgs { |
| 37 | + public function __construct(int $int_param, string $string_param) { |
| 38 | + return new stdClass(); |
| 39 | + } |
| 40 | + public function __destruct() { |
| 41 | + echo 'Destructor for ', __CLASS__, PHP_EOL; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +class TestUserWithConstructorNoParams { |
| 46 | + public function __construct() { |
| 47 | + return new stdClass(); |
| 48 | + } |
| 49 | + public function __destruct() { |
| 50 | + echo 'Destructor for ', __CLASS__, PHP_EOL; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +echo "Testing impossible initializations\n"; |
| 55 | +try { |
| 56 | + $o = zend_object_init_with_constructor("_ZendTestInterface"); |
| 57 | + var_dump($o); |
| 58 | + unset($o); |
| 59 | +} catch (\Throwable $e) { |
| 60 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 61 | +} |
| 62 | +try { |
| 63 | + $o = zend_object_init_with_constructor("_ZendTestTrait"); |
| 64 | + var_dump($o); |
| 65 | + unset($o); |
| 66 | +} catch (\Throwable $e) { |
| 67 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 68 | +} |
| 69 | +try { |
| 70 | + $o = zend_object_init_with_constructor("ZendTestUnitEnum"); |
| 71 | + var_dump($o); |
| 72 | + unset($o); |
| 73 | +} catch (\Throwable $e) { |
| 74 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 75 | +} |
| 76 | +try { |
| 77 | + $o = zend_object_init_with_constructor("AbstractClass"); |
| 78 | + var_dump($o); |
| 79 | + unset($o); |
| 80 | +} catch (\Throwable $e) { |
| 81 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 82 | +} |
| 83 | +try { |
| 84 | + $o = zend_object_init_with_constructor("SysvMessageQueue"); |
| 85 | + var_dump($o); |
| 86 | + unset($o); |
| 87 | +} catch (\Throwable $e) { |
| 88 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 89 | +} |
| 90 | +try { |
| 91 | + $o = zend_object_init_with_constructor("PrivateUser"); |
| 92 | + var_dump($o); |
| 93 | + unset($o); |
| 94 | +} catch (\Throwable $e) { |
| 95 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 96 | +} |
| 97 | +try { |
| 98 | + $o = zend_object_init_with_constructor("ThrowingUser"); |
| 99 | + var_dump($o); |
| 100 | + unset($o); |
| 101 | +} catch (\Throwable $e) { |
| 102 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 103 | +} |
| 104 | + |
| 105 | +echo "Testing param passing\n"; |
| 106 | +try { |
| 107 | + $o = zend_object_init_with_constructor("TestUserWithConstructorArgs"); |
| 108 | + var_dump($o); |
| 109 | + unset($o); |
| 110 | +} catch (\Throwable $e) { |
| 111 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 112 | +} |
| 113 | +try { |
| 114 | + $o = zend_object_init_with_constructor("TestUserWithConstructorArgs", "str", 5); |
| 115 | + var_dump($o); |
| 116 | + unset($o); |
| 117 | +} catch (\Throwable $e) { |
| 118 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 119 | +} |
| 120 | +try { |
| 121 | + $o = zend_object_init_with_constructor("TestUserWithConstructorArgs", 5, string_param: "str", unused_param: 15.3); |
| 122 | + var_dump($o); |
| 123 | + unset($o); |
| 124 | +} catch (\Throwable $e) { |
| 125 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 126 | +} |
| 127 | + |
| 128 | +$o = zend_object_init_with_constructor("TestUserWithConstructorArgs", 5, string_param: "str"); |
| 129 | +var_dump($o); |
| 130 | +unset($o); |
| 131 | + |
| 132 | +echo "Passing too many args to constructor\n"; |
| 133 | +$o = zend_object_init_with_constructor("TestUserWithConstructorArgs", 5, "str", 'unused_param'); |
| 134 | +var_dump($o); |
| 135 | +unset($o); |
| 136 | + |
| 137 | +echo "Testing class with defined constructor and no params\n"; |
| 138 | +$o = zend_object_init_with_constructor("TestUserWithConstructorNoParams"); |
| 139 | +var_dump($o); |
| 140 | +unset($o); |
| 141 | +?> |
| 142 | +--EXPECT-- |
| 143 | +Testing impossible initializations |
| 144 | +Error: Cannot instantiate interface _ZendTestInterface |
| 145 | +Error: Cannot instantiate trait _ZendTestTrait |
| 146 | +Error: Cannot instantiate enum ZendTestUnitEnum |
| 147 | +Error: Cannot instantiate abstract class AbstractClass |
| 148 | +Error: Cannot directly construct SysvMessageQueue, use msg_get_queue() instead |
| 149 | +Error: Call to private PrivateUser::__construct() from global scope |
| 150 | +Exception: Don't construct |
| 151 | +Testing param passing |
| 152 | +ArgumentCountError: Too few arguments to function TestUserWithConstructorArgs::__construct(), 0 passed and exactly 2 expected |
| 153 | +TypeError: TestUserWithConstructorArgs::__construct(): Argument #1 ($int_param) must be of type int, string given |
| 154 | +Error: Unknown named parameter $unused_param |
| 155 | +object(TestUserWithConstructorArgs)#1 (0) { |
| 156 | +} |
| 157 | +Destructor for TestUserWithConstructorArgs |
| 158 | +Passing too many args to constructor |
| 159 | +object(TestUserWithConstructorArgs)#1 (0) { |
| 160 | +} |
| 161 | +Destructor for TestUserWithConstructorArgs |
| 162 | +Testing class with defined constructor and no params |
| 163 | +object(TestUserWithConstructorNoParams)#1 (0) { |
| 164 | +} |
| 165 | +Destructor for TestUserWithConstructorNoParams |
0 commit comments