Completed
Push — master ( 0d0a18...6997ac )
by Julián
02:15
created

RelationalBuilderTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 205
rs 10
c 3
b 0
f 0
1
<?php
2
/**
3
 * doctrine-manager-builder (https://github.com/juliangut/doctrine-manager-builder)
4
 * Doctrine2 managers builder
5
 *
6
 * @license BSD-3-Clause
7
 * @author Julián Gutiérrez <[email protected]>
8
 */
9
10
namespace Jgut\Doctrine\ManagerBuilder\Tests;
11
12
use Doctrine\Common\Cache\ArrayCache;
13
use Doctrine\Common\Cache\CacheProvider;
14
use Doctrine\Common\Cache\VoidCache;
15
use Doctrine\Common\Persistence\Mapping\Driver\StaticPHPDriver;
16
use Doctrine\DBAL\Logging\EchoSQLLogger;
17
use Doctrine\DBAL\Types\StringType;
18
use Doctrine\ORM\EntityManager;
19
use Jgut\Doctrine\ManagerBuilder\ManagerBuilder;
20
use Jgut\Doctrine\ManagerBuilder\RelationalBuilder;
21
use Symfony\Component\Console\Command\Command;
22
use Symfony\Component\Console\Helper\HelperSet;
23
24
/**
25
 * Relational entity builder tests.
26
 *
27
 * @group relational
28
 */
29
class RelationalBuilderTest extends \PHPUnit_Framework_TestCase
30
{
31
    /**
32
     * @var RelationalBuilder
33
     */
34
    protected $builder;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function setUp()
40
    {
41
        $this->builder = new RelationalBuilder([], 'test');
42
    }
43
44
    public function testQueryCache()
45
    {
46
        $cacheDriver = $this->getMockBuilder(VoidCache::class)
47
            ->disableOriginalConstructor()
48
            ->setMethodsExcept(['getNamespace', 'setNamespace'])
49
            ->getMock();
50
51
        $this->builder->setOption('query_cache_driver', $cacheDriver);
52
        $this->builder->setOption('query_cache_namespace', 'namespace');
53
54
        /* @var CacheProvider $driver */
55
        $driver = $this->builder->getQueryCacheDriver();
56
        self::assertEquals($cacheDriver, $driver);
57
        self::assertEquals('namespace', $driver->getNamespace());
58
59
        /* @var CacheProvider $cacheDriver */
60
        $cacheDriver = $this->getMockBuilder(ArrayCache::class)
61
            ->disableOriginalConstructor()
62
            ->setMethodsExcept(['getNamespace'])
63
            ->getMock();
64
        $this->builder->setQueryCacheDriver($cacheDriver);
65
66
        self::assertEquals($cacheDriver, $this->builder->getQueryCacheDriver());
67
    }
68
69
    public function testResultCache()
70
    {
71
        $cacheDriver = $this->getMockBuilder(VoidCache::class)
72
            ->disableOriginalConstructor()
73
            ->setMethodsExcept(['getNamespace', 'setNamespace'])
74
            ->getMock();
75
76
        $this->builder->setOption('result_cache_driver', $cacheDriver);
77
        $this->builder->setOption('result_cache_namespace', '');
78
79
        self::assertInstanceOf(CacheProvider::class, $this->builder->getResultCacheDriver());
80
81
        /* @var CacheProvider $cacheDriver */
82
        $cacheDriver = $this->getMockBuilder(VoidCache::class)
83
            ->disableOriginalConstructor()
84
            ->getMock();
85
        $this->builder->setResultCacheDriver($cacheDriver);
86
87
        self::assertEquals($cacheDriver, $this->builder->getResultCacheDriver());
88
    }
89
90
    /**
91
     * @expectedException \RuntimeException
92
     * @expectedExceptionMessageRegExp /^".+" file does not exist$/
93
     */
94
    public function testManagerNoAnnotationFile()
95
    {
96
        $this->builder->setOption('annotation_files', __DIR__ . '/fake_file.php');
97
98
        $this->builder->getManager(true);
99
    }
100
101
    /**
102
     * @expectedException \RuntimeException
103
     * @expectedExceptionMessage No metadata mapping defined
104
     */
105
    public function testManagerNoMetadataMapping()
106
    {
107
        $this->builder->getManager();
108
    }
109
110
    /**
111
     * @expectedException \RuntimeException
112
     * @expectedExceptionMessage metadata_mapping must be array with "driver" key or "type" and "path" keys
113
     */
114
    public function testManagerNoMappingDriver()
115
    {
116
        $this->builder->setOption('metadata_mapping', [[]]);
117
118
        $this->builder->getManager(true);
119
    }
120
121
    /**
122
     * @expectedException \RuntimeException
123
     * @expectedExceptionMessageRegExp /^Provided driver should be of the type MappingDriver, ".+" given$/
124
     */
125
    public function testManagerWrongMappingDriver()
126
    {
127
        $this->builder->setOption('metadata_mapping', [__DIR__]);
128
129
        $this->builder->getManager(true);
130
    }
131
132
    /**
133
     * @expectedException \UnexpectedValueException
134
     * @expectedExceptionMessageRegExp /^".+" is not a valid metadata mapping type$/
135
     */
136
    public function testManagerWrongMappingType()
137
    {
138
        $this->builder->setOption('metadata_mapping', [['type' => 'unknown', 'path' => __DIR__]]);
139
140
        $this->builder->getManager(true);
141
    }
142
143
    /**
144
     * @expectedException \RuntimeException
145
     * @expectedExceptionMessage Only one default metadata mapping driver allowed, a namespace must be defined
146
     */
147
    public function testManagerSingleDefaultMapping()
148
    {
149
        $this->builder->setOption(
150
            'metadata_mapping',
151
            [
152
                ['driver' => new StaticPHPDriver([__DIR__])],
153
                ['type' => ManagerBuilder::METADATA_MAPPING_XML, 'path' => __DIR__, 'namespace' => 'namespace'],
154
                ['type' => ManagerBuilder::METADATA_MAPPING_YAML, 'path' => __DIR__],
155
            ]
156
        );
157
158
        $this->builder->getManager(true);
159
    }
160
161
    /**
162
     * @expectedException \Doctrine\DBAL\DBALException
163
     * @expectedExceptionMessageRegExp /^The options 'driver' or 'driverClass' are mandatory if no PDO instance/
164
     */
165
    public function testManagerNoConnection()
166
    {
167
        $this->builder->setOption(
168
            'metadata_mapping',
169
            [
170
                ['type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, 'path' => __DIR__, 'namespace' => 'annotation'],
171
                ['type' => ManagerBuilder::METADATA_MAPPING_XML, 'path' => __DIR__, 'namespace' => 'xml'],
172
                ['type' => ManagerBuilder::METADATA_MAPPING_YAML, 'path' => __DIR__, 'namespace' => 'yaml'],
173
                ['type' => ManagerBuilder::METADATA_MAPPING_PHP, 'path' => __DIR__, 'namespace' => 'php'],
174
            ]
175
        );
176
177
        $this->builder->getManager(true);
178
    }
179
180
    public function testManager()
181
    {
182
        $this->builder->setOption('annotation_files', __FILE__);
183
        $this->builder->setOption('annotation_namespaces', ['namespace' => __FILE__]);
184
        $this->builder->setOption('annotation_autoloaders', ['class_exists']);
185
        $this->builder->setOption('connection', ['driver' => 'pdo_sqlite', 'memory' => true]);
186
        $this->builder->setOption(
187
            'metadata_mapping',
188
            [['type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, 'path' => __DIR__]]
189
        );
190
        $this->builder->setOption('sql_logger', new EchoSQLLogger);
191
        $this->builder->setOption('custom_string_functions', 'string');
192
        $this->builder->setOption('custom_numeric_functions', 'numeric');
193
        $this->builder->setOption('custom_datetime_functions', 'datetime');
194
        $this->builder->setOption('custom_types', ['fake_type' => StringType::class]);
195
196
        static::assertInstanceOf(EntityManager::class, $this->builder->getManager());
197
    }
198
199
    public function testConsoleCommands()
200
    {
201
        $this->builder->setOption('connection', ['driver' => 'pdo_sqlite', 'memory' => true]);
202
        $this->builder->setOption(
203
            'metadata_mapping',
204
            [['type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, 'path' => __DIR__]]
205
        );
206
207
        $commands = $this->builder->getConsoleCommands();
208
209
        return array_walk(
210
            $commands,
211
            function (Command $command) {
212
                static::assertEquals(1, preg_match('/^test:(dbal|orm):/', $command->getName()));
213
            }
214
        );
215
    }
216
217
    public function testConsoleHelperSet()
218
    {
219
        $this->builder->setOption('connection', ['driver' => 'pdo_sqlite', 'memory' => true]);
220
        $this->builder->setOption(
221
            'metadata_mapping',
222
            [['type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, 'path' => __DIR__]]
223
        );
224
225
        $helperSet = $this->builder->getConsoleHelperSet();
226
227
        static::assertInstanceOf(HelperSet::class, $helperSet);
228
        static::assertTrue($helperSet->has('connection'));
229
        static::assertTrue($helperSet->has('db'));
230
        static::assertTrue($helperSet->has('entityManager'));
231
        static::assertTrue($helperSet->has('em'));
232
    }
233
}
234