Completed
Pull Request — master (#6869)
by Michael
228:23 queued 163:26
created

EntityManagerTest::testCreateQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Tests\ORM;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
7
use Doctrine\Common\Persistence\Mapping\MappingException;
8
use Doctrine\DBAL\Connection;
9
use Doctrine\ORM\Configuration;
10
use Doctrine\ORM\EntityManager;
11
use Doctrine\ORM\Mapping\ClassMetadataFactory;
12
use Doctrine\ORM\NativeQuery;
13
use Doctrine\ORM\ORMException;
14
use Doctrine\ORM\ORMInvalidArgumentException;
15
use Doctrine\ORM\Proxy\ProxyFactory;
16
use Doctrine\ORM\Query;
17
use Doctrine\ORM\Query\ResultSetMapping;
18
use Doctrine\ORM\QueryBuilder;
19
use Doctrine\ORM\UnitOfWork;
20
use Doctrine\Tests\Models\CMS\CmsUser;
21
use Doctrine\Tests\Models\GeoNames\Country;
22
use Doctrine\Tests\OrmTestCase;
23
24
class EntityManagerTest extends OrmTestCase
25
{
26
    /**
27
     * @var EntityManager
28
     */
29
    private $_em;
30
31
    function setUp()
32
    {
33
        parent::setUp();
34
        $this->_em = $this->_getTestEntityManager();
35
    }
36
37
    /**
38
     * @group DDC-899
39
     */
40
    public function testIsOpen()
41
    {
42
        $this->assertTrue($this->_em->isOpen());
43
        $this->_em->close();
44
        $this->assertFalse($this->_em->isOpen());
45
    }
46
47
    public function testGetConnection()
48
    {
49
        $this->assertInstanceOf(Connection::class, $this->_em->getConnection());
50
    }
51
52
    public function testGetMetadataFactory()
53
    {
54
        $this->assertInstanceOf(ClassMetadataFactory::class, $this->_em->getMetadataFactory());
55
    }
56
57
    public function testGetConfiguration()
58
    {
59
        $this->assertInstanceOf(Configuration::class, $this->_em->getConfiguration());
60
    }
61
62
    public function testGetUnitOfWork()
63
    {
64
        $this->assertInstanceOf(UnitOfWork::class, $this->_em->getUnitOfWork());
65
    }
66
67
    public function testGetProxyFactory()
68
    {
69
        $this->assertInstanceOf(ProxyFactory::class, $this->_em->getProxyFactory());
70
    }
71
72
    public function testGetEventManager()
73
    {
74
        $this->assertInstanceOf(EventManager::class, $this->_em->getEventManager());
75
    }
76
77
    public function testCreateNativeQuery()
78
    {
79
        $rsm = new ResultSetMapping();
80
        $query = $this->_em->createNativeQuery('SELECT foo', $rsm);
81
82
        $this->assertSame('SELECT foo', $query->getSql());
83
    }
84
85
    /**
86
     * @covers \Doctrine\ORM\EntityManager::createNamedNativeQuery
87
     */
88
    public function testCreateNamedNativeQuery()
89
    {
90
        $rsm = new ResultSetMapping();
91
        $this->_em->getConfiguration()->addNamedNativeQuery('foo', 'SELECT foo', $rsm);
92
93
        $query = $this->_em->createNamedNativeQuery('foo');
94
95
        $this->assertInstanceOf(NativeQuery::class, $query);
96
    }
97
98
    public function testCreateQueryBuilder()
99
    {
100
        $this->assertInstanceOf(QueryBuilder::class, $this->_em->createQueryBuilder());
101
    }
102
103
    public function testCreateQueryBuilderAliasValid()
104
    {
105
        $q = $this->_em->createQueryBuilder()
106
             ->select('u')->from(CmsUser::class, 'u');
107
        $q2 = clone $q;
108
109
        $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q->getQuery()->getDql());
110
        $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q2->getQuery()->getDql());
111
112
        $q3 = clone $q;
113
114
        $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q3->getQuery()->getDql());
115
    }
116
117
    public function testCreateQuery_DqlIsOptional()
118
    {
119
        $this->assertInstanceOf(Query::class, $this->_em->createQuery());
120
    }
121
122
    public function testGetPartialReference()
123
    {
124
        $user = $this->_em->getPartialReference(CmsUser::class, 42);
125
        $this->assertTrue($this->_em->contains($user));
126
        $this->assertEquals(42, $user->id);
127
        $this->assertNull($user->getName());
128
    }
129
130
    public function testCreateQuery()
131
    {
132
        $q = $this->_em->createQuery('SELECT 1');
133
        $this->assertInstanceOf(Query::class, $q);
134
        $this->assertEquals('SELECT 1', $q->getDql());
135
    }
136
137
    /**
138
     * @covers Doctrine\ORM\EntityManager::createNamedQuery
139
     */
140
    public function testCreateNamedQuery()
141
    {
142
        $this->_em->getConfiguration()->addNamedQuery('foo', 'SELECT 1');
143
144
        $query = $this->_em->createNamedQuery('foo');
145
        $this->assertInstanceOf(Query::class, $query);
146
        $this->assertEquals('SELECT 1', $query->getDql());
147
    }
148
149 View Code Duplication
    static public function dataMethodsAffectedByNoObjectArguments()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
150
    {
151
        return [
152
            ['persist'],
153
            ['remove'],
154
            ['merge'],
155
            ['refresh'],
156
            ['detach']
157
        ];
158
    }
159
160
    /**
161
     * @dataProvider dataMethodsAffectedByNoObjectArguments
162
     * @group legacy
163
     */
164
    public function testThrowsExceptionOnNonObjectValues($methodName) {
165
        $this->expectException(ORMInvalidArgumentException::class);
166
        $this->expectExceptionMessage('EntityManager#' . $methodName . '() expects parameter 1 to be an entity object, NULL given.');
167
168
        $this->_em->$methodName(null);
169
    }
170
171 View Code Duplication
    static public function dataAffectedByErrorIfClosedException()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
172
    {
173
        return [
174
            ['flush'],
175
            ['persist'],
176
            ['remove'],
177
            ['merge'],
178
            ['refresh'],
179
        ];
180
    }
181
182
    /**
183
     * @dataProvider dataAffectedByErrorIfClosedException
184
     * @param string $methodName
185
     *
186
     * @group legacy
187
     */
188
    public function testAffectedByErrorIfClosedException($methodName)
189
    {
190
        $this->expectException(ORMException::class);
191
        $this->expectExceptionMessage('closed');
192
193
        $this->_em->close();
194
        $this->_em->$methodName(new \stdClass());
195
    }
196
197
    /**
198
     * @group DDC-1125
199
     */
200
    public function testTransactionalAcceptsReturn()
201
    {
202
        $return = $this->_em->transactional(function ($em) {
203
            return 'foo';
204
        });
205
206
        $this->assertEquals('foo', $return);
207
    }
208
209
    public function testTransactionalAcceptsVariousCallables()
210
    {
211
        $this->assertSame('callback', $this->_em->transactional([$this, 'transactionalCallback']));
212
    }
213
214
    public function testTransactionalThrowsInvalidArgumentExceptionIfNonCallablePassed()
215
    {
216
        $this->expectException(\InvalidArgumentException::class);
217
        $this->expectExceptionMessage('Expected argument of type "callable", got "object"');
218
219
        $this->_em->transactional($this);
220
    }
221
222
    public function transactionalCallback($em)
223
    {
224
        $this->assertSame($this->_em, $em);
225
        return 'callback';
226
    }
227
228
    public function testCreateInvalidConnection()
229
    {
230
        $this->expectException(\InvalidArgumentException::class);
231
        $this->expectExceptionMessage('Invalid $connection argument of type integer given: "1".');
232
233
        $config = new Configuration();
234
        $config->setMetadataDriverImpl($this->createMock(MappingDriver::class));
235
        EntityManager::create(1, $config);
0 ignored issues
show
Bug introduced by
1 of type integer is incompatible with the type array|Doctrine\DBAL\Connection expected by parameter $connection of Doctrine\ORM\EntityManager::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

235
        EntityManager::create(/** @scrutinizer ignore-type */ 1, $config);
Loading history...
236
    }
237
238
    /**
239
     * @group #5796
240
     */
241
    public function testTransactionalReThrowsThrowables()
242
    {
243
        try {
244
            $this->_em->transactional(function () {
245
                (function (array $value) {
246
                    // this only serves as an IIFE that throws a `TypeError`
247
                })(null);
248
            });
249
250
            self::fail('TypeError expected to be thrown');
251
        } catch (\TypeError $ignored) {
252
            self::assertFalse($this->_em->isOpen());
253
        }
254
    }
255
256
    /**
257
     * @group 6017
258
     */
259
    public function testClearManagerWithObject()
260
    {
261
        $entity = new Country(456, 'United Kingdom');
262
263
        $this->expectException(ORMInvalidArgumentException::class);
264
265
        $this->_em->clear($entity);
0 ignored issues
show
Bug introduced by
$entity of type Doctrine\Tests\Models\GeoNames\Country is incompatible with the type null|string expected by parameter $entityName of Doctrine\ORM\EntityManager::clear(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

265
        $this->_em->clear(/** @scrutinizer ignore-type */ $entity);
Loading history...
266
    }
267
268
    /**
269
     * @group 6017
270
     */
271
    public function testClearManagerWithUnknownEntityName()
272
    {
273
        $this->expectException(MappingException::class);
274
275
        $this->_em->clear(uniqid('nonExisting', true));
276
    }
277
278
    /**
279
     * @group 6017
280
     */
281
    public function testClearManagerWithProxyClassName()
282
    {
283
        $proxy = $this->_em->getReference(Country::class, ['id' => random_int(457, 100000)]);
284
285
        $entity = new Country(456, 'United Kingdom');
286
287
        $this->_em->persist($entity);
288
289
        $this->assertTrue($this->_em->contains($entity));
290
291
        $this->_em->clear(get_class($proxy));
0 ignored issues
show
Bug introduced by
It seems like $proxy can also be of type true; however, parameter $object of get_class() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

291
        $this->_em->clear(get_class(/** @scrutinizer ignore-type */ $proxy));
Loading history...
292
293
        $this->assertFalse($this->_em->contains($entity));
294
    }
295
296
    /**
297
     * @group 6017
298
     */
299
    public function testClearManagerWithNullValue()
300
    {
301
        $entity = new Country(456, 'United Kingdom');
302
303
        $this->_em->persist($entity);
304
305
        $this->assertTrue($this->_em->contains($entity));
306
307
        $this->_em->clear(null);
308
309
        $this->assertFalse($this->_em->contains($entity));
310
    }
311
312
    /**
313
     * @group legacy
314
     * @expectedDeprecation Calling Doctrine\ORM\EntityManager::flush() with any arguments to flush specific entities is deprecated and will not be supported in Doctrine 3.0.
315
     */
316
    public function testDeprecatedFlushWithArguments() : void
317
    {
318
        $entity = new Country(456, 'United Kingdom');
319
        $this->_em->persist($entity);
320
        $this->_em->flush($entity);
321
    }
322
323
    /**
324
     * @group legacy
325
     * @expectedDeprecation Method Doctrine\ORM\EntityManager::merge() is deprecated and will be removed in Doctrine 3.0.
326
     */
327
    public function testDeprecatedMerge() : void
328
    {
329
        $entity = new Country(456, 'United Kingdom');
330
        $this->_em->persist($entity);
331
        $this->_em->merge($entity);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::merge() has been deprecated: This method is deprecated and will be removed in Doctrine 3.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

331
        /** @scrutinizer ignore-deprecated */ $this->_em->merge($entity);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
332
    }
333
334
    /**
335
     * @group legacy
336
     * @expectedDeprecation Method Doctrine\ORM\EntityManager::detach() is deprecated and will be removed in Doctrine 3.0.
337
     */
338
    public function testDeprecatedDetach() : void
339
    {
340
        $entity = new Country(456, 'United Kingdom');
341
        $this->_em->persist($entity);
342
        $this->_em->detach($entity);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::detach() has been deprecated: This method is deprecated and will be removed in Doctrine 3.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

342
        /** @scrutinizer ignore-deprecated */ $this->_em->detach($entity);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
343
    }
344
345
    /**
346
     * @group legacy
347
     * @expectedDeprecation Method Doctrine\ORM\EntityManager::copy() is deprecated and will be removed in Doctrine 3.0.
348
     */
349
    public function testDeprecatedCopy() : void
350
    {
351
        $entity = new Country(456, 'United Kingdom');
352
        $this->_em->persist($entity);
353
354
        try {
355
            $this->_em->copy($entity);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::copy() has been deprecated: This method is deprecated and will be removed in Doctrine 3.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

355
            /** @scrutinizer ignore-deprecated */ $this->_em->copy($entity);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
356
        } catch (\BadMethodCallException $e) {
357
            // do nothing
358
        }
359
    }
360
}
361