OrmTestCase   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 49
c 2
b 0
f 0
dl 0
loc 147
rs 10
wmc 13

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
A getTestEntityManager() 0 47 5
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\Mapping\Driver\AnnotationDriver;
19
use Doctrine\ORM\Proxy\Factory\ProxyFactory;
20
use function is_array;
21
use function realpath;
22
23
/**
24
 * Base testcase class for all ORM testcases.
25
 */
26
abstract class OrmTestCase extends DoctrineTestCase
27
{
28
    /**
29
     * The metadata cache that is shared between all ORM tests (except functional tests).
30
     *
31
     * @var Cache|null
32
     */
33
    private static $metadataCacheImpl = null;
34
35
    /**
36
     * The query cache that is shared between all ORM tests (except functional tests).
37
     *
38
     * @var Cache|null
39
     */
40
    private static $queryCacheImpl = null;
41
42
    /** @var bool */
43
    protected $isSecondLevelCacheEnabled = false;
44
45
    /** @var bool */
46
    protected $isSecondLevelCacheLogEnabled = false;
47
48
    /** @var CacheFactory */
49
    protected $secondLevelCacheFactory;
50
51
    /** @var StatisticsCacheLogger */
52
    protected $secondLevelCacheLogger;
53
54
    /** @var Cache|null */
55
    protected $secondLevelCacheDriverImpl;
56
57
    /**
58
     * @param array $paths
59
     *
60
     * @return AnnotationDriver
61
     */
62
    protected function createAnnotationDriver($paths = [])
63
    {
64
        $reader = new Annotations\CachedReader(new Annotations\AnnotationReader(), new ArrayCache());
65
66
        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. Annotations will be autoloaded in 2.0. ( Ignorable by Annotation )

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

66
        /** @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...
67
68
        return new AnnotationDriver($reader, (array) $paths);
69
    }
70
71
    /**
72
     * Creates an EntityManager for testing purposes.
73
     *
74
     * NOTE: The created EntityManager will have its dependant DBAL parts completely
75
     * mocked out using a DriverMock, ConnectionMock, etc. These mocks can then
76
     * be configured in the tests to simulate the DBAL behavior that is desired
77
     * for a particular test,
78
     *
79
     * @param Connection|array  $conn
80
     * @param mixed             $conf
81
     * @param EventManager|null $eventManager
82
     * @param bool              $withSharedMetadata
83
     */
84
    protected function getTestEntityManager(
85
        $conn = null,
86
        $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

86
        /** @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...
87
        $eventManager = null,
88
        $withSharedMetadata = true
89
    ) : Mocks\EntityManagerMock {
90
        $metadataCache = $withSharedMetadata
91
            ? self::getSharedMetadataCacheImpl()
92
            : new ArrayCache();
93
94
        $config = new Configuration();
95
96
        $config->setMetadataCacheImpl($metadataCache);
97
        $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([]));
98
        $config->setQueryCacheImpl(self::getSharedQueryCacheImpl());
99
        $config->setProxyNamespace('Doctrine\Tests\Proxies');
100
        $config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL);
101
        $config->setMetadataDriverImpl(
102
            $config->newDefaultAnnotationDriver([realpath(__DIR__ . '/Models/Cache')])
103
        );
104
105
        if ($this->isSecondLevelCacheEnabled) {
106
            $cacheConfig = new CacheConfiguration();
107
            $cache       = $this->getSharedSecondLevelCacheDriverImpl();
108
            $factory     = new DefaultCacheFactory($cacheConfig->getRegionsConfiguration(), $cache);
109
110
            $this->secondLevelCacheFactory = $factory;
111
112
            $cacheConfig->setCacheFactory($factory);
113
            $config->setSecondLevelCacheEnabled(true);
114
            $config->setSecondLevelCacheConfiguration($cacheConfig);
115
        }
116
117
        if ($conn === null) {
118
            $conn = [
119
                'driverClass'  => Mocks\DriverMock::class,
120
                'wrapperClass' => Mocks\ConnectionMock::class,
121
                'user'         => 'john',
122
                'password'     => 'wayne',
123
            ];
124
        }
125
126
        if (is_array($conn)) {
127
            $conn = DriverManager::getConnection($conn, $config, $eventManager);
128
        }
129
130
        return Mocks\EntityManagerMock::create($conn, $config, $eventManager);
131
    }
132
133
    protected function enableSecondLevelCache($log = true)
134
    {
135
        $this->isSecondLevelCacheEnabled    = true;
136
        $this->isSecondLevelCacheLogEnabled = $log;
137
    }
138
139
    /**
140
     * @return Cache
141
     */
142
    private static function getSharedMetadataCacheImpl()
143
    {
144
        if (self::$metadataCacheImpl === null) {
145
            self::$metadataCacheImpl = new ArrayCache();
146
        }
147
148
        return self::$metadataCacheImpl;
149
    }
150
151
    /**
152
     * @return Cache
153
     */
154
    private static function getSharedQueryCacheImpl()
155
    {
156
        if (self::$queryCacheImpl === null) {
157
            self::$queryCacheImpl = new ArrayCache();
158
        }
159
160
        return self::$queryCacheImpl;
161
    }
162
163
    /**
164
     * @return Cache
165
     */
166
    protected function getSharedSecondLevelCacheDriverImpl()
167
    {
168
        if ($this->secondLevelCacheDriverImpl === null) {
169
            $this->secondLevelCacheDriverImpl = new ArrayCache();
170
        }
171
172
        return $this->secondLevelCacheDriverImpl;
173
    }
174
}
175