BaseTestCaseOrm::getDoctrine()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 34
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
3
namespace Khepin\Utils;
4
5
use Doctrine\ORM\EntityManager;
6
use \Mockery as m;
7
use Doctrine\ORM\Tools\SchemaTool;
8
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
9
use Doctrine\ORM\Mapping\DefaultQuoteStrategy;
10
use Doctrine\ORM\Repository\DefaultRepositoryFactory;
11
12
class BaseTestCaseOrm extends \PHPUnit_Framework_TestCase
13
{
14
    protected $doctrine;
15
16
    private function getMockAnnotatedConfig()
17
    {
18
        $config = $this->createMock('Doctrine\ORM\Configuration');
19
        $config
20
                ->expects($this->once())
21
                ->method('getProxyDir')
22
                ->will($this->returnValue(__DIR__ . '/temp'))
23
        ;
24
25
        $config
26
                ->expects($this->once())
27
                ->method('getProxyNamespace')
28
                ->will($this->returnValue('Proxy'))
29
        ;
30
31
        $config
32
                ->expects($this->once())
33
                ->method('getAutoGenerateProxyClasses')
34
                ->will($this->returnValue(true))
35
        ;
36
37
        $config
38
                ->expects($this->once())
39
                ->method('getClassMetadataFactoryName')
40
                ->will($this->returnValue('Doctrine\\ORM\\Mapping\\ClassMetadataFactory'))
41
        ;
42
43
        $mappingDriver = $this->getMetadataDriverImplementation();
44
45
        $config
46
                ->expects($this->any())
47
                ->method('getMetadataDriverImpl')
48
                ->will($this->returnValue($mappingDriver))
49
        ;
50
51
        $config
52
                ->expects($this->any())
53
                ->method('getDefaultRepositoryClassName')
54
                ->will($this->returnValue('Doctrine\\ORM\\EntityRepository'))
55
        ;
56
57
        $quoteStrategy = new DefaultQuoteStrategy();
58
59
        $config
60
            ->expects($this->any())
61
            ->method('getQuoteStrategy')
62
            ->will($this->returnValue($quoteStrategy))
63
        ;
64
65
        $repositoryFactory = new DefaultRepositoryFactory();
66
67
        $config
68
            ->expects($this->any())
69
            ->method('getRepositoryFactory')
70
            ->will($this->returnValue($repositoryFactory))
71
        ;
72
73
        return $config;
74
    }
75
76
    /**
77
     * Creates default mapping driver
78
     *
79
     * @return \Doctrine\ORM\Mapping\Driver\Driver
80
     */
81
    protected function getMetadataDriverImplementation()
0 ignored issues
show
Coding Style introduced by
getMetadataDriverImplementation uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
82
    {
83
        return new AnnotationDriver(
84
            $_ENV['annotation_reader'],
85
            array(__DIR__.'/../Fixture/Entity')
86
        );
87
    }
88
89
    /**
90
     * EntityManager mock object together with
91
     * annotation mapping driver and pdo_sqlite
92
     * database in memory
93
     *
94
     * @param  EventManager  $evm
0 ignored issues
show
Bug introduced by
There is no parameter named $evm. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
95
     * @return EntityManager
96
     */
97
    protected function getDoctrine()
98
    {
99
        $conn = array(
100
            'driver' => 'pdo_sqlite',
101
            'memory' => true,
102
            //'path' => __DIR__.'/../db.sqlite',
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
        );
104
105
        $config = $this->getMockAnnotatedConfig();
106
        $em = EntityManager::create($conn, $config);
107
108
        $entities = array(
109
            'Khepin\\Fixture\\Entity\\Car',
110
            'Khepin\\Fixture\\Entity\\Driver',
111
            'Khepin\\Fixture\\Entity\\Owner'
112
        );
113
114
        $schema = array_map(function ($class) use ($em) {
115
            return $em->getClassMetadata($class);
116
        }, $entities);
117
118
        $schemaTool = new SchemaTool($em);
119
        $schemaTool->dropSchema(array());
120
        $schemaTool->createSchema($schema);
121
122
        return $this->doctrine = m::mock(
123
            array(
124
                'getEntityManager'      => $em,
125
                'getManager'            => $em,
126
                'getManagers'           => array($em),
127
                'getManagerForClass'    => $em
128
            )
129
        );
130
    }
131
}
132