Failed Conditions
Pull Request — develop (#6719)
by Marco
79:37 queued 14:59
created

testCreateQueryBuilderAliasValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM;
6
7
use Doctrine\Common\EventManager;
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\Mapping\Driver\MappingDriver;
13
use Doctrine\ORM\NativeQuery;
14
use Doctrine\ORM\ORMException;
15
use Doctrine\ORM\ORMInvalidArgumentException;
16
use Doctrine\ORM\Proxy\Factory\ProxyFactory;
17
use Doctrine\ORM\Query;
18
use Doctrine\ORM\Query\ResultSetMapping;
19
use Doctrine\ORM\QueryBuilder;
20
use Doctrine\ORM\UnitOfWork;
21
use Doctrine\Tests\Models\CMS\CmsUser;
22
use Doctrine\Tests\Models\GeoNames\Country;
23
use Doctrine\Tests\Models\IdentityIsAssociation\SimpleId;
24
use Doctrine\Tests\Models\IdentityIsAssociation\ToOneAssociationIdToSimpleId;
25
use Doctrine\Tests\Models\IdentityIsAssociation\ToOneCompositeAssociationToMultipleSimpleId;
26
use Doctrine\Tests\OrmTestCase;
27
use ProxyManager\Proxy\GhostObjectInterface;
28
29
class EntityManagerTest extends OrmTestCase
30
{
31
    /**
32
     * @var EntityManager
33
     */
34
    private $em;
35
36
    public function setUp()
37
    {
38
        parent::setUp();
39
40
        $this->em = $this->getTestEntityManager();
41
    }
42
43
    /**
44
     * @group DDC-899
45
     */
46
    public function testIsOpen()
47
    {
48
        self::assertTrue($this->em->isOpen());
49
        $this->em->close();
50
        self::assertFalse($this->em->isOpen());
51
    }
52
53
    public function testGetConnection()
54
    {
55
        self::assertInstanceOf(Connection::class, $this->em->getConnection());
56
    }
57
58
    public function testGetMetadataFactory()
59
    {
60
        self::assertInstanceOf(ClassMetadataFactory::class, $this->em->getMetadataFactory());
61
    }
62
63
    public function testGetConfiguration()
64
    {
65
        self::assertInstanceOf(Configuration::class, $this->em->getConfiguration());
66
    }
67
68
    public function testGetUnitOfWork()
69
    {
70
        self::assertInstanceOf(UnitOfWork::class, $this->em->getUnitOfWork());
71
    }
72
73
    public function testGetProxyFactory()
74
    {
75
        self::assertInstanceOf(ProxyFactory::class, $this->em->getProxyFactory());
76
    }
77
78
    public function testGetEventManager()
79
    {
80
        self::assertInstanceOf(EventManager::class, $this->em->getEventManager());
81
    }
82
83
    public function testCreateNativeQuery()
84
    {
85
        $rsm = new ResultSetMapping();
86
        $query = $this->em->createNativeQuery('SELECT foo', $rsm);
87
88
        self::assertSame('SELECT foo', $query->getSql());
89
    }
90
91
    /**
92
     * @covers \Doctrine\ORM\EntityManager::createNamedNativeQuery
93
     */
94
    public function testCreateNamedNativeQuery()
95
    {
96
        $rsm = new ResultSetMapping();
97
        $this->em->getConfiguration()->addNamedNativeQuery('foo', 'SELECT foo', $rsm);
98
99
        $query = $this->em->createNamedNativeQuery('foo');
100
101
        self::assertInstanceOf(NativeQuery::class, $query);
102
    }
103
104
    public function testCreateQueryBuilder()
105
    {
106
        self::assertInstanceOf(QueryBuilder::class, $this->em->createQueryBuilder());
107
    }
108
109
    public function testCreateQueryBuilderAliasValid()
110
    {
111
        $q = $this->em->createQueryBuilder()
112
             ->select('u')->from(CmsUser::class, 'u');
113
        $q2 = clone $q;
114
115
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q->getQuery()->getDql());
116
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q2->getQuery()->getDql());
117
118
        $q3 = clone $q;
119
120
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q3->getQuery()->getDql());
121
    }
122
123
    public function testCreateQuery_DqlIsOptional()
124
    {
125
        self::assertInstanceOf(Query::class, $this->em->createQuery());
126
    }
127
128
    public function testGetPartialReference()
129
    {
130
        $user = $this->em->getPartialReference(CmsUser::class, 42);
131
        self::assertTrue($this->em->contains($user));
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->em->getPartialRef...CMS\CmsUser::class, 42) on line 130 can also be of type null; however, Doctrine\ORM\EntityManager::contains() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
132
        self::assertEquals(42, $user->id);
133
        self::assertNull($user->getName());
134
    }
135
136
    public function testCreateQuery()
137
    {
138
        $q = $this->em->createQuery('SELECT 1');
139
        self::assertInstanceOf(Query::class, $q);
140
        self::assertEquals('SELECT 1', $q->getDql());
141
    }
142
143
    /**
144
     * @covers Doctrine\ORM\EntityManager::createNamedQuery
145
     */
146
    public function testCreateNamedQuery()
147
    {
148
        $this->em->getConfiguration()->addNamedQuery('foo', 'SELECT 1');
149
150
        $query = $this->em->createNamedQuery('foo');
151
        self::assertInstanceOf(Query::class, $query);
152
        self::assertEquals('SELECT 1', $query->getDql());
153
    }
154
155
    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...
156
    {
157
        return [
158
            ['persist'],
159
            ['remove'],
160
            ['refresh'],
161
        ];
162
    }
163
164
    /**
165
     * @dataProvider dataMethodsAffectedByNoObjectArguments
166
     */
167
    public function testThrowsExceptionOnNonObjectValues($methodName) {
168
        $this->expectException(ORMInvalidArgumentException::class);
169
        $this->expectExceptionMessage('EntityManager#' . $methodName . '() expects parameter 1 to be an entity object, NULL given.');
170
171
        $this->em->$methodName(null);
172
    }
173
174
    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...
175
    {
176
        return [
177
            ['flush'],
178
            ['persist'],
179
            ['remove'],
180
            ['refresh'],
181
        ];
182
    }
183
184
    /**
185
     * @dataProvider dataAffectedByErrorIfClosedException
186
     * @param string $methodName
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
    public function dataToBeReturnedByTransactional()
198
    {
199
        return [
200
            [null],
201
            [false],
202
            ['foo'],
203
        ];
204
    }
205
206
    /**
207
     * @dataProvider dataToBeReturnedByTransactional
208
     */
209
    public function testTransactionalAcceptsReturn($value)
210
    {
211
        self::assertSame(
212
            $value,
213
            $this->em->transactional(function ($em) use ($value) {
0 ignored issues
show
Unused Code introduced by
The parameter $em is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
214
                return $value;
215
            })
216
        );
217
    }
218
219
    public function testTransactionalAcceptsVariousCallables()
220
    {
221
        self::assertSame('callback', $this->em->transactional([$this, 'transactionalCallback']));
222
    }
223
224
    public function transactionalCallback($em)
225
    {
226
        self::assertSame($this->em, $em);
227
        return 'callback';
228
    }
229
230
    public function testCreateInvalidConnection()
231
    {
232
        $this->expectException(\InvalidArgumentException::class);
233
        $this->expectExceptionMessage('Invalid $connection argument of type integer given: "1".');
234
235
        $config = new Configuration();
236
        $config->setMetadataDriverImpl($this->createMock(MappingDriver::class));
237
        EntityManager::create(1, $config);
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a array|object<Doctrine\DBAL\Connection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
238
    }
239
240
    /**
241
     * @group 6017
242
     */
243
    public function testClearManager()
244
    {
245
        $entity = new Country(456, 'United Kingdom');
246
247
        $this->em->persist($entity);
248
249
        self::assertTrue($this->em->contains($entity));
250
251
        $this->em->clear(null);
252
253
        self::assertFalse($this->em->contains($entity));
254
    }
255
256
    public function testGetReferenceRetrievesReferencesWithGivenProxiesAsIdentifiers() : void
257
    {
258
        $simpleIdReference = $this->em->getReference(
259
            SimpleId::class,
260
            ['id' => 123]
261
        );
262
        /* @var $nestedReference GhostObjectInterface|ToOneAssociationIdToSimpleId */
263
        $nestedIdReference = $this->em->getReference(
264
            ToOneAssociationIdToSimpleId::class,
265
            ['simpleId' => $simpleIdReference]
266
        );
267
268
        self::assertInstanceOf(ToOneAssociationIdToSimpleId::class, $nestedIdReference);
269
        self::assertSame($simpleIdReference, $nestedIdReference->simpleId);
270
        self::assertTrue($this->em->contains($simpleIdReference));
0 ignored issues
show
Bug introduced by
It seems like $simpleIdReference defined by $this->em->getReference(...ss, array('id' => 123)) on line 258 can also be of type null; however, Doctrine\ORM\EntityManager::contains() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
271
        self::assertTrue($this->em->contains($nestedIdReference));
272
    }
273
274
    public function testGetReferenceRetrievesReferencesWithGivenProxiesAsIdentifiersEvenIfIdentifierOrderIsSwapped() : void
275
    {
276
        $simpleIdReferenceA = $this->em->getReference(
277
            SimpleId::class,
278
            ['id' => 123]
279
        );
280
        $simpleIdReferenceB = $this->em->getReference(
281
            SimpleId::class,
282
            ['id' => 456]
283
        );
284
        /* @var $nestedIdReference GhostObjectInterface|ToOneCompositeAssociationToMultipleSimpleId */
285
        $nestedIdReference = $this->em->getReference(
286
            ToOneCompositeAssociationToMultipleSimpleId::class,
287
            ['simpleIdB' => $simpleIdReferenceB, 'simpleIdA' => $simpleIdReferenceA]
288
        );
289
290
        self::assertInstanceOf(ToOneCompositeAssociationToMultipleSimpleId::class, $nestedIdReference);
291
        self::assertSame($simpleIdReferenceA, $nestedIdReference->simpleIdA);
292
        self::assertSame($simpleIdReferenceB, $nestedIdReference->simpleIdB);
293
        self::assertTrue($this->em->contains($simpleIdReferenceA));
0 ignored issues
show
Bug introduced by
It seems like $simpleIdReferenceA defined by $this->em->getReference(...ss, array('id' => 123)) on line 276 can also be of type null; however, Doctrine\ORM\EntityManager::contains() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
294
        self::assertTrue($this->em->contains($simpleIdReferenceB));
0 ignored issues
show
Bug introduced by
It seems like $simpleIdReferenceB defined by $this->em->getReference(...ss, array('id' => 456)) on line 280 can also be of type null; however, Doctrine\ORM\EntityManager::contains() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
295
        self::assertTrue($this->em->contains($nestedIdReference));
296
    }
297
}
298