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

BaseTestCaseORM::getMockCustomEntityManager()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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