/
InstantiatorTest.php
168 lines (143 loc) · 5.03 KB
/
InstantiatorTest.php
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
<?php
namespace DoctrineTest\InstantiatorTest;
use ArrayObject;
use Doctrine\Instantiator\Exception\InvalidArgumentException;
use Doctrine\Instantiator\Exception\UnexpectedValueException;
use Doctrine\Instantiator\Instantiator;
use Doctrine\Instantiator\InstantiatorInterface;
use DoctrineTest\InstantiatorTestAsset\AbstractClassAsset;
use DoctrineTest\InstantiatorTestAsset\ArrayObjectAsset;
use DoctrineTest\InstantiatorTestAsset\ExceptionAsset;
use DoctrineTest\InstantiatorTestAsset\FinalExceptionAsset;
use DoctrineTest\InstantiatorTestAsset\PharExceptionAsset;
use DoctrineTest\InstantiatorTestAsset\SerializableArrayObjectAsset;
use DoctrineTest\InstantiatorTestAsset\SerializableFinalInternalChildAsset;
use DoctrineTest\InstantiatorTestAsset\SimpleEnumAsset;
use DoctrineTest\InstantiatorTestAsset\SimpleSerializableAsset;
use DoctrineTest\InstantiatorTestAsset\SimpleTraitAsset;
use DoctrineTest\InstantiatorTestAsset\UnCloneableAsset;
use DoctrineTest\InstantiatorTestAsset\UnserializeExceptionArrayObjectAsset;
use DoctrineTest\InstantiatorTestAsset\WakeUpNoticesAsset;
use DoctrineTest\InstantiatorTestAsset\XMLReaderAsset;
use Exception;
use Generator;
use PDORow;
use PharException;
use PHPUnit\Framework\TestCase;
use stdClass;
use function str_replace;
use function uniqid;
use const PHP_VERSION_ID;
/**
* Tests for {@see \Doctrine\Instantiator\Instantiator}
*
* @covers \Doctrine\Instantiator\Instantiator
*/
class InstantiatorTest extends TestCase
{
/** @var Instantiator */
private $instantiator;
protected function setUp(): void
{
parent::setUp();
$this->instantiator = new Instantiator();
}
/**
* @phpstan-param class-string $className
*
* @dataProvider getInstantiableClasses
*/
public function testCanInstantiate(string $className): void
{
self::assertInstanceOf($className, $this->instantiator->instantiate($className));
}
/**
* @phpstan-param class-string $className
*
* @dataProvider getInstantiableClasses
*/
public function testInstantiatesSeparateInstances(string $className): void
{
$instance1 = $this->instantiator->instantiate($className);
$instance2 = $this->instantiator->instantiate($className);
self::assertEquals($instance1, $instance2);
self::assertNotSame($instance1, $instance2);
}
public function testExceptionOnUnSerializationException(): void
{
$this->expectException(UnexpectedValueException::class);
$this->instantiator->instantiate(PDORow::class);
}
/**
* @phpstan-param class-string $invalidClassName
*
* @dataProvider getInvalidClassNames
*/
public function testInstantiationFromNonExistingClass(string $invalidClassName): void
{
$this->expectException(InvalidArgumentException::class);
$this->instantiator->instantiate($invalidClassName);
}
public function testInstancesAreNotCloned(): void
{
$namespace = __NAMESPACE__;
$className = 'TemporaryClass' . str_replace('.', '', uniqid('', true));
eval(<<< PHP
namespace $namespace;
#[\AllowDynamicProperties]
class $className {}
PHP
);
/** @phpstan-var class-string */
$classNameWithNamespace = $namespace . '\\' . $className;
$instance = $this->instantiator->instantiate($classNameWithNamespace);
$instance->foo = 'bar';
$instance2 = $this->instantiator->instantiate($classNameWithNamespace);
self::assertObjectNotHasAttribute('foo', $instance2);
}
/**
* Provides a list of instantiable classes (existing)
*
* @return string[][]
* @phpstan-return list<array{class-string}>
*/
public function getInstantiableClasses(): array
{
return [
[stdClass::class],
[self::class],
[Instantiator::class],
[Exception::class],
[PharException::class],
[SimpleSerializableAsset::class],
[ExceptionAsset::class],
[FinalExceptionAsset::class],
[PharExceptionAsset::class],
[UnCloneableAsset::class],
[XMLReaderAsset::class],
[PharException::class],
[ArrayObject::class],
[ArrayObjectAsset::class],
[SerializableArrayObjectAsset::class],
[WakeUpNoticesAsset::class],
[UnserializeExceptionArrayObjectAsset::class],
[SerializableFinalInternalChildAsset::class],
];
}
/**
* Provides a list of instantiable classes (existing)
*
* @psalm-return Generator<string, array{string}>
*/
public function getInvalidClassNames(): Generator
{
yield 'invalid string' => [self::class . str_replace('.', '', uniqid('', true))];
yield 'interface' => [InstantiatorInterface::class];
yield 'abstract class' => [AbstractClassAsset::class];
yield 'trait' => [SimpleTraitAsset::class];
if (PHP_VERSION_ID < 80100) {
return;
}
yield 'enum' => [SimpleEnumAsset::class];
}
}