Completed
Pull Request — master (#23)
by Marco
35:57 queued 32:55
created

InstantiatorTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanInstantiate() 0 4 1
A setUp() 0 6 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 TestCase
53
{
54
    /**
55
     * @var Instantiator
56
     */
57
    private $instantiator;
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    protected function setUp() : void
63
    {
64
        parent::setUp();
65
66
        $this->instantiator = new Instantiator();
67
    }
68
69
    /**
70
     * @dataProvider getInstantiableClasses
71
     */
72
    public function testCanInstantiate(string $className) : void
73
    {
74
        $this->assertInstanceOf($className, $this->instantiator->instantiate($className));
75
    }
76
77
    /**
78
     * @dataProvider getInstantiableClasses
79
     */
80
    public function testInstantiatesSeparateInstances(string $className) : void
81
    {
82
        $instance1 = $this->instantiator->instantiate($className);
83
        $instance2 = $this->instantiator->instantiate($className);
84
85
        $this->assertEquals($instance1, $instance2);
86
        $this->assertNotSame($instance1, $instance2);
87
    }
88
89
    public function testExceptionOnUnSerializationException() : void
90
    {
91
        if (defined('HHVM_VERSION')) {
92
            $this->markTestSkipped(
93
                'As of facebook/hhvm#3432, HHVM has no PDORow, and therefore '
94
                . ' no internal final classes that cannot be instantiated'
95
            );
96
        }
97
98
        $this->expectException(UnexpectedValueException::class);
99
100
        $this->instantiator->instantiate(PDORow::class);
101
    }
102
103
    /**
104
     * @dataProvider getInvalidClassNames
105
     */
106
    public function testInstantiationFromNonExistingClass(string $invalidClassName) : void
107
    {
108
        $this->expectException(InvalidArgumentException::class);
109
110
        $this->instantiator->instantiate($invalidClassName);
111
    }
112
113
    public function testInstancesAreNotCloned() : void
114
    {
115
        $className = 'TemporaryClass' . str_replace('.', '', uniqid('', true));
116
117
        eval('namespace ' . __NAMESPACE__ . '; class ' . $className . '{}');
118
119
        $instance = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className);
120
121
        $instance->foo = 'bar';
122
123
        $instance2 = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className);
124
125
        $this->assertObjectNotHasAttribute('foo', $instance2);
126
    }
127
128
    /**
129
     * Provides a list of instantiable classes (existing)
130
     *
131
     * @return string[][]
132
     */
133
    public function getInstantiableClasses() : array
134
    {
135
        return [
136
            [stdClass::class],
137
            [__CLASS__],
138
            [Instantiator::class],
139
            [Exception::class],
140
            [PharException::class],
141
            [SimpleSerializableAsset::class],
142
            [ExceptionAsset::class],
143
            [FinalExceptionAsset::class],
144
            [PharExceptionAsset::class],
145
            [UnCloneableAsset::class],
146
            [XMLReaderAsset::class],
147
            [PharException::class],
148
            [ArrayObject::class],
149
            [ArrayObjectAsset::class],
150
            [SerializableArrayObjectAsset::class],
151
            [WakeUpNoticesAsset::class],
152
            [UnserializeExceptionArrayObjectAsset::class],
153
        ];
154
    }
155
156
    /**
157
     * Provides a list of instantiable classes (existing)
158
     *
159
     * @return string[][]
160
     */
161
    public function getInvalidClassNames() : array
162
    {
163
        return [
164
            [__CLASS__ . str_replace('.', '', uniqid('', true))],
165
            [InstantiatorInterface::class],
166
            [AbstractClassAsset::class],
167
            [SimpleTraitAsset::class]
168
        ];
169
    }
170
}
171