Completed
Pull Request — 2.6 (#7750)
by
unknown
09:09
created

Issue7735Test::_getEntityManager()   F

Complexity

Conditions 15
Paths 864

Size

Total Lines 97
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 51
c 0
b 0
f 0
nc 864
nop 2
dl 0
loc 97
rs 1.9388

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Doctrine\Tests\ORM\Functional\Ticket\Issue7735;
3
4
use Doctrine\Common\Cache\ArrayCache;
5
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
6
use Doctrine\DBAL\Driver\Connection;
7
use Doctrine\DBAL\Logging\DebugStack;
8
use Doctrine\ORM\Cache\CacheConfiguration;
9
use Doctrine\ORM\Cache\DefaultCacheFactory;
10
use Doctrine\ORM\Cache\Logging\StatisticsCacheLogger;
11
use Doctrine\ORM\Configuration;
12
use Doctrine\ORM\EntityManager;
13
use Doctrine\ORM\Tools\DebugUnitOfWorkListener;
14
use Doctrine\Tests\EventListener\CacheMetadataListener;
15
use Doctrine\Tests\OrmFunctionalTestCase;
16
17
/**
18
 * Class Issue7735Test
19
 * @package Doctrine\Tests\ORM\Functional\Ticket\Issue7735
20
 */
21
class Issue7735Test extends OrmFunctionalTestCase
22
{
23
    protected const DEFAULT_CACHE_REGION_LIFE_TIME = 1;
24
25
    /**
26
     * The metadata cache shared between all functional tests.
27
     *
28
     * @var \Doctrine\Common\Cache\Cache|null
29
     */
30
    private static $_metadataCacheImpl = null;
31
32
    /**
33
     * The query cache shared between all functional tests.
34
     *
35
     * @var \Doctrine\Common\Cache\Cache|null
36
     */
37
    private static $_queryCacheImpl = null;
38
39
    /**
40
     * @var \Doctrine\ORM\Cache
41
     */
42
    protected $cache;
43
44
    public function setUp()
45
    {
46
        $this->enableSecondLevelCache();
47
        $this->useModelSet('issue7735');
48
        parent::setUp();
49
        $this->cache = $this->_em->getCache();
50
51
52
        $engine = new Engine('turbo');
53
        $power = new Power($engine);
54
        $car = new Car($engine);
55
56
        $this->_em->persist($car);
57
        $this->_em->persist($power);
58
        $this->_em->persist($engine);
59
60
        $this->_em->flush();
61
        $this->_em->clear();
62
        $this->evictRegions();
63
64
    }
65
66
    public function testFindByReturnsCachedEntity()
67
    {
68
        for ($i = 1000000; $i > 0; $i--) {
69
            $carRepository = $this->_em->getRepository(Car::class);
70
            $car = $carRepository->find(1);
71
            $engine = $car->getEngine();
72
            $power = $engine->getPower();
0 ignored issues
show
Unused Code introduced by
The assignment to $power is dead and can be removed.
Loading history...
73
            $model = $engine->getModel();
74
            $this->assertNotEmpty($model);
75
            $this->_em->clear();
76
        }
77
    }
78
79
    private function evictRegions()
80
    {
81
        $this->cache->evictQueryRegions();
82
        $this->cache->evictEntityRegions();
83
        $this->cache->evictCollectionRegions();
84
    }
85
86
    /**
87
     * Gets an EntityManager for testing purposes.
88
     *
89
     * @return EntityManager
90
     *
91
     * @throws \Doctrine\ORM\ORMException
92
     */
93
    protected function _getEntityManager(
94
        Connection $connection = null,
95
        MappingDriver $mappingDriver = null
96
    )
97
    {
98
        // NOTE: Functional tests use their own shared metadata cache, because
99
        // the actual database platform used during execution has effect on some
100
        // metadata mapping behaviors (like the choice of the ID generation).
101
        if (is_null(self::$_metadataCacheImpl)) {
102
            if (isset($GLOBALS['DOCTRINE_CACHE_IMPL'])) {
103
                self::$_metadataCacheImpl = new $GLOBALS['DOCTRINE_CACHE_IMPL'];
104
            } else {
105
                self::$_metadataCacheImpl = new ArrayCache();
106
            }
107
        }
108
109
        if (is_null(self::$_queryCacheImpl)) {
110
            self::$_queryCacheImpl = new ArrayCache();
111
        }
112
113
        $this->_sqlLoggerStack = new DebugStack();
114
        $this->_sqlLoggerStack->enabled = false;
115
116
        //FIXME: two different configs! $conn and the created entity manager have
117
        // different configs.
118
        $config = new Configuration();
119
        $config->setMetadataCacheImpl(self::$_metadataCacheImpl);
120
        $config->setQueryCacheImpl(self::$_queryCacheImpl);
121
        $config->setProxyDir(__DIR__ . '/../../../../Proxies');
122
        $config->setProxyNamespace('Doctrine\Tests\Proxies');
123
124
        if (null !== $this->resultCacheImpl) {
125
            $config->setResultCacheImpl($this->resultCacheImpl);
126
        }
127
128
        $enableSecondLevelCache = getenv('ENABLE_SECOND_LEVEL_CACHE');
129
130
        if ($this->isSecondLevelCacheEnabled || $enableSecondLevelCache) {
131
132
            $cacheConfig = new CacheConfiguration();
133
            $cache = $this->getSharedSecondLevelCacheDriverImpl();
134
135
            $regionConfig = $cacheConfig->getRegionsConfiguration();
136
            $regionConfig->setDefaultLifetime(static::DEFAULT_CACHE_REGION_LIFE_TIME);
137
            $factory = new DefaultCacheFactory($regionConfig, $cache);
138
139
            $this->secondLevelCacheFactory = $factory;
140
141
            if ($this->isSecondLevelCacheLogEnabled) {
142
                $this->secondLevelCacheLogger = new StatisticsCacheLogger();
143
                $cacheConfig->setCacheLogger($this->secondLevelCacheLogger);
144
            }
145
146
            $cacheConfig->setCacheFactory($factory);
147
            $config->setSecondLevelCacheEnabled(true);
148
            $config->setSecondLevelCacheConfiguration($cacheConfig);
149
150
            $this->isSecondLevelCacheEnabled = true;
151
        }
152
153
        $config->setMetadataDriverImpl(
154
            $mappingDriver ?? $config->newDefaultAnnotationDriver(
155
                [
156
                    realpath(__DIR__ . '/../../../../Models/Cache'),
157
                    realpath(__DIR__ . '/../../../../Models/GeoNames')
158
                ],
159
                true
160
            )
161
        );
162
163
        $conn = $connection ?: static::$_sharedConn;
164
        $conn->getConfiguration()->setSQLLogger($this->_sqlLoggerStack);
0 ignored issues
show
Bug introduced by
The method getConfiguration() does not exist on null. ( Ignorable by Annotation )

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

164
        $conn->/** @scrutinizer ignore-call */ 
165
               getConfiguration()->setSQLLogger($this->_sqlLoggerStack);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getConfiguration() does not exist on Doctrine\DBAL\Driver\Connection. It seems like you code against a sub-type of Doctrine\DBAL\Driver\Connection such as Doctrine\DBAL\Connection. ( Ignorable by Annotation )

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

164
        $conn->/** @scrutinizer ignore-call */ 
165
               getConfiguration()->setSQLLogger($this->_sqlLoggerStack);
Loading history...
165
166
        // get rid of more global state
167
        $evm = $conn->getEventManager();
0 ignored issues
show
Bug introduced by
The method getEventManager() does not exist on Doctrine\DBAL\Driver\Connection. It seems like you code against a sub-type of Doctrine\DBAL\Driver\Connection such as Doctrine\DBAL\Connection. ( Ignorable by Annotation )

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

167
        /** @scrutinizer ignore-call */ 
168
        $evm = $conn->getEventManager();
Loading history...
168
        foreach ($evm->getListeners() AS $event => $listeners) {
169
            foreach ($listeners AS $listener) {
170
                $evm->removeEventListener([$event], $listener);
171
            }
172
        }
173
174
        if ($enableSecondLevelCache) {
175
            $evm->addEventListener('loadClassMetadata', new CacheMetadataListener());
176
        }
177
178
        if (isset($GLOBALS['db_event_subscribers'])) {
179
            foreach (explode(",", $GLOBALS['db_event_subscribers']) AS $subscriberClass) {
180
                $subscriberInstance = new $subscriberClass();
181
                $evm->addEventSubscriber($subscriberInstance);
182
            }
183
        }
184
185
        if (isset($GLOBALS['debug_uow_listener'])) {
186
            $evm->addEventListener(['onFlush'], new DebugUnitOfWorkListener());
187
        }
188
189
        return EntityManager::create($conn, $config);
190
    }
191
}
192