ModuleIntegrationTest::testCanLoadModule()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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