Failed Conditions
Pull Request — master (#7143)
by Mike
07:18
created

OrmTestCase   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 217
rs 10
c 0
b 0
f 0
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createAnnotationDriver() 0 7 1
A getSharedQueryCacheImpl() 0 7 2
A getSharedMetadataCacheImpl() 0 7 2
A enableSecondLevelCache() 0 4 1
A getSharedSecondLevelCacheDriverImpl() 0 7 2
C getTestEntityManager() 0 116 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests;
6
7
use Doctrine\Common\Annotations;
8
use Doctrine\Common\Cache\ArrayCache;
9
use Doctrine\Common\Cache\Cache;
10
use Doctrine\Common\EventManager;
11
use Doctrine\DBAL\Connection;
12
use Doctrine\DBAL\DriverManager;
13
use Doctrine\ORM\Cache\CacheConfiguration;
14
use Doctrine\ORM\Cache\CacheFactory;
15
use Doctrine\ORM\Cache\DefaultCacheFactory;
16
use Doctrine\ORM\Cache\Logging\StatisticsCacheLogger;
17
use Doctrine\ORM\Configuration;
18
use Doctrine\ORM\EntityManagerInterface;
19
use Doctrine\ORM\Mapping\ClassMetadataFactory;
20
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
21
use Doctrine\ORM\Mapping\MetadataCollection;
22
use Doctrine\ORM\Proxy\Factory\ProxyFactory;
23
use function is_array;
24
use function realpath;
25
26
/**
27
 * Base testcase class for all ORM testcases.
28
 */
29
abstract class OrmTestCase extends DoctrineTestCase
30
{
31
    /**
32
     * The metadata cache that is shared between all ORM tests (except functional tests).
33
     *
34
     * @var Cache|null
35
     */
36
    private static $metadataCacheImpl = null;
37
38
    /**
39
     * The query cache that is shared between all ORM tests (except functional tests).
40
     *
41
     * @var Cache|null
42
     */
43
    private static $queryCacheImpl = null;
44
45
    /** @var bool */
46
    protected $isSecondLevelCacheEnabled = false;
47
48
    /** @var bool */
49
    protected $isSecondLevelCacheLogEnabled = false;
50
51
    /** @var CacheFactory */
52
    protected $secondLevelCacheFactory;
53
54
    /** @var StatisticsCacheLogger */
55
    protected $secondLevelCacheLogger;
56
57
    /** @var Cache|null */
58
    protected $secondLevelCacheDriverImpl;
59
60
    /**
61
     * @param array $paths
62
     *
63
     * @return AnnotationDriver
64
     */
65
    protected function createAnnotationDriver($paths = [])
66
    {
67
        $reader = new Annotations\CachedReader(new Annotations\AnnotationReader(), new ArrayCache());
68
69
        Annotations\AnnotationRegistry::registerFile(__DIR__ . '/../../../lib/Doctrine/ORM/Annotation/DoctrineAnnotations.php');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...egistry::registerFile() has been deprecated: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists') ( Ignorable by Annotation )

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

69
        /** @scrutinizer ignore-deprecated */ Annotations\AnnotationRegistry::registerFile(__DIR__ . '/../../../lib/Doctrine/ORM/Annotation/DoctrineAnnotations.php');

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...
70
71
        return new AnnotationDriver($reader, (array) $paths);
72
    }
73
74
    /**
75
     * Creates an EntityManager for testing purposes.
76
     *
77
     * NOTE: The created EntityManager will have its dependant DBAL parts completely
78
     * mocked out using a DriverMock, ConnectionMock, etc. These mocks can then
79
     * be configured in the tests to simulate the DBAL behavior that is desired
80
     * for a particular test,
81
     *
82
     * @param Connection|array  $conn
83
     * @param EventManager|null $eventManager
84
     * @param bool              $withSharedMetadata
85
     *
86
     * @return EntityManagerInterface
87
     */
88
    protected function getTestEntityManager(
89
        $conn = null,
90
        $conf = null,
0 ignored issues
show
Unused Code introduced by
The parameter $conf is not used and could be removed. ( Ignorable by Annotation )

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

90
        /** @scrutinizer ignore-unused */ $conf = null,

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

Loading history...
91
        $eventManager = null,
92
        $withSharedMetadata = true,
93
        MetadataCollection $metadatas = null
0 ignored issues
show
introduced by
Parameter $metadatas has null default value, but is not marked as nullable.
Loading history...
94
    ) {
95
        $metadataCache = $withSharedMetadata
96
            ? self::getSharedMetadataCacheImpl()
97
            : new ArrayCache();
98
99
        $config = new Configuration();
100
101
        $config->setMetadataCacheImpl($metadataCache);
102
        $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([]));
103
        $config->setQueryCacheImpl(self::getSharedQueryCacheImpl());
104
        $config->setProxyNamespace('Doctrine\Tests\Proxies');
105
        $config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL);
106
        $config->setMetadataDriverImpl(
107
            $config->newDefaultAnnotationDriver([
108
                realpath(__DIR__ . '/Models/Cache'),
109
                realpath(__DIR__ . '/Models/DDC1476'),
110
                realpath(__DIR__ . '/Models/DDC3231'),
111
                realpath(__DIR__ . '/Models/DDC3711'),
112
                realpath(__DIR__ . '/Models/DDC869'),
113
                realpath(__DIR__ . '/Models/FriendObject'),
114
                realpath(__DIR__ . '/Models/Issue5989'),
115
                realpath(__DIR__ . '/Models/NonPublicSchemaJoins'),
116
                realpath(__DIR__ . '/Models/Quote'),
117
                //realpath(__DIR__ . '/Models/ValueConversionType'),
118
                realpath(__DIR__ . '/Models/CMS'),
119
                realpath(__DIR__ . '/Models/DDC1590'),
120
                realpath(__DIR__ . '/Models/DDC3293'),
121
                realpath(__DIR__ . '/Models/DDC3899'),
122
                //realpath(__DIR__ . '/Models/DDC889'),
123
                realpath(__DIR__ . '/Models/Generic'),
124
                realpath(__DIR__ . '/Models/JoinedInheritanceType'),
125
                realpath(__DIR__ . '/Models/NullDefault'),
126
                realpath(__DIR__ . '/Models/Reflection'),
127
                realpath(__DIR__ . '/Models/ValueObjects'),
128
                realpath(__DIR__ . '/Models/Company'),
129
                realpath(__DIR__ . '/Models/DDC1872'),
130
                realpath(__DIR__ . '/Models/DDC3346'),
131
                //realpath(__DIR__ . '/Models/DDC4006'),
132
                realpath(__DIR__ . '/Models/DDC964'),
133
                realpath(__DIR__ . '/Models/GeoNames'),
134
                realpath(__DIR__ . '/Models/Legacy'),
135
                realpath(__DIR__ . '/Models/OneToOneInverseSideLoad'),
136
                realpath(__DIR__ . '/Models/Routing'),
137
                realpath(__DIR__ . '/Models/VersionedManyToOne'),
138
                realpath(__DIR__ . '/Models/CompositeKeyInheritance'),
139
                //realpath(__DIR__ . '/Models/DDC2372'),
140
                realpath(__DIR__ . '/Models/DDC3579'),
141
                //realpath(__DIR__ . '/Models/DDC5934'),
142
                realpath(__DIR__ . '/Models/DirectoryTree'),
143
                realpath(__DIR__ . '/Models/Global'),
144
                realpath(__DIR__ . '/Models/ManyToManyPersister'),
145
                realpath(__DIR__ . '/Models/OneToOneSingleTableInheritance'),
146
                realpath(__DIR__ . '/Models/StockExchange'),
147
                realpath(__DIR__ . '/Models/VersionedOneToOne'),
148
                //realpath(__DIR__ . '/Models/CustomType'),
149
                realpath(__DIR__ . '/Models/DDC2504'),
150
                realpath(__DIR__ . '/Models/DDC3597'),
151
                //realpath(__DIR__ . '/Models/DDC6412'),
152
                realpath(__DIR__ . '/Models/ECommerce'),
153
                realpath(__DIR__ . '/Models/Hydration'),
154
                //realpath(__DIR__ . '/Models/MixedToOneIdentity'),
155
                realpath(__DIR__ . '/Models/Pagination'),
156
                realpath(__DIR__ . '/Models/Taxi'),
157
                realpath(__DIR__ . '/Models/DDC117'),
158
                realpath(__DIR__ . '/Models/DDC2825'),
159
                realpath(__DIR__ . '/Models/DDC3699'),
160
                realpath(__DIR__ . '/Models/DDC753'),
161
                realpath(__DIR__ . '/Models/Forum'),
162
                realpath(__DIR__ . '/Models/IdentityIsAssociation'),
163
                realpath(__DIR__ . '/Models/Navigation'),
164
                realpath(__DIR__ . '/Models/ProxySpecifics'),
165
                realpath(__DIR__ . '/Models/Tweet'),
166
            ])
167
        );
168
169
        if ($this->isSecondLevelCacheEnabled) {
170
            $cacheConfig = new CacheConfiguration();
171
            $cache       = $this->getSharedSecondLevelCacheDriverImpl();
172
            $factory     = new DefaultCacheFactory($cacheConfig->getRegionsConfiguration(), $cache);
173
174
            $this->secondLevelCacheFactory = $factory;
175
176
            $cacheConfig->setCacheFactory($factory);
177
            $config->setSecondLevelCacheEnabled(true);
178
            $config->setSecondLevelCacheConfiguration($cacheConfig);
179
        }
180
181
        if ($conn === null) {
182
            $conn = [
183
                'driverClass'  => Mocks\DriverMock::class,
184
                'wrapperClass' => Mocks\ConnectionMock::class,
185
                'user'         => 'john',
186
                'password'     => 'wayne',
187
            ];
188
        }
189
190
        if (is_array($conn)) {
191
            $conn = DriverManager::getConnection($conn, $config, $eventManager);
192
        }
193
194
        if ($eventManager === null) {
195
            $eventManager = new EventManager();
196
        }
197
198
        if (! ($metadatas instanceof MetadataCollection)){
199
            $metadataFactory = new ClassMetadataFactory($config, $conn, $eventManager);
200
            $metadatas = MetadataCollection::fromClassMetadatas(...$metadataFactory->getAllMetadata());
0 ignored issues
show
Bug introduced by
$metadataFactory->getAllMetadata() is expanded, but the parameter $firstClass of Doctrine\ORM\Mapping\Met...n::fromClassMetadatas() does not expect variable arguments. ( Ignorable by Annotation )

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

200
            $metadatas = MetadataCollection::fromClassMetadatas(/** @scrutinizer ignore-type */ ...$metadataFactory->getAllMetadata());
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
201
        }
202
203
        return Mocks\EntityManagerMock::create($conn, $config, $eventManager, $metadatas)->getWrappedEntityManager();
204
    }
205
206
    protected function enableSecondLevelCache($log = true)
207
    {
208
        $this->isSecondLevelCacheEnabled    = true;
209
        $this->isSecondLevelCacheLogEnabled = $log;
210
    }
211
212
    /**
213
     * @return Cache
214
     */
215
    private static function getSharedMetadataCacheImpl()
216
    {
217
        if (self::$metadataCacheImpl === null) {
218
            self::$metadataCacheImpl = new ArrayCache();
219
        }
220
221
        return self::$metadataCacheImpl;
222
    }
223
224
    /**
225
     * @return Cache
226
     */
227
    private static function getSharedQueryCacheImpl()
228
    {
229
        if (self::$queryCacheImpl === null) {
230
            self::$queryCacheImpl = new ArrayCache();
231
        }
232
233
        return self::$queryCacheImpl;
234
    }
235
236
    /**
237
     * @return Cache
238
     */
239
    protected function getSharedSecondLevelCacheDriverImpl()
240
    {
241
        if ($this->secondLevelCacheDriverImpl === null) {
242
            $this->secondLevelCacheDriverImpl = new ArrayCache();
243
        }
244
245
        return $this->secondLevelCacheDriverImpl;
246
    }
247
}
248