Completed
Pull Request — 2.x (#121)
by
unknown
02:18
created

BaseTestCaseORM::getMockAnnotatedConfig()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 82
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 82
rs 8.5076
cc 4
eloc 51
nc 3
nop 0

How to fix   Long Method   

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
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\TranslationBundle\Tests\Tool;
13
14
use Doctrine\Common\Annotations\AnnotationReader;
15
use Doctrine\Common\EventManager;
16
use Doctrine\ORM\Configuration;
17
use Doctrine\ORM\EntityManager;
18
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
19
use Doctrine\ORM\Mapping\DefaultQuoteStrategy;
20
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
21
use Doctrine\ORM\Repository\DefaultRepositoryFactory;
22
use Doctrine\ORM\Tools\SchemaTool;
23
use Gedmo\Translatable\TranslatableListener;
24
25
/**
26
 * Base test case contains common mock objects
27
 * and functionality among all extensions using
28
 * ORM object manager.
29
 *
30
 * @author Gediminas Morkevicius <[email protected]>
31
 *
32
 * @link http://www.gediminasm.org
33
 *
34
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
35
 */
36
abstract class BaseTestCaseORM extends \PHPUnit_Framework_TestCase
37
{
38
    /**
39
     * @var EntityManager
40
     */
41
    protected $em;
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function setUp()
47
    {
48
    }
49
50
    /**
51
     * EntityManager mock object together with
52
     * annotation mapping driver and pdo_sqlite
53
     * database in memory.
54
     *
55
     * @param EventManager $evm
56
     *
57
     * @return EntityManager
58
     */
59
    protected function getMockSqliteEntityManager(EventManager $evm = null, Configuration $config = null)
60
    {
61
        $conn = array(
62
            'driver' => 'pdo_sqlite',
63
            'memory' => true,
64
        );
65
66
        $config = null === $config ? $this->getMockAnnotatedConfig() : $config;
67
        $em = EntityManager::create($conn, $config, $evm ?: $this->getEventManager());
68
69
        $schema = array_map(function ($class) use ($em) {
70
            return $em->getClassMetadata($class);
71
        }, (array) $this->getUsedEntityFixtures());
72
73
        $schemaTool = new SchemaTool($em);
74
        $schemaTool->dropSchema(array());
75
        $schemaTool->createSchema($schema);
76
77
        return $this->em = $em;
78
    }
79
80
    /**
81
     * EntityManager mock object together with
82
     * annotation mapping driver and custom
83
     * connection.
84
     *
85
     * @param array        $conn
86
     * @param EventManager $evm
87
     *
88
     * @return EntityManager
89
     */
90
    protected function getMockCustomEntityManager(array $conn, EventManager $evm = null)
91
    {
92
        $config = $this->getMockAnnotatedConfig();
93
        $em = EntityManager::create($conn, $config, $evm ?: $this->getEventManager());
94
95
        $schema = array_map(function ($class) use ($em) {
96
            return $em->getClassMetadata($class);
97
        }, (array) $this->getUsedEntityFixtures());
98
99
        $schemaTool = new SchemaTool($em);
100
        $schemaTool->dropSchema(array());
101
        $schemaTool->createSchema($schema);
102
103
        return $this->em = $em;
104
    }
105
106
    /**
107
     * EntityManager mock object with
108
     * annotation mapping driver.
109
     *
110
     * @param EventManager $evm
111
     *
112
     * @return EntityManager
113
     */
114
    protected function getMockMappedEntityManager(EventManager $evm = null)
115
    {
116
        $driver = $this->getMock('Doctrine\DBAL\Driver');
117
        $driver->expects($this->once())
118
            ->method('getDatabasePlatform')
119
            ->will($this->returnValue($this->getMock('Doctrine\DBAL\Platforms\MySqlPlatform')));
120
121
        $conn = $this->getMock('Doctrine\DBAL\Connection', array(), array(array(), $driver));
122
        $conn->expects($this->once())
123
            ->method('getEventManager')
124
            ->will($this->returnValue($evm ?: $this->getEventManager()));
125
126
        $config = $this->getMockAnnotatedConfig();
127
        $this->em = EntityManager::create($conn, $config);
128
129
        return $this->em;
130
    }
131
132
    /**
133
     * Creates default mapping driver.
134
     *
135
     * @return \Doctrine\ORM\Mapping\Driver\Driver
136
     */
137
    protected function getMetadataDriverImplementation()
138
    {
139
        return new AnnotationDriver(new AnnotationReader());
140
    }
141
142
    /**
143
     * Get a list of used fixture classes.
144
     *
145
     * @return array
146
     */
147
    abstract protected function getUsedEntityFixtures();
148
149
    /**
150
     * Get annotation mapping configuration.
151
     *
152
     * @return \Doctrine\ORM\Configuration
153
     */
154
    protected function getMockAnnotatedConfig()
155
    {
156
        // We need to mock every method except the ones which
157
        // handle the filters
158
        $configurationClass = 'Doctrine\ORM\Configuration';
159
        $refl = new \ReflectionClass($configurationClass);
160
        $methods = $refl->getMethods();
161
162
        $mockMethods = array();
163
164
        foreach ($methods as $method) {
165
            if ($method->name !== 'addFilter' && $method->name !== 'getFilterClassName') {
166
                $mockMethods[] = $method->name;
167
            }
168
        }
169
170
        $config = $this->getMock($configurationClass, $mockMethods);
171
172
        $config
173
            ->expects($this->once())
174
            ->method('getProxyDir')
175
            ->will($this->returnValue(__DIR__.'/../temp'))
176
        ;
177
178
        $config
179
            ->expects($this->once())
180
            ->method('getProxyNamespace')
181
            ->will($this->returnValue('Proxy'))
182
        ;
183
184
        $config
185
            ->expects($this->any())
186
            ->method('getDefaultQueryHints')
187
            ->will($this->returnValue(array()))
188
        ;
189
190
        $config
191
            ->expects($this->once())
192
            ->method('getAutoGenerateProxyClasses')
193
            ->will($this->returnValue(true))
194
        ;
195
196
        $config
197
            ->expects($this->once())
198
            ->method('getClassMetadataFactoryName')
199
            ->will($this->returnValue('Doctrine\\ORM\\Mapping\\ClassMetadataFactory'))
200
        ;
201
202
        $mappingDriver = $this->getMetadataDriverImplementation();
203
204
        $config
205
            ->expects($this->any())
206
            ->method('getMetadataDriverImpl')
207
            ->will($this->returnValue($mappingDriver))
208
        ;
209
210
        $config
211
            ->expects($this->any())
212
            ->method('getDefaultRepositoryClassName')
213
            ->will($this->returnValue('Doctrine\\ORM\\EntityRepository'))
214
        ;
215
216
        $config
217
            ->expects($this->any())
218
            ->method('getQuoteStrategy')
219
            ->will($this->returnValue(new DefaultQuoteStrategy()))
220
        ;
221
222
        $config
223
            ->expects($this->any())
224
            ->method('getNamingStrategy')
225
            ->will($this->returnValue(new DefaultNamingStrategy()))
226
        ;
227
228
        $config
229
            ->expects($this->once())
230
            ->method('getRepositoryFactory')
231
            ->will($this->returnValue(new DefaultRepositoryFactory()))
232
        ;
233
234
        return $config;
235
    }
236
237
    /**
238
     * Build event manager.
239
     *
240
     * @return EventManager
241
     */
242
    private function getEventManager()
243
    {
244
        $evm = new EventManager();
245
        $evm->addEventSubscriber(new TranslatableListener());
246
247
        return $evm;
248
    }
249
}
250