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

DoctrineOrmTestCase::getEventManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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
/**
22
 * Base test case contains common mock objects
23
 * and functionality among all tests using
24
 * ORM entity manager.
25
 *
26
 * @author Dariusz Markowicz <[email protected]>
27
 *
28
 * Inspired by BaseTestCaseORM
29
 * @author Gediminas Morkevicius <[email protected]>
30
 */
31
abstract class DoctrineOrmTestCase extends \PHPUnit_Framework_TestCase
32
{
33
    /**
34
     * @var EntityManager
35
     */
36
    protected $em;
37
38
    /**
39
     * EntityManager mock object together with
40
     * annotation mapping driver and pdo_sqlite
41
     * database in memory.
42
     *
43
     * @param EventManager  $evm
44
     * @param Configuration $config
45
     *
46
     * @return EntityManager
47
     */
48
    protected function getMockSqliteEntityManager(EventManager $evm = null, Configuration $config = null)
49
    {
50
        $conn = array(
51
            'driver' => 'pdo_sqlite',
52
            'memory' => true,
53
        );
54
55
        $em = EntityManager::create($conn, $config ?: $this->getMockAnnotatedConfig(),
56
            $evm ?: $this->getEventManager());
57
58
        $schema = array_map(function ($class) use ($em) {
59
            return $em->getClassMetadata($class);
60
        }, (array) $this->getUsedEntityFixtures());
61
62
        $schemaTool = new SchemaTool($em);
63
        $schemaTool->dropSchema(array());
64
        $schemaTool->createSchema($schema);
65
66
        return $this->em = $em;
67
    }
68
69
    /**
70
     * Creates default mapping driver.
71
     *
72
     * @return AnnotationDriver
73
     */
74
    protected function getMetadataDriverImplementation()
75
    {
76
        return new AnnotationDriver(new AnnotationReader());
77
    }
78
79
    /**
80
     * Get a list of used fixture classes.
81
     *
82
     * @return array
83
     */
84
    abstract protected function getUsedEntityFixtures();
85
86
    /**
87
     * Get annotation mapping configuration.
88
     *
89
     * @return Configuration
90
     */
91
    protected function getMockAnnotatedConfig()
92
    {
93
        $config = new Configuration();
94
        $config->setProxyDir(sys_get_temp_dir().'/sonata-translation-bundle');
95
        $config->setProxyNamespace('Proxy');
96
        $config->setMetadataDriverImpl($this->getMetadataDriverImplementation());
97
98
        return $config;
99
    }
100
101
    /**
102
     * Build event manager.
103
     *
104
     * @return EventManager
105
     */
106
    private function getEventManager()
107
    {
108
        $evm = new EventManager();
109
110
        return $evm;
111
    }
112
}
113