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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
<?php
declare(strict_types=1);
namespace ProxyManagerTest\Functional;
use Error;
use PHPUnit\Framework\TestCase;
use ProxyManager\Factory\NullObjectFactory;
use ProxyManager\Proxy\NullObjectInterface;
use ProxyManagerTestAsset\BaseClass;
use ProxyManagerTestAsset\BaseInterface;
use ProxyManagerTestAsset\ClassWithMethodWithByRefVariadicFunction;
use ProxyManagerTestAsset\ClassWithMethodWithVariadicFunction;
use ProxyManagerTestAsset\ClassWithParentHint;
use ProxyManagerTestAsset\ClassWithPublicStringNullableNullDefaultTypedProperty;
use ProxyManagerTestAsset\ClassWithPublicStringNullableTypedProperty;
use ProxyManagerTestAsset\ClassWithPublicStringTypedProperty;
use ProxyManagerTestAsset\ClassWithSelfHint;
use ProxyManagerTestAsset\EmptyClass;
use ProxyManagerTestAsset\NeverCounter;
use ProxyManagerTestAsset\VoidCounter;
use ReflectionProperty;
use stdClass;
use TypeError;
use function array_values;
use function assert;
use function random_int;
use function serialize;
use function uniqid;
use function unserialize;
/**
* Tests for {@see \ProxyManager\ProxyGenerator\NullObjectGenerator} produced objects
*
* @group Functional
* @coversNothing
*/
final class NullObjectFunctionalTest extends TestCase
{
/**
* @param mixed[] $params
* @psalm-param class-string $className
*
* @dataProvider getProxyMethods
*/
public function testMethodCalls(string $className, string $method, array $params): void
{
$proxy = (new NullObjectFactory())->createProxy($className);
$this->assertNullMethodCall($proxy, $method, $params);
}
/**
* @param mixed[] $params
* @psalm-param class-string $className
*
* @dataProvider getProxyMethods
*/
public function testMethodCallsAfterUnSerialization(string $className, string $method, array $params): void
{
$proxy = unserialize(serialize((new NullObjectFactory())->createProxy($className)));
assert($proxy instanceof NullObjectInterface);
$this->assertNullMethodCall($proxy, $method, $params);
}
/**
* @param mixed[] $params
* @psalm-param class-string $className
*
* @dataProvider getProxyMethods
*/
public function testMethodCallsAfterCloning(string $className, string $method, array $params): void
{
$proxy = (new NullObjectFactory())->createProxy($className);
$this->assertNullMethodCall(clone $proxy, $method, $params);
}
/**
* @dataProvider getPropertyAccessProxies
*/
public function testPropertyReadAccess(NullObjectInterface $proxy, string $publicProperty): void
{
if (! $this->propertyHasDefaultNullableValue(new ReflectionProperty($proxy, $publicProperty))) {
// Accessing a typed property without default value before initialization
$this->expectException(Error::class);
$this->expectExceptionMessageMatches('/must not be accessed before initialization/');
}
self::assertNull($proxy->$publicProperty);
}
/**
* @dataProvider getPropertyAccessProxies
*/
public function testPropertyWriteAccess(NullObjectInterface $proxy, string $publicProperty): void
{
$newValue = uniqid('', true);
$proxy->$publicProperty = $newValue;
self::assertSame($newValue, $proxy->$publicProperty);
}
/**
* @dataProvider getPropertyAccessProxies
*/
public function testPropertyExistence(NullObjectInterface $proxy, string $publicProperty): void
{
if (! $this->propertyHasDefaultNullableValue(new ReflectionProperty($proxy, $publicProperty))) {
// Accessing a typed property without default value before initialization
$this->expectException(Error::class);
$this->expectExceptionMessageMatches('/must not be accessed before initialization/');
}
self::assertNull($proxy->$publicProperty);
}
/**
* @dataProvider getPropertyAccessProxies
*/
public function testPropertyUnset(NullObjectInterface $proxy, string $publicProperty): void
{
unset($proxy->$publicProperty);
self::assertFalse(isset($proxy->$publicProperty));
}
/**
* @requires PHP 8.1
*/
public function testNeverReturningMethodCalls(): void
{
$proxy = (new NullObjectFactory())->createProxy(NeverCounter::class);
$method = [$proxy, 'increment'];
self::assertIsCallable($method);
$this->expectException(TypeError::class);
$method(random_int(10, 1000));
}
/**
* Generates a list of object | invoked method | parameters | expected result
*
* @return string[][]|null[][]|mixed[][][]|object[][]
*/
public static function getProxyMethods(): array
{
$selfHintParam = new ClassWithSelfHint();
$empty = new EmptyClass();
return [
[
BaseClass::class,
'publicMethod',
[],
'publicMethodDefault',
],
[
BaseClass::class,
'publicTypeHintedMethod',
['param' => new stdClass()],
'publicTypeHintedMethodDefault',
],
[
BaseClass::class,
'publicByReferenceMethod',
[],
'publicByReferenceMethodDefault',
],
[
BaseInterface::class,
'publicMethod',
[],
'publicMethodDefault',
],
[
ClassWithSelfHint::class,
'selfHintMethod',
['parameter' => $selfHintParam],
$selfHintParam,
],
[
ClassWithParentHint::class,
'parentHintMethod',
['parameter' => $empty],
$empty,
],
[
ClassWithMethodWithVariadicFunction::class,
'buz',
['Ocramius', 'Malukenho'],
null,
],
[
ClassWithMethodWithByRefVariadicFunction::class,
'tuz',
['Ocramius', 'Malukenho'],
null,
],
[
VoidCounter::class,
'increment',
[random_int(10, 1000)],
null,
],
];
}
/**
* Generates proxies and instances with a public property to feed to the property accessor methods
*
* @return array<int, array<int, NullObjectInterface|string|null>>
*/
public static function getPropertyAccessProxies(): array
{
$factory = new NullObjectFactory();
$serialized = unserialize(serialize($factory->createProxy(BaseClass::class)));
assert($serialized instanceof NullObjectInterface);
$tests = [
[
$factory->createProxy(BaseClass::class),
'publicProperty',
'publicPropertyDefault',
],
[
$serialized,
'publicProperty',
'publicPropertyDefault',
],
];
if (\PHP_VERSION_ID < 70400) {
return $tests;
}
return array_merge($tests, [
[
$factory->createProxy(ClassWithPublicStringTypedProperty::class),
'typedProperty',
'Typed property initialized value',
],
[
$factory->createProxy(ClassWithPublicStringNullableTypedProperty::class),
'typedNullableProperty',
'Typed nullable property initialized value',
],
[
$factory->createProxy(ClassWithPublicStringNullableNullDefaultTypedProperty::class),
'typedNullableNullDefaultProperty',
null,
],
]);
}
/**
* @param mixed[] $parameters
*/
private function assertNullMethodCall(NullObjectInterface $proxy, string $methodName, array $parameters): void
{
$method = [$proxy, $methodName];
self::assertIsCallable($method);
$parameterValues = array_values($parameters);
self::assertNull($method(...$parameterValues));
}
private function propertyHasDefaultNullableValue(ReflectionProperty $property): bool
{
$type = \PHP_VERSION_ID >= 70400 ? $property->getType() : null;
return $type === null
|| $type->allowsNull();
}
}
|