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.
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>.
20
namespace DoctrineTest\InstantiatorTest;
22
use Doctrine\Instantiator\Exception\UnexpectedValueException;
23
use Doctrine\Instantiator\Instantiator;
24
use PHPUnit_Framework_TestCase;
28
* Tests for {@see \Doctrine\Instantiator\Instantiator}
30
* @author Marco Pivetta <ocramius@gmail.com>
32
* @covers \Doctrine\Instantiator\Instantiator
34
class InstantiatorTest extends PHPUnit_Framework_TestCase
39
private $instantiator;
44
protected function setUp()
46
$this->instantiator = new Instantiator();
50
* @param string $className
52
* @dataProvider getInstantiableClasses
54
public function testCanInstantiate($className)
56
$this->assertInstanceOf($className, $this->instantiator->instantiate($className));
60
* @param string $className
62
* @dataProvider getInstantiableClasses
64
public function testInstantiatesSeparateInstances($className)
66
$instance1 = $this->instantiator->instantiate($className);
67
$instance2 = $this->instantiator->instantiate($className);
69
$this->assertEquals($instance1, $instance2);
70
$this->assertNotSame($instance1, $instance2);
73
public function testExceptionOnUnSerializationException()
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'
82
$className = 'DoctrineTest\\InstantiatorTestAsset\\UnserializeExceptionArrayObjectAsset';
84
if (\PHP_VERSION_ID >= 50600) {
85
$className = 'PDORow';
88
if (\PHP_VERSION_ID === 50429 || \PHP_VERSION_ID === 50513) {
89
$className = 'DoctrineTest\\InstantiatorTestAsset\\SerializableArrayObjectAsset';
92
$this->setExpectedException('Doctrine\\Instantiator\\Exception\\UnexpectedValueException');
94
$this->instantiator->instantiate($className);
97
public function testNoticeOnUnSerializationException()
99
if (\PHP_VERSION_ID >= 50600) {
100
$this->markTestSkipped(
101
'PHP 5.6 supports `ReflectionClass#newInstanceWithoutConstructor()` for some internal classes'
106
$this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\WakeUpNoticesAsset');
108
$this->fail('No exception was raised');
109
} catch (UnexpectedValueException $exception) {
110
$wakeUpNoticesReflection = new ReflectionClass('DoctrineTest\\InstantiatorTestAsset\\WakeUpNoticesAsset');
111
$previous = $exception->getPrevious();
113
$this->assertInstanceOf('Exception', $previous);
115
// in PHP 5.4.29 and PHP 5.5.13, this case is not a notice, but an exception being thrown
116
if (! (\PHP_VERSION_ID === 50429 || \PHP_VERSION_ID === 50513)) {
118
'Could not produce an instance of "DoctrineTest\\InstantiatorTestAsset\WakeUpNoticesAsset" '
119
. 'via un-serialization, since an error was triggered in file "'
120
. $wakeUpNoticesReflection->getFileName() . '" at line "36"',
121
$exception->getMessage()
124
$this->assertSame('Something went bananas while un-serializing this instance', $previous->getMessage());
125
$this->assertSame(\E_USER_NOTICE, $previous->getCode());
131
* @param string $invalidClassName
133
* @dataProvider getInvalidClassNames
135
public function testInstantiationFromNonExistingClass($invalidClassName)
137
$this->setExpectedException('Doctrine\\Instantiator\\Exception\\InvalidArgumentException');
139
$this->instantiator->instantiate($invalidClassName);
142
public function testInstancesAreNotCloned()
144
$className = 'TemporaryClass' . uniqid();
146
eval('namespace ' . __NAMESPACE__ . '; class ' . $className . '{}');
148
$instance = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className);
150
$instance->foo = 'bar';
152
$instance2 = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className);
154
$this->assertObjectNotHasAttribute('foo', $instance2);
158
* Provides a list of instantiable classes (existing)
162
public function getInstantiableClasses()
167
array('Doctrine\\Instantiator\\Instantiator'),
168
array('PharException'),
169
array('DoctrineTest\\InstantiatorTestAsset\\SimpleSerializableAsset'),
170
array('DoctrineTest\\InstantiatorTestAsset\\PharExceptionAsset'),
171
array('DoctrineTest\\InstantiatorTestAsset\\UnCloneableAsset'),
172
array('DoctrineTest\\InstantiatorTestAsset\\XMLReaderAsset'),
175
if (\PHP_VERSION_ID === 50429 || \PHP_VERSION_ID === 50513) {
179
$classes = array_merge(
182
array('PharException'),
183
array('ArrayObject'),
184
array('DoctrineTest\\InstantiatorTestAsset\\ArrayObjectAsset'),
185
array('DoctrineTest\\InstantiatorTestAsset\\SerializableArrayObjectAsset'),
189
if (\PHP_VERSION_ID >= 50600) {
190
$classes[] = array('DoctrineTest\\InstantiatorTestAsset\\WakeUpNoticesAsset');
191
$classes[] = array('DoctrineTest\\InstantiatorTestAsset\\UnserializeExceptionArrayObjectAsset');
198
* Provides a list of instantiable classes (existing)
202
public function getInvalidClassNames()
205
array(__CLASS__ . uniqid()),
206
array('Doctrine\\Instantiator\\InstantiatorInterface'),
207
array('DoctrineTest\\InstantiatorTestAsset\\AbstractClassAsset'),
210
if (\PHP_VERSION_ID >= 50400) {
211
$classNames[] = array('DoctrineTest\\InstantiatorTestAsset\\SimpleTraitAsset');