ModuleIntegrationTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 2 Features 1
Metric Value
wmc 5
c 2
b 2
f 1
lcom 1
cbo 4
dl 0
loc 128
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testCanLoadModule() 0 7 1
B testDoctrineORMEntityManagerDefaultMappingsAreConfigured() 0 34 1
A testDataMapperManagerConfiguration() 0 48 1
A addModuleConfig() 0 14 1
1
<?php
2
/**
3
 * @license See the file LICENSE for copying permission.
4
 */
5
6
namespace Thorr\Persistence\Doctrine\Test\Integration;
7
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use PHPUnit_Framework_TestCase as TestCase;
10
use Thorr\Persistence\DataMapper\Manager\DataMapperManager;
11
use Thorr\Persistence\Doctrine;
12
use Thorr\Persistence\Doctrine\DataMapper\DoctrineAdapter;
13
use Zend\Mvc\Application;
14
use Zend\ServiceManager\ServiceManager;
15
use Zend\Stdlib\ArrayUtils;
16
17
class ModuleIntegrationTest extends TestCase
18
{
19
    /**
20
     * @var array
21
     */
22
    protected $appConfig;
23
24
    protected function setUp()
25
    {
26
        $this->serviceManager = new ServiceManager();
0 ignored issues
show
Bug introduced by
The property serviceManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
        $this->appConfig      = [
28
            'modules' => [
29
                'Thorr\Persistence\Doctrine',
30
            ],
31
            'module_listener_options' => [],
32
        ];
33
    }
34
35
    public function testCanLoadModule()
36
    {
37
        $app           = Application::init($this->appConfig);
38
        $loadedModules = $app->getServiceManager()->get('ModuleManager')->getLoadedModules();
39
        $this->assertArrayHasKey('Thorr\Persistence\Doctrine', $loadedModules);
40
        $this->assertInstanceOf(Doctrine\Module::class, $loadedModules['Thorr\Persistence\Doctrine']);
41
    }
42
43
    public function testDoctrineORMEntityManagerDefaultMappingsAreConfigured()
44
    {
45
        $this->appConfig['modules'][] = 'DoctrineModule';
46
        $this->appConfig['modules'][] = 'DoctrineORMModule';
47
48
        $this->addModuleConfig([
49
            'doctrine' => [
50
                'connection' => [
51
                    'orm_default' => [
52
                        'driverClass' => \Doctrine\DBAL\Driver\PDOSqlite\Driver::class,
53
                        'params'      => [
54
                            'memory' => true,
55
                        ],
56
                    ],
57
                ],
58
                'driver' => [
59
                    'test_driver' => [
60
                        'class' => 'Doctrine\ORM\Mapping\Driver\XmlDriver',
61
                        'paths' => __DIR__ . '/Asset',
62
                    ],
63
                    'orm_default' => [
64
                        'drivers' => [
65
                            Asset\Entity::class => 'test_driver',
66
                        ],
67
                    ],
68
                ],
69
            ],
70
        ]);
71
        $app            = Application::init($this->appConfig);
72
        $serviceManager = $app->getServiceManager();
73
        $entityManager  = $serviceManager->get('Doctrine\ORM\EntityManager');
74
75
        $this->assertInstanceOf(ClassMetadata::class, $entityManager->getClassMetadata(Asset\Entity::class));
76
    }
77
78
    public function testDataMapperManagerConfiguration()
79
    {
80
        array_unshift($this->appConfig['modules'], 'Thorr\Persistence');
81
        $this->appConfig['modules'][] = 'DoctrineModule';
82
        $this->appConfig['modules'][] = 'DoctrineORMModule';
83
84
        $this->addModuleConfig([
85
            'doctrine' => [
86
                'connection' => [
87
                    'orm_default' => [
88
                        'driverClass' => \Doctrine\DBAL\Driver\PDOSqlite\Driver::class,
89
                        'params'      => [
90
                            'memory' => true,
91
                        ],
92
                    ],
93
                ],
94
                'driver' => [
95
                    'test_driver' => [
96
                        'class' => 'Doctrine\ORM\Mapping\Driver\XmlDriver',
97
                        'paths' => __DIR__ . '/Asset',
98
                    ],
99
                    'orm_default' => [
100
                        'drivers' => [
101
                            Asset\Entity::class => 'test_driver',
102
                        ],
103
                    ],
104
                ],
105
            ],
106
            'thorr_persistence_dmm' => [
107
                'entity_data_mapper_map' => [
108
                    Asset\Entity::class => 'TestMapper',
109
                ],
110
                'doctrine' => [
111
                    'adapters' => [
112
                        'TestMapper' => DoctrineAdapter::class,
113
                    ],
114
                ],
115
            ],
116
        ]);
117
118
        $app               = Application::init($this->appConfig);
119
        $serviceManager    = $app->getServiceManager();
120
        $dataMapperManager = $serviceManager->get(DataMapperManager::class);
121
        $this->assertInstanceOf(
122
            DoctrineAdapter::class,
123
            $dataMapperManager->getDataMapperForEntity(Asset\Entity::class)
124
        );
125
    }
126
127
    /**
128
     * @param array $config
129
     */
130
    protected function addModuleConfig(array $config)
131
    {
132
        $this->appConfig['service_manager'] = [
133
            'services'   => [
134
                'config-delegator' => function ($sl, $name, $rName, $callback) use ($config) {
135
                    $oldConfig = $callback();
136
                    $newConfig = ArrayUtils::merge($oldConfig, $config);
137
138
                    return $newConfig;
139
                },
140
            ],
141
            'delegators' => [ 'config' => [ 'config-delegator' ] ],
142
        ];
143
    }
144
}
145