Failed Conditions
Pull Request — master (#7143)
by Mike
08:11
created

EntityManagerTest::testGetMetadataFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 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;
0 ignored issues
show
introduced by
Type Doctrine\ORM\Mapping\ClassMetadataFactory is not used in this file.
Loading history...
12
use Doctrine\ORM\Mapping\Driver\MappingDriver;
13
use Doctrine\ORM\Mapping\MetadataCollection;
0 ignored issues
show
introduced by
Type Doctrine\ORM\Mapping\MetadataCollection is not used in this file.
Loading history...
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
28
class EntityManagerTest extends OrmTestCase
29
{
30
    /** @var EntityManager */
31
    private $em;
32
33
    public function setUp() : void
34
    {
35
        parent::setUp();
36
37
        $this->em = $this->getTestEntityManager();
38
    }
39
40
    /**
41
     * @group DDC-899
42
     */
43
    public function testIsOpen() : void
44
    {
45
        self::assertTrue($this->em->isOpen());
46
        $this->em->close();
47
        self::assertFalse($this->em->isOpen());
48
    }
49
50
    public function testGetConnection() : void
51
    {
52
        self::assertInstanceOf(Connection::class, $this->em->getConnection());
53
    }
54
55
    public function testGetConfiguration() : void
56
    {
57
        self::assertInstanceOf(Configuration::class, $this->em->getConfiguration());
58
    }
59
60
    public function testGetUnitOfWork() : void
61
    {
62
        self::assertInstanceOf(UnitOfWork::class, $this->em->getUnitOfWork());
63
    }
64
65
    public function testGetProxyFactory() : void
66
    {
67
        self::assertInstanceOf(ProxyFactory::class, $this->em->getProxyFactory());
68
    }
69
70
    public function testGetEventManager() : void
71
    {
72
        self::assertInstanceOf(EventManager::class, $this->em->getEventManager());
73
    }
74
75
    public function testCreateNativeQuery() : void
76
    {
77
        $rsm   = new ResultSetMapping();
78
        $query = $this->em->createNativeQuery('SELECT foo', $rsm);
79
80
        self::assertSame('SELECT foo', $query->getSql());
81
    }
82
83
    public function testCreateQueryBuilder() : void
84
    {
85
        self::assertInstanceOf(QueryBuilder::class, $this->em->createQueryBuilder());
86
    }
87
88
    public function testCreateQueryBuilderAliasValid() : void
89
    {
90
        $q  = $this->em->createQueryBuilder()
91
             ->select('u')->from(CmsUser::class, 'u');
92
        $q2 = clone $q;
93
94
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q->getQuery()->getDql());
95
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q2->getQuery()->getDql());
96
97
        $q3 = clone $q;
98
99
        self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q3->getQuery()->getDql());
100
    }
101
102
    public function testCreateQueryDqlIsOptional() : void
103
    {
104
        self::assertInstanceOf(Query::class, $this->em->createQuery());
105
    }
106
107
    public function testGetPartialReference() : void
108
    {
109
        $user = $this->em->getPartialReference(CmsUser::class, 42);
110
        self::assertTrue($this->em->contains($user));
111
        self::assertEquals(42, $user->id);
112
        self::assertNull($user->getName());
113
    }
114
115
    public function testCreateQuery() : void
116
    {
117
        $q = $this->em->createQuery('SELECT 1');
118
        self::assertInstanceOf(Query::class, $q);
119
        self::assertEquals('SELECT 1', $q->getDql());
120
    }
121
122
    public static function dataMethodsAffectedByNoObjectArguments()
123
    {
124
        return [
125
            ['persist'],
126
            ['remove'],
127
            ['refresh'],
128
        ];
129
    }
130
131
    /**
132
     * @dataProvider dataMethodsAffectedByNoObjectArguments
133
     */
134
    public function testThrowsExceptionOnNonObjectValues($methodName) : void
135
    {
136
        $this->expectException(ORMInvalidArgumentException::class);
137
        $this->expectExceptionMessage('EntityManager#' . $methodName . '() expects parameter 1 to be an entity object, NULL given.');
138
139
        $this->em->{$methodName}(null);
140
    }
141
142
    public static function dataAffectedByErrorIfClosedException()
143
    {
144
        return [
145
            ['flush'],
146
            ['persist'],
147
            ['remove'],
148
            ['refresh'],
149
        ];
150
    }
151
152
    /**
153
     * @dataProvider dataAffectedByErrorIfClosedException
154
     * @param string $methodName
155
     */
156
    public function testAffectedByErrorIfClosedException($methodName) : void
157
    {
158
        $this->expectException(ORMException::class);
159
        $this->expectExceptionMessage('closed');
160
161
        $this->em->close();
162
        $this->em->{$methodName}(new \stdClass());
163
    }
164
165
    public function dataToBeReturnedByTransactional()
166
    {
167
        return [
168
            [null],
169
            [false],
170
            ['foo'],
171
        ];
172
    }
173
174
    /**
175
     * @dataProvider dataToBeReturnedByTransactional
176
     */
177
    public function testTransactionalAcceptsReturn($value) : void
178
    {
179
        self::assertSame(
180
            $value,
181
            $this->em->transactional(function ($em) use ($value) {
182
                return $value;
183
            })
184
        );
185
    }
186
187
    public function testTransactionalAcceptsVariousCallables() : void
188
    {
189
        self::assertSame('callback', $this->em->transactional([$this, 'transactionalCallback']));
190
    }
191
192
    public function transactionalCallback($em)
193
    {
194
        self::assertSame($this->em, $em);
195
        return 'callback';
196
    }
197
198
    public function testCreateInvalidConnection() : void
199
    {
200
        $this->expectException(\InvalidArgumentException::class);
201
        $this->expectExceptionMessage('Invalid $connection argument of type integer given: "1".');
202
203
        $config = new Configuration();
204
        $config->setMetadataDriverImpl($this->createMock(MappingDriver::class));
205
        EntityManager::create(1, $config);
0 ignored issues
show
Bug introduced by
The call to Doctrine\ORM\EntityManager::create() has too few arguments starting with eventManager. ( Ignorable by Annotation )

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

205
        EntityManager::/** @scrutinizer ignore-call */ 
206
                       create(1, $config);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
1 of type integer is incompatible with the type Doctrine\DBAL\Connection|array<mixed,mixed> 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

205
        EntityManager::create(/** @scrutinizer ignore-type */ 1, $config);
Loading history...
206
    }
207
208
    /**
209
     * @group #5796
210
     */
211
    public function testTransactionalReThrowsThrowables() : void
212
    {
213
        try {
214
            $this->em->transactional(function () {
215
                (function (array $value) {
216
                    // this only serves as an IIFE that throws a `TypeError`
217
                })(null);
218
            });
219
220
            self::fail('TypeError expected to be thrown');
221
        } catch (\TypeError $ignored) {
222
            self::assertFalse($this->em->isOpen());
223
        }
224
    }
225
226
    /**
227
     * @group 6017
228
     */
229
    public function testClearManager() : void
230
    {
231
        $entity = new Country(456, 'United Kingdom');
232
233
        $this->em->persist($entity);
234
235
        self::assertTrue($this->em->contains($entity));
236
237
        $this->em->clear();
238
239
        self::assertFalse($this->em->contains($entity));
240
    }
241
242
    public function testGetReferenceRetrievesReferencesWithGivenProxiesAsIdentifiers() : void
243
    {
244
        $simpleIdReference = $this->em->getReference(
245
            SimpleId::class,
246
            ['id' => 123]
247
        );
248
        /** @var GhostObjectInterface|ToOneAssociationIdToSimpleId $nestedReference */
249
        $nestedIdReference = $this->em->getReference(
250
            ToOneAssociationIdToSimpleId::class,
251
            ['simpleId' => $simpleIdReference]
252
        );
253
254
        self::assertInstanceOf(ToOneAssociationIdToSimpleId::class, $nestedIdReference);
255
        self::assertSame($simpleIdReference, $nestedIdReference->simpleId);
256
        self::assertTrue($this->em->contains($simpleIdReference));
257
        self::assertTrue($this->em->contains($nestedIdReference));
258
    }
259
260
    public function testGetReferenceRetrievesReferencesWithGivenProxiesAsIdentifiersEvenIfIdentifierOrderIsSwapped() : void
261
    {
262
        $simpleIdReferenceA = $this->em->getReference(
263
            SimpleId::class,
264
            ['id' => 123]
265
        );
266
        $simpleIdReferenceB = $this->em->getReference(
267
            SimpleId::class,
268
            ['id' => 456]
269
        );
270
        /** @var GhostObjectInterface|ToOneCompositeAssociationToMultipleSimpleId $nestedIdReference */
271
        $nestedIdReference = $this->em->getReference(
272
            ToOneCompositeAssociationToMultipleSimpleId::class,
273
            ['simpleIdB' => $simpleIdReferenceB, 'simpleIdA' => $simpleIdReferenceA]
274
        );
275
276
        self::assertInstanceOf(ToOneCompositeAssociationToMultipleSimpleId::class, $nestedIdReference);
277
        self::assertSame($simpleIdReferenceA, $nestedIdReference->simpleIdA);
278
        self::assertSame($simpleIdReferenceB, $nestedIdReference->simpleIdB);
279
        self::assertTrue($this->em->contains($simpleIdReferenceA));
280
        self::assertTrue($this->em->contains($simpleIdReferenceB));
281
        self::assertTrue($this->em->contains($nestedIdReference));
282
    }
283
}
284