-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathReflectionTypeTest.php
More file actions
80 lines (74 loc) · 3.75 KB
/
Copy pathReflectionTypeTest.php
File metadata and controls
80 lines (74 loc) · 3.75 KB
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
<?php
declare(strict_types=1);
namespace Go\ParserReflection;
use PHPUnit\Framework\TestCase;
class ReflectionTypeTest extends TestCase
{
protected function setUp(): void
{
include_once (__DIR__ . '/Stub/FileWithClasses70.php');
include_once (__DIR__ . '/Stub/FileWithClasses71.php');
include_once (__DIR__ . '/Stub/FileWithClasses80.php');
}
/**
* Testing convertToDisplayType() with native \ReflectionType
*
* We're already testing it with Go\ParserReflection\ReflectionType
* elsewhere.
*/
public function testTypeConvertToDisplayTypeWithNativeType(): void
{
$nativeClassRef = new \ReflectionClass('Go\\ParserReflection\\Stub\\ClassWithPhp70ScalarTypeHints');
$nativeMethodRef = $nativeClassRef->getMethod('acceptsDefaultString');
$this->assertInstanceOf(\ReflectionMethod::class, $nativeMethodRef);
$nativeParamRefArr = $nativeMethodRef->getParameters();
$this->assertCount(2, $nativeParamRefArr);
$this->assertInstanceOf(\ReflectionParameter::class, $nativeParamRefArr[0]);
$nativeTypeRef = $nativeParamRefArr[0]->getType();
$this->assertSame('string', $nativeTypeRef->getName());
$this->assertStringNotContainsString('\\', get_class($nativeTypeRef));
$this->assertInstanceOf(\ReflectionType::class, $nativeTypeRef);
$this->assertSame('string', \Go\ParserReflection\ReflectionType::convertToDisplayType($nativeTypeRef));
}
/**
* Testing convertToDisplayType() with native \ReflectionType
*
* We're already testing it with Go\ParserReflection\ReflectionType
* elsewhere.
*/
public function testTypeConvertToDisplayTypeWithNullableNativeType(): void
{
$nativeClassRef = new \ReflectionClass('Go\\ParserReflection\\Stub\\ClassWithPhp71NullableScalarTypeHints');
$nativeMethodRef = $nativeClassRef->getMethod('acceptsDefaultString');
$this->assertInstanceOf(\ReflectionMethod::class, $nativeMethodRef);
$nativeParamRefArr = $nativeMethodRef->getParameters();
$this->assertCount(2, $nativeParamRefArr);
$this->assertInstanceOf(\ReflectionParameter::class, $nativeParamRefArr[0]);
$nativeTypeRef = $nativeParamRefArr[0]->getType();
$this->assertSame('string', $nativeTypeRef->getName());
$this->assertStringNotContainsString('\\', get_class($nativeTypeRef));
$this->assertInstanceOf(\ReflectionType::class, $nativeTypeRef);
$this->assertSame('?string', \Go\ParserReflection\ReflectionType::convertToDisplayType($nativeTypeRef));
}
/**
* Testing convertToDisplayType() with native \ReflectionType
*
* We're already testing it with Go\ParserReflection\ReflectionType
* elsewhere.
*/
public function testTypeConvertToDisplayTypeImplicitlyUnionNullable(): void
{
$nativeClassRef = new \ReflectionClass('Go\\ParserReflection\\Stub\\ClassWithPhp80Features');
$nativeMethodRef = $nativeClassRef->getMethod('acceptsStringArrayDefaultToNull');
$this->assertInstanceOf(\ReflectionMethod::class, $nativeMethodRef);
$nativeParamRefArr = $nativeMethodRef->getParameters();
$this->assertCount(1, $nativeParamRefArr);
$this->assertInstanceOf(\ReflectionParameter::class, $nativeParamRefArr[0]);
$nativeTypeRef = $nativeParamRefArr[0]->getType();
$this->assertTrue($nativeTypeRef->allowsNull());
$this->assertSame('array|string|null', (string) $nativeTypeRef);
$this->assertStringNotContainsString('\\', get_class($nativeTypeRef));
$this->assertInstanceOf(\ReflectionType::class, $nativeTypeRef);
$this->assertSame('array|string|null', \Go\ParserReflection\ReflectionType::convertToDisplayType($nativeTypeRef));
}
}