Completed
Pull Request — master (#534)
by Thomas Mauro
08:02
created

testGettingPlatformFromContainer()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 1
eloc 21
nc 1
nop 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModuleTest\Service;
21
22
use Doctrine\DBAL\Platforms\AbstractPlatform;
23
use PHPUnit\Framework\TestCase;
24
use DoctrineORMModuleTest\Assets\Types\MoneyType;
25
use DoctrineORMModule\Service\DBALConnectionFactory;
26
use Doctrine\DBAL\Types\Type;
27
use Doctrine\Common\Cache\ArrayCache;
28
use Doctrine\Common\EventManager;
29
use Zend\ServiceManager\ServiceManager;
30
use DoctrineORMModule\Service\ConfigurationFactory;
31
32
/**
33
 * @covers \DoctrineORMModule\Service\DBALConnectionFactory
34
 */
35
class DBALConnectionFactoryTest extends TestCase
36
{
37
    /**
38
     * @var ServiceManager
39
     */
40
    protected $serviceManager;
41
    /**
42
     * @var DBALConnectionFactory
43
     */
44
    protected $factory;
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function setUp()
50
    {
51
        $this->serviceManager = new ServiceManager();
52
        $this->factory = new DBALConnectionFactory('orm_default');
53
        $this->serviceManager->setService('doctrine.cache.array', new ArrayCache());
54
        $this->serviceManager->setService('doctrine.eventmanager.orm_default', new EventManager());
55
    }
56
57
    public function testNoConnectWithoutCustomMappingsAndCommentedTypes()
58
    {
59
        $config = [
60
            'doctrine' => [
61
                'connection' => [
62
                    'orm_default' => [
63
                        'driverClass'   => \Doctrine\DBAL\Driver\PDOSqlite\Driver::class,
64
                        'params' => [
65
                            'memory' => true,
66
                        ],
67
                    ]
68
                ],
69
            ],
70
        ];
71
        $configurationMock = $this->getMockBuilder(\Doctrine\ORM\Configuration::class)
72
            ->disableOriginalConstructor()
73
            ->getMock();
74
75
        $this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock);
76
        $this->serviceManager->setService('config', $config);
77
        $this->serviceManager->setService('Configuration', $config);
78
79
        $dbal = $this->factory->createService($this->serviceManager);
80
        $this->assertFalse($dbal->isConnected());
81
    }
82
83
    public function testDoctrineMappingTypeReturnCorrectParent()
84
    {
85
        $config = [
86
            'doctrine' => [
87
                'connection' => [
88
                    'orm_default' => [
89
                        'driverClass'   => \Doctrine\DBAL\Driver\PDOSqlite\Driver::class,
90
                        'params' => [
91
                            'memory' => true,
92
                        ],
93
                        'doctrineTypeMappings' => [
94
                            'money' => 'string',
95
                        ],
96
                    ],
97
                ],
98
            ],
99
        ];
100
        $configurationMock = $this->getMockBuilder(\Doctrine\ORM\Configuration::class)
101
            ->disableOriginalConstructor()
102
            ->getMock();
103
104
        $this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock);
105
        $this->serviceManager->setService('config', $config);
106
        $this->serviceManager->setService('Configuration', $config);
107
108
        $dbal = $this->factory->createService($this->serviceManager);
109
        $platform = $dbal->getDatabasePlatform();
110
        $this->assertSame('string', $platform->getDoctrineTypeMapping("money"));
111
    }
112
113
    public function testDoctrineAddCustomCommentedType()
114
    {
115
        $config = [
116
            'doctrine' => [
117
                'connection' => [
118
                    'orm_default' => [
119
                        'driverClass'   => \Doctrine\DBAL\Driver\PDOSqlite\Driver::class,
120
                        'params' => [
121
                            'memory' => true,
122
                        ],
123
                        'doctrineTypeMappings' => [
124
                            'money' => 'money',
125
                        ],
126
                        'doctrineCommentedTypes' => [
127
                            'money',
128
                        ],
129
                    ],
130
                ],
131
                'configuration' => [
132
                    'orm_default' => [
133
                        'types' => [
134
                            'money' => \DoctrineORMModuleTest\Assets\Types\MoneyType::class,
135
                        ],
136
                    ],
137
                ],
138
            ],
139
        ];
140
        $this->serviceManager->setService('config', $config);
141
        $this->serviceManager->setService('Configuration', $config);
142
        $this->serviceManager->setService(
143
            'doctrine.driver.orm_default',
144
            $this->createMock(\Doctrine\Common\Persistence\Mapping\Driver\MappingDriver::class)
145
        );
146
        $configurationFactory = new ConfigurationFactory('orm_default');
147
        $this->serviceManager->setService(
148
            'doctrine.configuration.orm_default',
149
            $configurationFactory->createService($this->serviceManager)
150
        );
151
        $dbal = $this->factory->createService($this->serviceManager);
152
        $platform = $dbal->getDatabasePlatform();
153
        $type = Type::getType($platform->getDoctrineTypeMapping("money"));
154
155
        $this->assertInstanceOf(\DoctrineORMModuleTest\Assets\Types\MoneyType::class, $type);
156
        $this->assertTrue($platform->isCommentedDoctrineType($type));
157
    }
158
159
    public function testGettingPlatformFromContainer()
160
    {
161
        $config = [
162
            'doctrine' => [
163
                'connection' => [
164
                    'orm_default' => [
165
                        'driverClass'   => \Doctrine\DBAL\Driver\PDOSqlite\Driver::class,
166
                        'params' => [
167
                            'platform' => 'platform_service',
168
                        ],
169
                    ]
170
                ],
171
            ],
172
        ];
173
        $configurationMock = $this->getMockBuilder(\Doctrine\ORM\Configuration::class)
174
            ->disableOriginalConstructor()
175
            ->getMock();
176
177
        $platformMock = $this->getMockBuilder(AbstractPlatform::class)
178
            ->disableOriginalConstructor()
179
            ->getMock();
180
181
        $this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock);
182
        $this->serviceManager->setService('config', $config);
183
        $this->serviceManager->setService('Configuration', $config);
184
        $this->serviceManager->setService('platform_service', $platformMock);
185
186
        $dbal = $this->factory->createService($this->serviceManager);
187
        $platform = $dbal->getDatabasePlatform();
188
        $this->assertSame($platformMock, $platform);
189
    }
190
}
191