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

DoctrineOrmTestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
c 1
b 1
f 0
lcom 1
cbo 3
dl 0
loc 82
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getMockSqliteEntityManager() 0 20 3
A getMetadataDriverImplementation() 0 4 1
getUsedEntityFixtures() 0 1 ?
A getMockAnnotatedConfig() 0 9 1
A getEventManager() 0 6 1
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;
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\Driver\AnnotationDriver;
19
use Doctrine\ORM\Tools\SchemaTool;
20
21
abstract class DoctrineOrmTestCase extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var EntityManager
25
     */
26
    protected $em;
27
28
    /**
29
     * EntityManager mock object together with
30
     * annotation mapping driver and pdo_sqlite
31
     * database in memory.
32
     *
33
     * @param EventManager  $evm
34
     * @param Configuration $config
35
     *
36
     * @return EntityManager
37
     */
38
    protected function getMockSqliteEntityManager(EventManager $evm = null, Configuration $config = null)
39
    {
40
        $conn = array(
41
            'driver' => 'pdo_sqlite',
42
            'memory' => true,
43
        );
44
45
        $em = EntityManager::create($conn, $config ?: $this->getMockAnnotatedConfig(),
46
            $evm ?: $this->getEventManager());
47
48
        $schema = array_map(function ($class) use ($em) {
49
            return $em->getClassMetadata($class);
50
        }, (array) $this->getUsedEntityFixtures());
51
52
        $schemaTool = new SchemaTool($em);
53
        $schemaTool->dropSchema(array());
54
        $schemaTool->createSchema($schema);
55
56
        return $this->em = $em;
57
    }
58
59
    /**
60
     * Creates default mapping driver.
61
     *
62
     * @return AnnotationDriver
63
     */
64
    protected function getMetadataDriverImplementation()
65
    {
66
        return new AnnotationDriver(new AnnotationReader());
67
    }
68
69
    /**
70
     * Get a list of used fixture classes.
71
     *
72
     * @return array
73
     */
74
    abstract protected function getUsedEntityFixtures();
75
76
    /**
77
     * Get annotation mapping configuration.
78
     *
79
     * @return Configuration
80
     */
81
    protected function getMockAnnotatedConfig()
82
    {
83
        $config = new Configuration();
84
        $config->setProxyDir(sys_get_temp_dir().'/sonata-translation-bundle');
85
        $config->setProxyNamespace('Proxy');
86
        $config->setMetadataDriverImpl($this->getMetadataDriverImplementation());
87
88
        return $config;
89
    }
90
91
    /**
92
     * Build event manager.
93
     *
94
     * @return EventManager
95
     */
96
    private function getEventManager()
97
    {
98
        $evm = new EventManager();
99
100
        return $evm;
101
    }
102
}
103