Completed
Pull Request — master (#23)
by Marco
37:36 queued 35:24
created

InstantiatorTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 9
c 10
b 0
f 0
lcom 1
cbo 2
dl 0
loc 123
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testCanInstantiate() 0 4 1
A testInstantiatesSeparateInstances() 0 8 1
A testExceptionOnUnSerializationException() 0 13 2
A testInstantiationFromNonExistingClass() 0 6 1
A testInstancesAreNotCloned() 0 14 1
A getInstantiableClasses() 0 22 1
A getInvalidClassNames() 0 9 1
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 [
140
            [stdClass::class],
141
            [__CLASS__],
142
            [Instantiator::class],
143
            [Exception::class],
144
            [PharException::class],
145
            [SimpleSerializableAsset::class],
146
            [ExceptionAsset::class],
147
            [FinalExceptionAsset::class],
148
            [PharExceptionAsset::class],
149
            [UnCloneableAsset::class],
150
            [XMLReaderAsset::class],
151
            [PharException::class],
152
            [ArrayObject::class],
153
            [ArrayObjectAsset::class],
154
            [SerializableArrayObjectAsset::class],
155
            [WakeUpNoticesAsset::class],
156
            [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 [
168
            [__CLASS__ . uniqid()],
169
            [InstantiatorInterface::class],
170
            [AbstractClassAsset::class],
171
            [SimpleTraitAsset::class]
172
        ];
173
    }
174
}
175