1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineTest\InstantiatorTest; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
use Doctrine\Instantiator\Exception\InvalidArgumentException; |
7
|
|
|
use Doctrine\Instantiator\Exception\UnexpectedValueException; |
8
|
|
|
use Doctrine\Instantiator\Instantiator; |
9
|
|
|
use Doctrine\Instantiator\InstantiatorInterface; |
10
|
|
|
use DoctrineTest\InstantiatorTestAsset\AbstractClassAsset; |
11
|
|
|
use DoctrineTest\InstantiatorTestAsset\ArrayObjectAsset; |
12
|
|
|
use DoctrineTest\InstantiatorTestAsset\ExceptionAsset; |
13
|
|
|
use DoctrineTest\InstantiatorTestAsset\FinalExceptionAsset; |
14
|
|
|
use DoctrineTest\InstantiatorTestAsset\PharExceptionAsset; |
15
|
|
|
use DoctrineTest\InstantiatorTestAsset\SerializableArrayObjectAsset; |
16
|
|
|
use DoctrineTest\InstantiatorTestAsset\SerializableFinalInternalChildAsset; |
17
|
|
|
use DoctrineTest\InstantiatorTestAsset\SimpleSerializableAsset; |
18
|
|
|
use DoctrineTest\InstantiatorTestAsset\SimpleTraitAsset; |
19
|
|
|
use DoctrineTest\InstantiatorTestAsset\UnCloneableAsset; |
20
|
|
|
use DoctrineTest\InstantiatorTestAsset\UnserializeExceptionArrayObjectAsset; |
21
|
|
|
use DoctrineTest\InstantiatorTestAsset\WakeUpNoticesAsset; |
22
|
|
|
use DoctrineTest\InstantiatorTestAsset\XMLReaderAsset; |
23
|
|
|
use Exception; |
24
|
|
|
use PDORow; |
25
|
|
|
use PharException; |
26
|
|
|
use PHPUnit\Framework\TestCase; |
27
|
|
|
use stdClass; |
28
|
|
|
use function str_replace; |
29
|
|
|
use function uniqid; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Tests for {@see \Doctrine\Instantiator\Instantiator} |
33
|
|
|
* |
34
|
|
|
* @covers \Doctrine\Instantiator\Instantiator |
35
|
|
|
*/ |
36
|
|
|
class InstantiatorTest extends TestCase |
37
|
|
|
{ |
38
|
|
|
/** @var Instantiator */ |
39
|
|
|
private $instantiator; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritDoc} |
43
|
|
|
*/ |
44
|
|
|
protected function setUp() : void |
45
|
|
|
{ |
46
|
|
|
parent::setUp(); |
47
|
|
|
|
48
|
|
|
$this->instantiator = new Instantiator(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @dataProvider getInstantiableClasses |
53
|
|
|
*/ |
54
|
|
|
public function testCanInstantiate(string $className) : void |
55
|
|
|
{ |
56
|
|
|
self::assertInstanceOf($className, $this->instantiator->instantiate($className)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @dataProvider getInstantiableClasses |
61
|
|
|
*/ |
62
|
|
|
public function testInstantiatesSeparateInstances(string $className) : void |
63
|
|
|
{ |
64
|
|
|
$instance1 = $this->instantiator->instantiate($className); |
65
|
|
|
$instance2 = $this->instantiator->instantiate($className); |
66
|
|
|
|
67
|
|
|
self::assertEquals($instance1, $instance2); |
68
|
|
|
self::assertNotSame($instance1, $instance2); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testExceptionOnUnSerializationException() : void |
72
|
|
|
{ |
73
|
|
|
$this->expectException(UnexpectedValueException::class); |
74
|
|
|
|
75
|
|
|
$this->instantiator->instantiate(PDORow::class); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @dataProvider getInvalidClassNames |
80
|
|
|
*/ |
81
|
|
|
public function testInstantiationFromNonExistingClass(string $invalidClassName) : void |
82
|
|
|
{ |
83
|
|
|
$this->expectException(InvalidArgumentException::class); |
84
|
|
|
|
85
|
|
|
$this->instantiator->instantiate($invalidClassName); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testInstancesAreNotCloned() : void |
89
|
|
|
{ |
90
|
|
|
$className = 'TemporaryClass' . str_replace('.', '', uniqid('', true)); |
91
|
|
|
|
92
|
|
|
eval('namespace ' . __NAMESPACE__ . '; class ' . $className . '{}'); |
93
|
|
|
|
94
|
|
|
$instance = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className); |
95
|
|
|
|
96
|
|
|
$instance->foo = 'bar'; |
97
|
|
|
|
98
|
|
|
$instance2 = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className); |
99
|
|
|
|
100
|
|
|
self::assertObjectNotHasAttribute('foo', $instance2); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Provides a list of instantiable classes (existing) |
105
|
|
|
* |
106
|
|
|
* @return string[][] |
107
|
|
|
*/ |
108
|
|
|
public function getInstantiableClasses() : array |
109
|
|
|
{ |
110
|
|
|
return [ |
111
|
|
|
[stdClass::class], |
112
|
|
|
[self::class], |
113
|
|
|
[Instantiator::class], |
114
|
|
|
[Exception::class], |
115
|
|
|
[PharException::class], |
116
|
|
|
[SimpleSerializableAsset::class], |
117
|
|
|
[ExceptionAsset::class], |
118
|
|
|
[FinalExceptionAsset::class], |
119
|
|
|
[PharExceptionAsset::class], |
120
|
|
|
[UnCloneableAsset::class], |
121
|
|
|
[XMLReaderAsset::class], |
122
|
|
|
[PharException::class], |
123
|
|
|
[ArrayObject::class], |
124
|
|
|
[ArrayObjectAsset::class], |
125
|
|
|
[SerializableArrayObjectAsset::class], |
126
|
|
|
[WakeUpNoticesAsset::class], |
127
|
|
|
[UnserializeExceptionArrayObjectAsset::class], |
128
|
|
|
[SerializableFinalInternalChildAsset::class], |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Provides a list of instantiable classes (existing) |
134
|
|
|
* |
135
|
|
|
* @return string[][] |
136
|
|
|
*/ |
137
|
|
|
public function getInvalidClassNames() : array |
138
|
|
|
{ |
139
|
|
|
return [ |
140
|
|
|
[self::class . str_replace('.', '', uniqid('', true))], |
141
|
|
|
[InstantiatorInterface::class], |
142
|
|
|
[AbstractClassAsset::class], |
143
|
|
|
[SimpleTraitAsset::class], |
144
|
|
|
]; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|