SetupTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 47
c 3
b 0
f 0
dl 0
loc 125
rs 10
wmc 14

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 11 4
A testConfigureCache() 0 8 1
A makeTemporaryDirectory() 0 8 1
A testConfigureProxyDir() 0 5 1
A testConfigureCacheCustomInstance() 0 8 1
A testXMLConfiguration() 0 6 1
A testConfiguredCacheNamespaceShouldBeUsedAsPrefixOfGeneratedNamespace() 0 10 1
A testCacheNamespaceShouldBeGeneratedWhenCacheIsNotGiven() 0 6 1
A testCacheNamespaceShouldBeGeneratedWhenCacheIsGivenButHasNoNamespace() 0 6 1
A testAnnotationConfiguration() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Tools;
6
7
use Doctrine\Common\Cache\ArrayCache;
8
use Doctrine\Common\Cache\Cache;
9
use Doctrine\ORM\Configuration;
10
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
11
use Doctrine\ORM\Mapping\Driver\XmlDriver;
12
use Doctrine\ORM\Tools\Setup;
13
use Doctrine\Tests\OrmTestCase;
14
use function count;
15
use function get_include_path;
16
use function md5;
17
use function mkdir;
18
use function set_include_path;
19
use function spl_autoload_functions;
20
use function spl_autoload_unregister;
21
use function sys_get_temp_dir;
22
use function tempnam;
23
use function unlink;
24
25
class SetupTest extends OrmTestCase
26
{
27
    private $originalAutoloaderCount;
28
    private $originalIncludePath;
29
30
    public function setUp() : void
31
    {
32
        $this->originalAutoloaderCount = count(spl_autoload_functions());
33
        $this->originalIncludePath     = get_include_path();
34
    }
35
36
    public function tearDown() : void
37
    {
38
        if (! $this->originalIncludePath) {
39
            return;
40
        }
41
42
        set_include_path($this->originalIncludePath);
43
44
        foreach (spl_autoload_functions() as $i => $loader) {
45
            if ($i > $this->originalAutoloaderCount + 1) {
46
                spl_autoload_unregister($loader);
47
            }
48
        }
49
    }
50
51
    public function testAnnotationConfiguration() : void
52
    {
53
        $config = Setup::createAnnotationMetadataConfiguration([], true);
54
55
        self::assertInstanceOf(Configuration::class, $config);
56
        self::assertEquals(sys_get_temp_dir(), $config->getProxyManagerConfiguration()->getProxiesTargetDir());
57
        self::assertEquals('DoctrineProxies', $config->getProxyManagerConfiguration()->getProxiesNamespace());
58
        self::assertInstanceOf(AnnotationDriver::class, $config->getMetadataDriverImpl());
59
    }
60
61
    public function testXMLConfiguration() : void
62
    {
63
        $config = Setup::createXMLMetadataConfiguration([], true);
64
65
        self::assertInstanceOf(Configuration::class, $config);
66
        self::assertInstanceOf(XmlDriver::class, $config->getMetadataDriverImpl());
67
    }
68
69
    /**
70
     * @group 5904
71
     */
72
    public function testCacheNamespaceShouldBeGeneratedWhenCacheIsNotGiven() : void
73
    {
74
        $config = Setup::createConfiguration(false, __DIR__);
75
        $cache  = $config->getMetadataCacheImpl();
76
77
        self::assertSame('dc2_' . md5(__DIR__) . '_', $cache->getNamespace());
78
    }
79
80
    /**
81
     * @group 5904
82
     */
83
    public function testCacheNamespaceShouldBeGeneratedWhenCacheIsGivenButHasNoNamespace() : void
84
    {
85
        $config = Setup::createConfiguration(false, __DIR__, new ArrayCache());
86
        $cache  = $config->getMetadataCacheImpl();
87
88
        self::assertSame('dc2_' . md5(__DIR__) . '_', $cache->getNamespace());
89
    }
90
91
    /**
92
     * @group 5904
93
     */
94
    public function testConfiguredCacheNamespaceShouldBeUsedAsPrefixOfGeneratedNamespace() : void
95
    {
96
        $originalCache = new ArrayCache();
97
        $originalCache->setNamespace('foo');
98
99
        $config = Setup::createConfiguration(false, __DIR__, $originalCache);
100
        $cache  = $config->getMetadataCacheImpl();
101
102
        self::assertSame($originalCache, $cache);
103
        self::assertSame('foo:dc2_' . md5(__DIR__) . '_', $cache->getNamespace());
104
    }
105
106
    /**
107
     * @group DDC-1350
108
     */
109
    public function testConfigureProxyDir() : void
110
    {
111
        $path   = $this->makeTemporaryDirectory();
112
        $config = Setup::createAnnotationMetadataConfiguration([], true, $path);
113
        self::assertSame($path, $config->getProxyManagerConfiguration()->getProxiesTargetDir());
114
    }
115
116
    /**
117
     * @group DDC-1350
118
     */
119
    public function testConfigureCache() : void
120
    {
121
        $cache  = new ArrayCache();
122
        $config = Setup::createAnnotationMetadataConfiguration([], true, null, $cache);
123
124
        self::assertSame($cache, $config->getResultCacheImpl());
125
        self::assertSame($cache, $config->getMetadataCacheImpl());
126
        self::assertSame($cache, $config->getQueryCacheImpl());
127
    }
128
129
    /**
130
     * @group DDC-3190
131
     */
132
    public function testConfigureCacheCustomInstance() : void
133
    {
134
        $cache  = $this->createMock(Cache::class);
135
        $config = Setup::createConfiguration(true, null, $cache);
136
137
        self::assertSame($cache, $config->getResultCacheImpl());
138
        self::assertSame($cache, $config->getMetadataCacheImpl());
139
        self::assertSame($cache, $config->getQueryCacheImpl());
140
    }
141
142
    private function makeTemporaryDirectory() : string
143
    {
144
        $path = tempnam(sys_get_temp_dir(), 'foo');
145
146
        unlink($path);
147
        mkdir($path);
148
149
        return $path;
150
    }
151
}
152