Completed
Push — master ( f33fb0...dd4c03 )
by
unknown
02:17
created

DoctrineOrmTestCase::getMockSqliteEntityManager()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 19
rs 9.4285
cc 3
eloc 12
nc 1
nop 2
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\Test;
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
    final 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(), $evm ?: new EventManager());
56
57
        $schema = array_map(function ($class) use ($em) {
58
            return $em->getClassMetadata($class);
59
        }, (array) $this->getUsedEntityFixtures());
60
61
        $schemaTool = new SchemaTool($em);
62
        $schemaTool->dropSchema(array());
63
        $schemaTool->createSchema($schema);
64
65
        return $this->em = $em;
66
    }
67
68
    /**
69
     * Creates default mapping driver.
70
     *
71
     * @return AnnotationDriver
72
     */
73
    final protected function getMetadataDriverImplementation()
74
    {
75
        return new AnnotationDriver(new AnnotationReader());
76
    }
77
78
    /**
79
     * Get a list of used fixture classes.
80
     *
81
     * @return array
82
     */
83
    abstract protected function getUsedEntityFixtures();
84
85
    /**
86
     * Get annotation mapping configuration.
87
     *
88
     * @return Configuration
89
     */
90
    final protected function getMockAnnotatedConfig()
91
    {
92
        $config = new Configuration();
93
        $config->setProxyDir(sys_get_temp_dir().'/sonata-translation-bundle');
94
        $config->setProxyNamespace('Proxy');
95
        $config->setMetadataDriverImpl($this->getMetadataDriverImplementation());
96
97
        return $config;
98
    }
99
}
100