~canonical-sysadmins/wordpress/openstack-objectstorage-k8s

« back to all changes in this revision

Viewing changes to vendor/doctrine/instantiator/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <ocramius@gmail.com>
 
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
        $className = 'DoctrineTest\\InstantiatorTestAsset\\UnserializeExceptionArrayObjectAsset';
 
83
 
 
84
        if (\PHP_VERSION_ID >= 50600) {
 
85
            $className = 'PDORow';
 
86
        }
 
87
 
 
88
        if (\PHP_VERSION_ID === 50429 || \PHP_VERSION_ID === 50513) {
 
89
            $className = 'DoctrineTest\\InstantiatorTestAsset\\SerializableArrayObjectAsset';
 
90
        }
 
91
 
 
92
        $this->setExpectedException('Doctrine\\Instantiator\\Exception\\UnexpectedValueException');
 
93
 
 
94
        $this->instantiator->instantiate($className);
 
95
    }
 
96
 
 
97
    public function testNoticeOnUnSerializationException()
 
98
    {
 
99
        if (\PHP_VERSION_ID >= 50600) {
 
100
            $this->markTestSkipped(
 
101
                'PHP 5.6 supports `ReflectionClass#newInstanceWithoutConstructor()` for some internal classes'
 
102
            );
 
103
        }
 
104
 
 
105
        try {
 
106
            $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\WakeUpNoticesAsset');
 
107
 
 
108
            $this->fail('No exception was raised');
 
109
        } catch (UnexpectedValueException $exception) {
 
110
            $wakeUpNoticesReflection = new ReflectionClass('DoctrineTest\\InstantiatorTestAsset\\WakeUpNoticesAsset');
 
111
            $previous                = $exception->getPrevious();
 
112
 
 
113
            $this->assertInstanceOf('Exception', $previous);
 
114
 
 
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)) {
 
117
                $this->assertSame(
 
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()
 
122
                );
 
123
 
 
124
                $this->assertSame('Something went bananas while un-serializing this instance', $previous->getMessage());
 
125
                $this->assertSame(\E_USER_NOTICE, $previous->getCode());
 
126
            }
 
127
        }
 
128
    }
 
129
 
 
130
    /**
 
131
     * @param string $invalidClassName
 
132
     *
 
133
     * @dataProvider getInvalidClassNames
 
134
     */
 
135
    public function testInstantiationFromNonExistingClass($invalidClassName)
 
136
    {
 
137
        $this->setExpectedException('Doctrine\\Instantiator\\Exception\\InvalidArgumentException');
 
138
 
 
139
        $this->instantiator->instantiate($invalidClassName);
 
140
    }
 
141
 
 
142
    public function testInstancesAreNotCloned()
 
143
    {
 
144
        $className = 'TemporaryClass' . uniqid();
 
145
 
 
146
        eval('namespace ' . __NAMESPACE__ . '; class ' . $className . '{}');
 
147
 
 
148
        $instance = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className);
 
149
 
 
150
        $instance->foo = 'bar';
 
151
 
 
152
        $instance2 = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className);
 
153
 
 
154
        $this->assertObjectNotHasAttribute('foo', $instance2);
 
155
    }
 
156
 
 
157
    /**
 
158
     * Provides a list of instantiable classes (existing)
 
159
     *
 
160
     * @return string[][]
 
161
     */
 
162
    public function getInstantiableClasses()
 
163
    {
 
164
        $classes = array(
 
165
            array('stdClass'),
 
166
            array(__CLASS__),
 
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'),
 
173
        );
 
174
 
 
175
        if (\PHP_VERSION_ID === 50429 || \PHP_VERSION_ID === 50513) {
 
176
            return $classes;
 
177
        }
 
178
 
 
179
        $classes = array_merge(
 
180
            $classes,
 
181
            array(
 
182
                array('PharException'),
 
183
                array('ArrayObject'),
 
184
                array('DoctrineTest\\InstantiatorTestAsset\\ArrayObjectAsset'),
 
185
                array('DoctrineTest\\InstantiatorTestAsset\\SerializableArrayObjectAsset'),
 
186
            )
 
187
        );
 
188
 
 
189
        if (\PHP_VERSION_ID >= 50600) {
 
190
            $classes[] = array('DoctrineTest\\InstantiatorTestAsset\\WakeUpNoticesAsset');
 
191
            $classes[] = array('DoctrineTest\\InstantiatorTestAsset\\UnserializeExceptionArrayObjectAsset');
 
192
        }
 
193
 
 
194
        return $classes;
 
195
    }
 
196
 
 
197
    /**
 
198
     * Provides a list of instantiable classes (existing)
 
199
     *
 
200
     * @return string[][]
 
201
     */
 
202
    public function getInvalidClassNames()
 
203
    {
 
204
        $classNames = array(
 
205
            array(__CLASS__ . uniqid()),
 
206
            array('Doctrine\\Instantiator\\InstantiatorInterface'),
 
207
            array('DoctrineTest\\InstantiatorTestAsset\\AbstractClassAsset'),
 
208
        );
 
209
 
 
210
        if (\PHP_VERSION_ID >= 50400) {
 
211
            $classNames[] = array('DoctrineTest\\InstantiatorTestAsset\\SimpleTraitAsset');
 
212
        }
 
213
 
 
214
        return $classNames;
 
215
    }
 
216
}