1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace DoctrineTest\InstantiatorTest; |
21
|
|
|
|
22
|
|
|
use Doctrine\Instantiator\Exception\UnexpectedValueException; |
23
|
|
|
use Doctrine\Instantiator\Instantiator; |
24
|
|
|
use PHPUnit_Framework_TestCase; |
25
|
|
|
use ReflectionClass; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Tests for {@see \Doctrine\Instantiator\Instantiator} |
29
|
|
|
* |
30
|
|
|
* @author Marco Pivetta <[email protected]> |
31
|
|
|
* |
32
|
|
|
* @covers \Doctrine\Instantiator\Instantiator |
33
|
|
|
*/ |
34
|
|
|
class InstantiatorTest extends PHPUnit_Framework_TestCase |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var Instantiator |
38
|
|
|
*/ |
39
|
|
|
private $instantiator; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritDoc} |
43
|
|
|
*/ |
44
|
|
|
protected function setUp() |
45
|
|
|
{ |
46
|
|
|
$this->instantiator = new Instantiator(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $className |
51
|
|
|
* |
52
|
|
|
* @dataProvider getInstantiableClasses |
53
|
|
|
*/ |
54
|
|
|
public function testCanInstantiate($className) |
55
|
|
|
{ |
56
|
|
|
$this->assertInstanceOf($className, $this->instantiator->instantiate($className)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $className |
61
|
|
|
* |
62
|
|
|
* @dataProvider getInstantiableClasses |
63
|
|
|
*/ |
64
|
|
|
public function testInstantiatesSeparateInstances($className) |
65
|
|
|
{ |
66
|
|
|
$instance1 = $this->instantiator->instantiate($className); |
67
|
|
|
$instance2 = $this->instantiator->instantiate($className); |
68
|
|
|
|
69
|
|
|
$this->assertEquals($instance1, $instance2); |
70
|
|
|
$this->assertNotSame($instance1, $instance2); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testExceptionOnUnSerializationException() |
74
|
|
|
{ |
75
|
|
|
if (defined('HHVM_VERSION')) { |
76
|
|
|
$this->markTestSkipped( |
77
|
|
|
'As of facebook/hhvm#3432, HHVM has no PDORow, and therefore ' |
78
|
|
|
. ' no internal final classes that cannot be instantiated' |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->setExpectedException('Doctrine\\Instantiator\\Exception\\UnexpectedValueException'); |
83
|
|
|
|
84
|
|
|
$this->instantiator->instantiate(\PDORow::class); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $invalidClassName |
89
|
|
|
* |
90
|
|
|
* @dataProvider getInvalidClassNames |
91
|
|
|
*/ |
92
|
|
|
public function testInstantiationFromNonExistingClass($invalidClassName) |
93
|
|
|
{ |
94
|
|
|
$this->setExpectedException('Doctrine\\Instantiator\\Exception\\InvalidArgumentException'); |
95
|
|
|
|
96
|
|
|
$this->instantiator->instantiate($invalidClassName); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testInstancesAreNotCloned() |
100
|
|
|
{ |
101
|
|
|
$className = 'TemporaryClass' . uniqid(); |
102
|
|
|
|
103
|
|
|
eval('namespace ' . __NAMESPACE__ . '; class ' . $className . '{}'); |
104
|
|
|
|
105
|
|
|
$instance = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className); |
106
|
|
|
|
107
|
|
|
$instance->foo = 'bar'; |
108
|
|
|
|
109
|
|
|
$instance2 = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className); |
110
|
|
|
|
111
|
|
|
$this->assertObjectNotHasAttribute('foo', $instance2); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Provides a list of instantiable classes (existing) |
116
|
|
|
* |
117
|
|
|
* @return string[][] |
118
|
|
|
*/ |
119
|
|
|
public function getInstantiableClasses() |
120
|
|
|
{ |
121
|
|
|
return array( |
122
|
|
|
array('stdClass'), |
123
|
|
|
array(__CLASS__), |
124
|
|
|
array('Doctrine\\Instantiator\\Instantiator'), |
125
|
|
|
array('Exception'), |
126
|
|
|
array('PharException'), |
127
|
|
|
array('DoctrineTest\\InstantiatorTestAsset\\SimpleSerializableAsset'), |
128
|
|
|
array('DoctrineTest\\InstantiatorTestAsset\\ExceptionAsset'), |
129
|
|
|
array('DoctrineTest\\InstantiatorTestAsset\\FinalExceptionAsset'), |
130
|
|
|
array('DoctrineTest\\InstantiatorTestAsset\\PharExceptionAsset'), |
131
|
|
|
array('DoctrineTest\\InstantiatorTestAsset\\UnCloneableAsset'), |
132
|
|
|
array('DoctrineTest\\InstantiatorTestAsset\\XMLReaderAsset'), |
133
|
|
|
array('PharException'), |
134
|
|
|
array('ArrayObject'), |
135
|
|
|
array('DoctrineTest\\InstantiatorTestAsset\\ArrayObjectAsset'), |
136
|
|
|
array('DoctrineTest\\InstantiatorTestAsset\\SerializableArrayObjectAsset'), |
137
|
|
|
array('DoctrineTest\\InstantiatorTestAsset\\WakeUpNoticesAsset'), |
138
|
|
|
array('DoctrineTest\\InstantiatorTestAsset\\UnserializeExceptionArrayObjectAsset'), |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Provides a list of instantiable classes (existing) |
144
|
|
|
* |
145
|
|
|
* @return string[][] |
146
|
|
|
*/ |
147
|
|
|
public function getInvalidClassNames() |
148
|
|
|
{ |
149
|
|
|
return array( |
150
|
|
|
array(__CLASS__ . uniqid()), |
151
|
|
|
array('Doctrine\\Instantiator\\InstantiatorInterface'), |
152
|
|
|
array('DoctrineTest\\InstantiatorTestAsset\\AbstractClassAsset'), |
153
|
|
|
array('DoctrineTest\\InstantiatorTestAsset\\SimpleTraitAsset') |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|