Skip to content

Commit

Permalink
#628 added unit tests to verify correct codegen of proxies that exten…
Browse files Browse the repository at this point in the history
…d classes with PHP 8.0 types
  • Loading branch information
martinssipenko authored and Ocramius committed Dec 30, 2020
1 parent 27adab8 commit 7157b41
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 9 deletions.
13 changes: 10 additions & 3 deletions tests/ProxyManagerTest/Functional/MultipleProxyGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ProxyManagerTestAsset\ClassWithMixedReferenceableTypedProperties;
use ProxyManagerTestAsset\ClassWithMixedTypedProperties;
use ProxyManagerTestAsset\ClassWithParentHint;
use ProxyManagerTestAsset\ClassWithPhp80TypedMethods;
use ProxyManagerTestAsset\ClassWithPrivateProperties;
use ProxyManagerTestAsset\ClassWithProtectedProperties;
use ProxyManagerTestAsset\ClassWithPublicProperties;
Expand Down Expand Up @@ -101,7 +102,7 @@ public function testCanGenerateMultipleDifferentProxiesForSameClass(object $obje
*/
public function getTestedClasses(): array
{
return [
$objects = [
[new BaseClass()],
[new ClassWithMagicMethods()],
[new ClassWithFinalMethods()],
Expand All @@ -110,8 +111,8 @@ public function getTestedClasses(): array
[new ClassWithMixedProperties()],
[new ClassWithMixedTypedProperties()],
[new ClassWithMixedReferenceableTypedProperties()],
// [new ClassWithPublicStringTypedProperty()],
// [new ClassWithPublicStringNullableTypedProperty()],
// [new ClassWithPublicStringTypedProperty()],
// [new ClassWithPublicStringNullableTypedProperty()],
[new ClassWithPrivateProperties()],
[new ClassWithProtectedProperties()],
[new ClassWithPublicProperties()],
Expand All @@ -128,5 +129,11 @@ public function getTestedClasses(): array
[new ReturnTypeHintedClass()],
[new VoidMethodTypeHintedClass()],
];

if (PHP_VERSION_ID > 80000) {
$objects[] = [new ClassWithPhp80TypedMethods()];
}

return $objects;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace ProxyManagerTest\ProxyGenerator;

use PHPUnit\Framework\TestCase;
use ProxyManager\Generator\ClassGenerator;
use Laminas\Code\Generator\ClassGenerator;
use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
use ProxyManager\ProxyGenerator\ProxyGeneratorInterface;
Expand All @@ -17,13 +17,15 @@
use ProxyManagerTestAsset\ClassWithMixedProperties;
use ProxyManagerTestAsset\ClassWithMixedReferenceableTypedProperties;
use ProxyManagerTestAsset\ClassWithMixedTypedProperties;
use ProxyManagerTestAsset\ClassWithPhp80TypedMethods;
use ProxyManagerTestAsset\IterableMethodTypeHintedInterface;
use ProxyManagerTestAsset\ObjectMethodTypeHintedInterface;
use ProxyManagerTestAsset\ReturnTypeHintedClass;
use ProxyManagerTestAsset\ReturnTypeHintedInterface;
use ProxyManagerTestAsset\VoidMethodTypeHintedClass;
use ProxyManagerTestAsset\VoidMethodTypeHintedInterface;
use ReflectionClass;
use const PHP_VERSION_ID;

/**
* Base test for proxy generators
Expand Down Expand Up @@ -84,7 +86,7 @@ abstract protected function getExpectedImplementedInterfaces(): array;
/** @return string[][] */
public function getTestedImplementations(): array
{
return [
$implementations = [
[BaseClass::class],
[ClassWithMagicMethods::class],
[ClassWithByRefMagicMethods::class],
Expand All @@ -100,5 +102,11 @@ public function getTestedImplementations(): array
[IterableMethodTypeHintedInterface::class],
[ObjectMethodTypeHintedInterface::class],
];

if (PHP_VERSION_ID > 80000) {
$implementations[] = [ClassWithPhp80TypedMethods::class];
}

return $implementations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ProxyManagerTest\ProxyGenerator;

use ProxyManager\Generator\ClassGenerator;
use Laminas\Code\Generator\ClassGenerator;
use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
use ProxyManager\Proxy\NullObjectInterface;
Expand All @@ -18,6 +18,7 @@
use ProxyManagerTestAsset\ClassWithMixedProperties;
use ProxyManagerTestAsset\ClassWithMixedReferenceableTypedProperties;
use ProxyManagerTestAsset\ClassWithMixedTypedProperties;
use ProxyManagerTestAsset\ClassWithPhp80TypedMethods;
use ReflectionClass;
use ReflectionMethod;

Expand Down Expand Up @@ -109,7 +110,7 @@ protected function getExpectedImplementedInterfaces(): array
*/
public function getTestedImplementations(): array
{
return [
$implementations = [
[BaseClass::class],
[ClassWithMagicMethods::class],
[ClassWithByRefMagicMethods::class],
Expand All @@ -118,5 +119,11 @@ public function getTestedImplementations(): array
[ClassWithMixedReferenceableTypedProperties::class],
[BaseInterface::class],
];

if (PHP_VERSION_ID > 80000) {
$implementations[] = [ClassWithPhp80TypedMethods::class];
}

return $implementations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ProxyManagerTest\ProxyGenerator;

use ProxyManager\Generator\ClassGenerator;
use Laminas\Code\Generator\ClassGenerator;
use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
use ProxyManager\Proxy\RemoteObjectInterface;
Expand All @@ -17,6 +17,7 @@
use ProxyManagerTestAsset\ClassWithMixedProperties;
use ProxyManagerTestAsset\ClassWithMixedReferenceableTypedProperties;
use ProxyManagerTestAsset\ClassWithMixedTypedProperties;
use ProxyManagerTestAsset\ClassWithPhp80TypedMethods;
use ReflectionClass;

use function array_diff;
Expand Down Expand Up @@ -80,7 +81,7 @@ protected function getExpectedImplementedInterfaces(): array
/** @return string[][] */
public function getTestedImplementations(): array
{
return [
$implementations = [
[BaseClass::class],
[ClassWithMagicMethods::class],
[ClassWithByRefMagicMethods::class],
Expand All @@ -89,5 +90,11 @@ public function getTestedImplementations(): array
[ClassWithMixedReferenceableTypedProperties::class],
[BaseInterface::class],
];

if (PHP_VERSION_ID > 80000) {
$implementations[] = [ClassWithPhp80TypedMethods::class];
}

return $implementations;
}
}
54 changes: 54 additions & 0 deletions tests/ProxyManagerTestAsset/ClassWithPhp80TypedMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace ProxyManagerTestAsset;

use BadMethodCallException;

/**
* Base test class to play around with new method type definitions that came with PHP 8.0.0
*/
class ClassWithPhp80TypedMethods
{
public function mixedType(mixed $parameter): mixed
{
throw new BadMethodCallException('Not supposed to be run');
}

/** Note: the false type cannot be used standalone, and must be part of a union type */
public function falseType(false|self $parameter): false|self
{
throw new BadMethodCallException('Not supposed to be run');
}

public function unionNullableType(bool|null $parameter): bool|null
{
throw new BadMethodCallException('Not supposed to be run');
}

public function unionReverseNullableType(null|bool $parameter): null|bool
{
throw new BadMethodCallException('Not supposed to be run');
}

public function unionNullableTypeWithDefaultValue(bool|string|null $parameter = null): bool|string|null
{
throw new BadMethodCallException('Not supposed to be run');
}

public function unionType(ClassWithPhp80TypedMethods|\stdClass $parameter): ClassWithPhp80TypedMethods|\stdClass
{
throw new BadMethodCallException('Not supposed to be run');
}

public function staticType(self $parameter): static
{
throw new BadMethodCallException('Not supposed to be run');
}

public function selfAndBoolType(self|bool $parameter): self|bool
{
throw new BadMethodCallException('Not supposed to be run');
}
}

0 comments on commit 7157b41

Please sign in to comment.