|
| 1 | +--TEST-- |
| 2 | +Lazy objects: initializer must return the right type |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +class B { |
| 7 | + public int $b; |
| 8 | + public function __construct() { |
| 9 | + $this->b = 1; |
| 10 | + } |
| 11 | + public function __destruct() { |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +class C extends B { |
| 16 | +} |
| 17 | + |
| 18 | +class D extends C { |
| 19 | + public int $b; // override |
| 20 | +} |
| 21 | + |
| 22 | +class E extends B { |
| 23 | + public function __destruct() { // override |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +print "# Ghost initializer must return NULL or no value:\n"; |
| 28 | + |
| 29 | +$obj = (new ReflectionClass(C::class))->newLazyGhost(function ($obj) { |
| 30 | + var_dump("initializer"); |
| 31 | + $obj->__construct(); |
| 32 | + return new stdClass; |
| 33 | +}); |
| 34 | + |
| 35 | +var_dump($obj); |
| 36 | +try { |
| 37 | + var_dump($obj->a); |
| 38 | +} catch (\Error $e) { |
| 39 | + printf("%s: %s\n", $e::class, $e->getMessage()); |
| 40 | +} |
| 41 | +var_dump($obj); |
| 42 | + |
| 43 | +print "# Virtual initializer must return an instance of a compatible class:\n"; |
| 44 | +print "## Valid cases:\n"; |
| 45 | + |
| 46 | +$tests = [ |
| 47 | + [C::class, new C()], |
| 48 | + [C::class, new B()], |
| 49 | + [D::class, new B()], |
| 50 | +]; |
| 51 | + |
| 52 | +foreach ($tests as [$class, $instance]) { |
| 53 | + $reflector = new ReflectionClass($class); |
| 54 | + $obj = $reflector->newLazyProxy(function ($obj) use ($instance) { |
| 55 | + var_dump("initializer"); |
| 56 | + $instance->b = 1; |
| 57 | + return $instance; |
| 58 | + }); |
| 59 | + |
| 60 | + printf("## %s vs %s\n", get_class($obj), is_object($instance) ? get_class($instance) : gettype($instance)); |
| 61 | + var_dump($obj->b); |
| 62 | + var_dump($obj); |
| 63 | +} |
| 64 | + |
| 65 | +print "## Invalid cases:\n"; |
| 66 | + |
| 67 | +$tests = [ |
| 68 | + [C::class, new stdClass], |
| 69 | + [C::class, new DateTime()], |
| 70 | + [C::class, null], |
| 71 | + [C::class, new D()], |
| 72 | + [E::class, new B()], |
| 73 | +]; |
| 74 | + |
| 75 | +foreach ($tests as [$class, $instance]) { |
| 76 | + $obj = (new ReflectionClass($class))->newInstanceWithoutConstructor(); |
| 77 | + (new ReflectionClass($obj))->resetAsLazyProxy($obj, function ($obj) use ($instance) { |
| 78 | + var_dump("initializer"); |
| 79 | + return $instance; |
| 80 | + }); |
| 81 | + |
| 82 | + try { |
| 83 | + printf("## %s vs %s\n", get_class($obj), is_object($instance) ? get_class($instance) : gettype($instance)); |
| 84 | + var_dump($obj->a); |
| 85 | + } catch (\Error $e) { |
| 86 | + printf("%s: %s\n", $e::class, $e->getMessage()); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +$obj = (new ReflectionClass(C::class))->newLazyProxy(function ($obj) { |
| 91 | + var_dump("initializer"); |
| 92 | + return $obj; |
| 93 | +}); |
| 94 | + |
| 95 | +try { |
| 96 | + printf("## %s vs itself\n", get_class($obj)); |
| 97 | + var_dump($obj->a); |
| 98 | +} catch (\Error $e) { |
| 99 | + printf("%s: %s\n", $e::class, $e->getMessage()); |
| 100 | +} |
| 101 | + |
| 102 | +--EXPECTF-- |
| 103 | +# Ghost initializer must return NULL or no value: |
| 104 | +lazy ghost object(C)#%d (0) { |
| 105 | + ["b"]=> |
| 106 | + uninitialized(int) |
| 107 | +} |
| 108 | +string(11) "initializer" |
| 109 | +TypeError: Lazy object initializer must return NULL or no value |
| 110 | +lazy ghost object(C)#%d (0) { |
| 111 | + ["b"]=> |
| 112 | + uninitialized(int) |
| 113 | +} |
| 114 | +# Virtual initializer must return an instance of a compatible class: |
| 115 | +## Valid cases: |
| 116 | +## C vs C |
| 117 | +string(11) "initializer" |
| 118 | +int(1) |
| 119 | +lazy proxy object(C)#%d (1) { |
| 120 | + ["instance"]=> |
| 121 | + object(C)#%d (1) { |
| 122 | + ["b"]=> |
| 123 | + int(1) |
| 124 | + } |
| 125 | +} |
| 126 | +## C vs B |
| 127 | +string(11) "initializer" |
| 128 | +int(1) |
| 129 | +lazy proxy object(C)#%d (1) { |
| 130 | + ["instance"]=> |
| 131 | + object(B)#%d (1) { |
| 132 | + ["b"]=> |
| 133 | + int(1) |
| 134 | + } |
| 135 | +} |
| 136 | +## D vs B |
| 137 | +string(11) "initializer" |
| 138 | +int(1) |
| 139 | +lazy proxy object(D)#%d (1) { |
| 140 | + ["instance"]=> |
| 141 | + object(B)#%d (1) { |
| 142 | + ["b"]=> |
| 143 | + int(1) |
| 144 | + } |
| 145 | +} |
| 146 | +## Invalid cases: |
| 147 | +## C vs stdClass |
| 148 | +string(11) "initializer" |
| 149 | +TypeError: The real instance class stdClass is not compatible with the proxy class C. The proxy must be a instance of the same class as the real instance, or a sub-class with no additional properties, and no overrides of the __destructor or __clone methods. |
| 150 | +## C vs DateTime |
| 151 | +string(11) "initializer" |
| 152 | +TypeError: The real instance class DateTime is not compatible with the proxy class C. The proxy must be a instance of the same class as the real instance, or a sub-class with no additional properties, and no overrides of the __destructor or __clone methods. |
| 153 | +## C vs NULL |
| 154 | +string(11) "initializer" |
| 155 | +TypeError: The real instance class null is not compatible with the proxy class C. The proxy must be a instance of the same class as the real instance, or a sub-class with no additional properties, and no overrides of the __destructor or __clone methods. |
| 156 | +## C vs D |
| 157 | +string(11) "initializer" |
| 158 | +TypeError: The real instance class D is not compatible with the proxy class C. The proxy must be a instance of the same class as the real instance, or a sub-class with no additional properties, and no overrides of the __destructor or __clone methods. |
| 159 | +## E vs B |
| 160 | +string(11) "initializer" |
| 161 | +TypeError: The real instance class B is not compatible with the proxy class E. The proxy must be a instance of the same class as the real instance, or a sub-class with no additional properties, and no overrides of the __destructor or __clone methods. |
| 162 | +## C vs itself |
| 163 | +string(11) "initializer" |
| 164 | +Error: Lazy proxy factory must return a non-lazy object |
0 commit comments