Failed Conditions
Pull Request — master (#7688)
by Gabriel
09:43
created

OrmTestCase   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 57
dl 0
loc 165
rs 10
c 3
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createAnnotationDriver() 0 7 1
A getTestEntityManager() 0 47 5
A assertAttributeEmpty() 0 7 1
A getSharedQueryCacheImpl() 0 7 2
A getSharedMetadataCacheImpl() 0 7 2
A enableSecondLevelCache() 0 4 1
A assertAttributeSame() 0 7 1
A getSharedSecondLevelCacheDriverImpl() 0 7 2
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
use ReflectionClass;
0 ignored issues
show
Coding Style introduced by
Use statements should be sorted alphabetically. The first wrong one is ReflectionClass.
Loading history...
23
24
/**
25
 * Base testcase class for all ORM testcases.
26
 */
27
abstract class OrmTestCase extends DoctrineTestCase
28
{
29
    /**
30
     * The metadata cache that is shared between all ORM tests (except functional tests).
31
     *
32
     * @var Cache|null
33
     */
34
    private static $metadataCacheImpl = null;
35
36
    /**
37
     * The query cache that is shared between all ORM tests (except functional tests).
38
     *
39
     * @var Cache|null
40
     */
41
    private static $queryCacheImpl = null;
42
43
    /** @var bool */
44
    protected $isSecondLevelCacheEnabled = false;
45
46
    /** @var bool */
47
    protected $isSecondLevelCacheLogEnabled = false;
48
49
    /** @var CacheFactory */
50
    protected $secondLevelCacheFactory;
51
52
    /** @var StatisticsCacheLogger */
53
    protected $secondLevelCacheLogger;
54
55
    /** @var Cache|null */
56
    protected $secondLevelCacheDriverImpl;
57
58
    /**
59
     * @param array $paths
60
     *
61
     * @return AnnotationDriver
62
     */
63
    protected function createAnnotationDriver($paths = [])
64
    {
65
        $reader = new Annotations\CachedReader(new Annotations\AnnotationReader(), new ArrayCache());
66
67
        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

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

87
        /** @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...
88
        $eventManager = null,
89
        $withSharedMetadata = true
90
    ) : Mocks\EntityManagerMock {
91
        $metadataCache = $withSharedMetadata
92
            ? self::getSharedMetadataCacheImpl()
93
            : new ArrayCache();
94
95
        $config = new Configuration();
96
97
        $config->setMetadataCacheImpl($metadataCache);
98
        $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([]));
99
        $config->setQueryCacheImpl(self::getSharedQueryCacheImpl());
100
        $config->setProxyNamespace('Doctrine\Tests\Proxies');
101
        $config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL);
102
        $config->setMetadataDriverImpl(
103
            $config->newDefaultAnnotationDriver([realpath(__DIR__ . '/Models/Cache')])
104
        );
105
106
        if ($this->isSecondLevelCacheEnabled) {
107
            $cacheConfig = new CacheConfiguration();
108
            $cache       = $this->getSharedSecondLevelCacheDriverImpl();
109
            $factory     = new DefaultCacheFactory($cacheConfig->getRegionsConfiguration(), $cache);
110
111
            $this->secondLevelCacheFactory = $factory;
112
113
            $cacheConfig->setCacheFactory($factory);
114
            $config->setSecondLevelCacheEnabled(true);
115
            $config->setSecondLevelCacheConfiguration($cacheConfig);
116
        }
117
118
        if ($conn === null) {
119
            $conn = [
120
                'driverClass'  => Mocks\DriverMock::class,
121
                'wrapperClass' => Mocks\ConnectionMock::class,
122
                'user'         => 'john',
123
                'password'     => 'wayne',
124
            ];
125
        }
126
127
        if (is_array($conn)) {
128
            $conn = DriverManager::getConnection($conn, $config, $eventManager);
129
        }
130
131
        return Mocks\EntityManagerMock::create($conn, $config, $eventManager);
132
    }
133
134
    protected function enableSecondLevelCache($log = true)
135
    {
136
        $this->isSecondLevelCacheEnabled    = true;
137
        $this->isSecondLevelCacheLogEnabled = $log;
138
    }
139
140
    /**
141
     * @return Cache
142
     */
143
    private static function getSharedMetadataCacheImpl()
144
    {
145
        if (self::$metadataCacheImpl === null) {
146
            self::$metadataCacheImpl = new ArrayCache();
147
        }
148
149
        return self::$metadataCacheImpl;
150
    }
151
152
    /**
153
     * @return Cache
154
     */
155
    private static function getSharedQueryCacheImpl()
156
    {
157
        if (self::$queryCacheImpl === null) {
158
            self::$queryCacheImpl = new ArrayCache();
159
        }
160
161
        return self::$queryCacheImpl;
162
    }
163
164
    /**
165
     * @return Cache
166
     */
167
    protected function getSharedSecondLevelCacheDriverImpl()
168
    {
169
        if ($this->secondLevelCacheDriverImpl === null) {
170
            $this->secondLevelCacheDriverImpl = new ArrayCache();
171
        }
172
173
        return $this->secondLevelCacheDriverImpl;
174
    }
175
176
    public static function assertAttributeEmpty(string $haystackAttributeName, $haystackClassOrObject, string $message = ''): void
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
177
    {
178
        $class = new ReflectionClass($haystackClassOrObject);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
179
        $property = $class->getProperty($haystackAttributeName);
180
        $property->setAccessible(true);
181
182
        self::assertEmpty($property->getValue($haystackClassOrObject));
183
    }
184
185
    public static function assertAttributeSame($expected, string $actualAttributeName, $actualClassOrObject, string $message = ''): void
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
186
    {
187
        $class = new ReflectionClass($actualClassOrObject);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
188
        $property = $class->getProperty($actualAttributeName);
189
        $property->setAccessible(true);
190
191
        self::assertSame($expected, $property->getValue($actualClassOrObject));
192
    }
193
}
194