Completed
Pull Request — master (#23)
by Marco
31:10 queued 28:54
created

testExceptionOnUnSerializationException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 13
rs 9.4286
cc 2
eloc 7
nc 2
nop 0
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 ArrayObject;
23
use Doctrine\Instantiator\Exception\InvalidArgumentException;
24
use Doctrine\Instantiator\Exception\UnexpectedValueException;
25
use Doctrine\Instantiator\Instantiator;
26
use Doctrine\Instantiator\InstantiatorInterface;
27
use DoctrineTest\InstantiatorTestAsset\AbstractClassAsset;
28
use DoctrineTest\InstantiatorTestAsset\ArrayObjectAsset;
29
use DoctrineTest\InstantiatorTestAsset\ExceptionAsset;
30
use DoctrineTest\InstantiatorTestAsset\FinalExceptionAsset;
31
use DoctrineTest\InstantiatorTestAsset\PharExceptionAsset;
32
use DoctrineTest\InstantiatorTestAsset\SerializableArrayObjectAsset;
33
use DoctrineTest\InstantiatorTestAsset\SimpleSerializableAsset;
34
use DoctrineTest\InstantiatorTestAsset\SimpleTraitAsset;
35
use DoctrineTest\InstantiatorTestAsset\UnCloneableAsset;
36
use DoctrineTest\InstantiatorTestAsset\UnserializeExceptionArrayObjectAsset;
37
use DoctrineTest\InstantiatorTestAsset\WakeUpNoticesAsset;
38
use DoctrineTest\InstantiatorTestAsset\XMLReaderAsset;
39
use Exception;
40
use PDORow;
41
use PharException;
42
use PHPUnit_Framework_TestCase;
43
use stdClass;
44
45
/**
46
 * Tests for {@see \Doctrine\Instantiator\Instantiator}
47
 *
48
 * @author Marco Pivetta <[email protected]>
49
 *
50
 * @covers \Doctrine\Instantiator\Instantiator
51
 */
52
class InstantiatorTest extends PHPUnit_Framework_TestCase
53
{
54
    /**
55
     * @var Instantiator
56
     */
57
    private $instantiator;
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    protected function setUp()
63
    {
64
        $this->instantiator = new Instantiator();
65
    }
66
67
    /**
68
     * @param string $className
69
     *
70
     * @dataProvider getInstantiableClasses
71
     */
72
    public function testCanInstantiate($className)
73
    {
74
        $this->assertInstanceOf($className, $this->instantiator->instantiate($className));
75
    }
76
77
    /**
78
     * @param string $className
79
     *
80
     * @dataProvider getInstantiableClasses
81
     */
82
    public function testInstantiatesSeparateInstances($className)
83
    {
84
        $instance1 = $this->instantiator->instantiate($className);
85
        $instance2 = $this->instantiator->instantiate($className);
86
87
        $this->assertEquals($instance1, $instance2);
88
        $this->assertNotSame($instance1, $instance2);
89
    }
90
91
    public function testExceptionOnUnSerializationException()
92
    {
93
        if (defined('HHVM_VERSION')) {
94
            $this->markTestSkipped(
95
                'As of facebook/hhvm#3432, HHVM has no PDORow, and therefore '
96
                . ' no internal final classes that cannot be instantiated'
97
            );
98
        }
99
100
        $this->setExpectedException(UnexpectedValueException::class);
101
102
        $this->instantiator->instantiate(PDORow::class);
103
    }
104
105
    /**
106
     * @param string $invalidClassName
107
     *
108
     * @dataProvider getInvalidClassNames
109
     */
110
    public function testInstantiationFromNonExistingClass($invalidClassName)
111
    {
112
        $this->setExpectedException(InvalidArgumentException::class);
113
114
        $this->instantiator->instantiate($invalidClassName);
115
    }
116
117
    public function testInstancesAreNotCloned()
118
    {
119
        $className = 'TemporaryClass' . uniqid();
120
121
        eval('namespace ' . __NAMESPACE__ . '; class ' . $className . '{}');
122
123
        $instance = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className);
124
125
        $instance->foo = 'bar';
126
127
        $instance2 = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className);
128
129
        $this->assertObjectNotHasAttribute('foo', $instance2);
130
    }
131
132
    /**
133
     * Provides a list of instantiable classes (existing)
134
     *
135
     * @return string[][]
136
     */
137
    public function getInstantiableClasses()
138
    {
139
        return array(
140
            array(stdClass::class),
141
            array(__CLASS__),
142
            array(Instantiator::class),
143
            array(Exception::class),
144
            array(PharException::class),
145
            array(SimpleSerializableAsset::class),
146
            array(ExceptionAsset::class),
147
            array(FinalExceptionAsset::class),
148
            array(PharExceptionAsset::class),
149
            array(UnCloneableAsset::class),
150
            array(XMLReaderAsset::class),
151
            array(PharException::class),
152
            array(ArrayObject::class),
153
            array(ArrayObjectAsset::class),
154
            array(SerializableArrayObjectAsset::class),
155
            array(WakeUpNoticesAsset::class),
156
            array(UnserializeExceptionArrayObjectAsset::class),
157
        );
158
    }
159
160
    /**
161
     * Provides a list of instantiable classes (existing)
162
     *
163
     * @return string[][]
164
     */
165
    public function getInvalidClassNames()
166
    {
167
        return array(
168
            array(__CLASS__ . uniqid()),
169
            array(InstantiatorInterface::class),
170
            array(AbstractClassAsset::class),
171
            array(SimpleTraitAsset::class)
172
        );
173
    }
174
}
175