Completed
Pull Request — master (#534)
by Thomas Mauro
09:42
created

testGettingPlatformFromContainer()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
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\Common\Persistence\Mapping\Driver\MappingDriver;
23
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver;
24
use Doctrine\DBAL\Platforms\AbstractPlatform;
25
use Doctrine\ORM\Configuration;
26
use DoctrineORMModuleTest\Assets\Types\MoneyType;
27
use PHPUnit\Framework\TestCase;
28
use DoctrineORMModule\Service\DBALConnectionFactory;
29
use Doctrine\DBAL\Types\Type;
30
use Doctrine\Common\Cache\ArrayCache;
31
use Doctrine\Common\EventManager;
32
use Zend\ServiceManager\ServiceManager;
33
use DoctrineORMModule\Service\ConfigurationFactory;
34
35
/**
36
 * @covers \DoctrineORMModule\Service\DBALConnectionFactory
37
 */
38
class DBALConnectionFactoryTest extends TestCase
39
{
40
    /**
41
     * @var ServiceManager
42
     */
43
    protected $serviceManager;
44
    /**
45
     * @var DBALConnectionFactory
46
     */
47
    protected $factory;
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function setUp()
53
    {
54
        $this->serviceManager = new ServiceManager();
55
        $this->factory = new DBALConnectionFactory('orm_default');
56
        $this->serviceManager->setService('doctrine.cache.array', new ArrayCache());
57
        $this->serviceManager->setService('doctrine.eventmanager.orm_default', new EventManager());
58
    }
59
60
    public function testNoConnectWithoutCustomMappingsAndCommentedTypes()
61
    {
62
        $config = [
63
            'doctrine' => [
64
                'connection' => [
65
                    'orm_default' => [
66
                        'driverClass'   => PDOSqliteDriver::class,
67
                        'params' => [
68
                            'memory' => true,
69
                        ],
70
                    ]
71
                ],
72
            ],
73
        ];
74
        $configurationMock = $this->getMockBuilder(Configuration::class)
75
            ->disableOriginalConstructor()
76
            ->getMock();
77
78
        $this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock);
79
        $this->serviceManager->setService('config', $config);
80
        $this->serviceManager->setService('Configuration', $config);
81
82
        $dbal = $this->factory->createService($this->serviceManager);
83
        $this->assertFalse($dbal->isConnected());
84
    }
85
86
    public function testDoctrineMappingTypeReturnCorrectParent()
87
    {
88
        $config = [
89
            'doctrine' => [
90
                'connection' => [
91
                    'orm_default' => [
92
                        'driverClass'   => PDOSqliteDriver::class,
93
                        'params' => [
94
                            'memory' => true,
95
                        ],
96
                        'doctrineTypeMappings' => [
97
                            'money' => 'string',
98
                        ],
99
                    ],
100
                ],
101
            ],
102
        ];
103
        $configurationMock = $this->getMockBuilder(Configuration::class)
104
            ->disableOriginalConstructor()
105
            ->getMock();
106
107
        $this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock);
108
        $this->serviceManager->setService('config', $config);
109
        $this->serviceManager->setService('Configuration', $config);
110
111
        $dbal = $this->factory->createService($this->serviceManager);
112
        $platform = $dbal->getDatabasePlatform();
113
        $this->assertSame('string', $platform->getDoctrineTypeMapping("money"));
114
    }
115
116
    public function testDoctrineAddCustomCommentedType()
117
    {
118
        $config = [
119
            'doctrine' => [
120
                'connection' => [
121
                    'orm_default' => [
122
                        'driverClass'   => PDOSqliteDriver::class,
123
                        'params' => [
124
                            'memory' => true,
125
                        ],
126
                        'doctrineTypeMappings' => [
127
                            'money' => 'money',
128
                        ],
129
                        'doctrineCommentedTypes' => [
130
                            'money',
131
                        ],
132
                    ],
133
                ],
134
                'configuration' => [
135
                    'orm_default' => [
136
                        'types' => [
137
                            'money' => MoneyType::class,
138
                        ],
139
                    ],
140
                ],
141
            ],
142
        ];
143
        $this->serviceManager->setService('config', $config);
144
        $this->serviceManager->setService('Configuration', $config);
145
        $this->serviceManager->setService(
146
            'doctrine.driver.orm_default',
147
            $this->createMock(MappingDriver::class)
148
        );
149
        $configurationFactory = new ConfigurationFactory('orm_default');
150
        $this->serviceManager->setService(
151
            'doctrine.configuration.orm_default',
152
            $configurationFactory->createService($this->serviceManager)
153
        );
154
        $dbal = $this->factory->createService($this->serviceManager);
155
        $platform = $dbal->getDatabasePlatform();
156
        $type = Type::getType($platform->getDoctrineTypeMapping("money"));
157
158
        $this->assertInstanceOf(MoneyType::class, $type);
159
        $this->assertTrue($platform->isCommentedDoctrineType($type));
160
    }
161
162
    public function testGettingPlatformFromContainer()
163
    {
164
        $config = [
165
            'doctrine' => [
166
                'connection' => [
167
                    'orm_default' => [
168
                        'driverClass'   => PDOSqliteDriver::class,
169
                        'params' => [
170
                            'platform' => 'platform_service',
171
                        ],
172
                    ]
173
                ],
174
            ],
175
        ];
176
        $configurationMock = $this->getMockBuilder(Configuration::class)
177
            ->disableOriginalConstructor()
178
            ->getMock();
179
180
        $platformMock = $this->getMockBuilder(AbstractPlatform::class)
181
            ->disableOriginalConstructor()
182
            ->getMock();
183
184
        $this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock);
185
        $this->serviceManager->setService('config', $config);
186
        $this->serviceManager->setService('Configuration', $config);
187
        $this->serviceManager->setService('platform_service', $platformMock);
188
189
        $dbal = $this->factory->createService($this->serviceManager);
190
        $platform = $dbal->getDatabasePlatform();
191
        $this->assertSame($platformMock, $platform);
192
    }
193
}
194