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
|
<?php
namespace Mockery\Tests\Unit\PHP82;
use Generator;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\Reflector;
use ReflectionType;
/**
* @requires PHP 8.2.0-dev
*/
class Php82LanguageFeaturesTest extends MockeryTestCase
{
/**
* @param class-string $fullyQualifiedClassName
* @dataProvider parameterContraVarianceDataProvider
*/
public function testMockParameterDisjunctiveNormalFormTypes(string $fullyQualifiedClassName): void
{
$expectedReflectionClass = new \ReflectionClass($fullyQualifiedClassName);
$expectedMethod = $expectedReflectionClass->getMethods()[0];
$expectedType = $expectedMethod
->getParameters()[0]
->getType();
$mock = mock($fullyQualifiedClassName);
$reflectionClass = new \ReflectionClass($mock);
$type = $reflectionClass->getMethod($expectedMethod->getName())
->getParameters()[0]
->getType();
self::assertSame($expectedType->__toString(), $type->__toString());
}
/**
* @param class-string $fullyQualifiedClassName
* @dataProvider returnCoVarianceDataProvider
*/
public function testMockReturnDisjunctiveNormalFormTypes(string $fullyQualifiedClassName): void
{
$expectedReflectionClass = new \ReflectionClass($fullyQualifiedClassName);
$expectedMethod = $expectedReflectionClass->getMethods()[0];
$expectedType = $expectedMethod->getReturnType();
self::assertInstanceOf(ReflectionType::class, $expectedType);
$mock = mock($fullyQualifiedClassName);
$reflectionClass = new \ReflectionClass($mock);
$type = $reflectionClass->getMethod($expectedMethod->getName())
->getReturnType();
self::assertInstanceOf(ReflectionType::class, $type);
self::assertSame($expectedType->__toString(), $type->__toString());
}
public static function parameterContraVarianceDataProvider(): Generator
{
$fixtures = [
Sut::class,
TestOne::class,
TestTwo::class,
TestThree::class,
];
foreach ($fixtures as $fixture) {
yield $fixture => [$fixture];
}
}
public static function returnCoVarianceDataProvider(): Generator
{
$fixtures = [
TestReturnCoVarianceOne::class,
TestReturnCoVarianceTwo::class,
TestReturnCoVarianceThree::class,
];
foreach ($fixtures as $fixture) {
yield $fixture => [$fixture];
}
}
public function testTypeHintIterableObject(): void
{
$refClass = new \ReflectionClass(IterableObject::class);
$refMethod = $refClass->getMethods()[0];
$refParam = $refMethod->getParameters()[0];
self::assertSame(
'iterable|object',
Reflector::getTypeHint($refParam)
);
}
public function testTypeHintIterableObjectString(): void
{
$refClass = new \ReflectionClass(IterableObjectString::class);
$refMethod = $refClass->getMethods()[0];
$refParam = $refMethod->getParameters()[0];
self::assertSame(
'iterable|object|string',
Reflector::getTypeHint($refParam)
);
}
public function testTypeHintIIterableStdClassString(): void
{
$refClass = new \ReflectionClass(IterableStdClassString::class);
$refMethod = $refClass->getMethods()[0];
$refParam = $refMethod->getParameters()[0];
self::assertSame(
'iterable|\stdClass|string',
Reflector::getTypeHint($refParam)
);
}
}
class IterableObject
{
public function __invoke(iterable|object $arg): void
{
}
}
class IterableObjectString
{
public function __invoke(iterable|object|string $arg): void
{
}
}
class IterableStdClassString
{
public function __invoke(iterable|\stdClass|string $arg): void
{
}
}
/**
* The test fixtures in this directory have been directly copied and pasted from the source mentioned,
* which is the PHP RFC (Request for Comments) titled "DNF Types" available at https://wiki.php.net/rfc/dnf_types.
*
* Please note that the copyrights for these test fixtures belong to:
*
* Copyright (c) George Peter Banyard <girgias@php.net>
* Copyright (c) Larry Garfield <crell@php.net>
*
* All rights are reserved by the respective authors.
*/
interface A
{
}
interface B
{
}
interface C extends A, B
{
}
interface D
{
}
class W implements A
{
}
class X implements B
{
}
class Y implements C
{
}
class Z extends Y implements D
{
}
class Sut
{
public function foo(A|(B&C)$arg)
{
var_dump($arg);
}
}
interface ITest {
public function stuff((A&B)|D $arg): void;
}
// Acceptable. Everything that ITest accepts is still valid
// and then some.
class TestOne implements ITest {
public function stuff((A&B)|D|Z $arg): void {}
}
// Acceptable. This accepts objects that implement just
// A, which is a super-set of those that implement A&B.
class TestTwo implements ITest {
public function stuff(A|D $arg): void {}
}
interface ITestTwo {
public function things(C|D $arg): void;
}
// Anything that implements C implements A&B,
// but this rule also allows classes that implement A&B
// directly, and thus is wider.
class TestThree implements ITestTwo {
public function things((A&B)|D $arg): void
{
}
}
interface IReturnCoVarianceTest
{
public function stuff(): (A&B)|D;
}
// A&B is more restrictive.
class TestReturnCoVarianceOne implements IReturnCoVarianceTest {
public function stuff(): A&B {
return new Y;
}
}
// D is is a subset of A&B|D
class TestReturnCoVarianceTwo implements IReturnCoVarianceTest {
public function stuff(): D {
return new Z;
}
}
// Since C is a subset of A&B, even though it is not identical.
class TestReturnCoVarianceThree implements IReturnCoVarianceTest {
public function stuff(): C|D {
return new Y;
}
}
|