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
|
<?php
declare(strict_types=1);
namespace ProxyManagerTest\ProxyGenerator\Assertion;
use BadMethodCallException;
use PHPUnit\Framework\TestCase;
use ProxyManager\Exception\InvalidProxiedClassException;
use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion;
use ProxyManagerTestAsset\AccessInterceptorValueHolderMock;
use ProxyManagerTestAsset\BaseClass;
use ProxyManagerTestAsset\BaseInterface;
use ProxyManagerTestAsset\CallableTypeHintClass;
use ProxyManagerTestAsset\ClassWithAbstractProtectedMethod;
use ProxyManagerTestAsset\ClassWithByRefMagicMethods;
use ProxyManagerTestAsset\ClassWithFinalMagicMethods;
use ProxyManagerTestAsset\ClassWithFinalMethods;
use ProxyManagerTestAsset\ClassWithMethodWithDefaultParameters;
use ProxyManagerTestAsset\ClassWithMixedProperties;
use ProxyManagerTestAsset\ClassWithParentHint;
use ProxyManagerTestAsset\ClassWithPrivateProperties;
use ProxyManagerTestAsset\ClassWithProtectedProperties;
use ProxyManagerTestAsset\ClassWithPublicArrayProperty;
use ProxyManagerTestAsset\ClassWithPublicProperties;
use ProxyManagerTestAsset\ClassWithSelfHint;
use ProxyManagerTestAsset\EmptyClass;
use ProxyManagerTestAsset\FinalClass;
use ProxyManagerTestAsset\HydratedObject;
use ProxyManagerTestAsset\LazyLoadingMock;
use ProxyManagerTestAsset\NullObjectMock;
use ReflectionClass;
/**
* Tests for {@see \ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion}
*
* @covers \ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion
* @group Coverage
*/
final class CanProxyAssertionTest extends TestCase
{
public function testDeniesFinalClasses(): void
{
$this->expectException(InvalidProxiedClassException::class);
CanProxyAssertion::assertClassCanBeProxied(new ReflectionClass(FinalClass::class));
}
public function testDeniesClassesWithAbstractProtectedMethods(): void
{
$this->expectException(InvalidProxiedClassException::class);
CanProxyAssertion::assertClassCanBeProxied(new ReflectionClass(
ClassWithAbstractProtectedMethod::class
));
}
public function testAllowsInterfaceByDefault(): void
{
CanProxyAssertion::assertClassCanBeProxied(new ReflectionClass(
BaseInterface::class
));
self::assertTrue(true); // not nice, but assertions are just fail-checks, no real code executed
}
public function testDeniesInterfaceIfSpecified(): void
{
CanProxyAssertion::assertClassCanBeProxied(new ReflectionClass(BaseClass::class), false);
$this->expectException(InvalidProxiedClassException::class);
CanProxyAssertion::assertClassCanBeProxied(new ReflectionClass(BaseInterface::class), false);
}
/**
* @psalm-param class-string $className
*
* @dataProvider validClasses
*/
public function testAllowedClass(string $className): void
{
CanProxyAssertion::assertClassCanBeProxied(new ReflectionClass($className));
self::assertTrue(true); // not nice, but assertions are just fail-checks, no real code executed
}
public function testDisallowsConstructor(): void
{
$this->expectException(BadMethodCallException::class);
new CanProxyAssertion();
}
/**
* @return string[][]
*/
public static function validClasses(): array
{
return [
[AccessInterceptorValueHolderMock::class],
[BaseClass::class],
[BaseInterface::class],
[CallableTypeHintClass::class],
[ClassWithByRefMagicMethods::class],
[ClassWithFinalMagicMethods::class],
[ClassWithFinalMethods::class],
[ClassWithMethodWithDefaultParameters::class],
[ClassWithMixedProperties::class],
[ClassWithPrivateProperties::class],
[ClassWithProtectedProperties::class],
[ClassWithPublicProperties::class],
[ClassWithPublicArrayProperty::class],
[ClassWithSelfHint::class],
[ClassWithParentHint::class],
[EmptyClass::class],
[HydratedObject::class],
[LazyLoadingMock::class],
[NullObjectMock::class],
];
}
}
|