Completed
Push — master ( 569398...78d4be )
by Tom
25s queued 10s
created

testWithoutUseSavepoints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace DoctrineORMModuleTest\Service;
4
5
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
6
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver;
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\ORM\Configuration;
9
use DoctrineORMModuleTest\Assets\Types\MoneyType;
10
use PHPUnit\Framework\TestCase;
11
use DoctrineORMModule\Service\DBALConnectionFactory;
12
use Doctrine\DBAL\Types\Type;
13
use Doctrine\Common\Cache\ArrayCache;
14
use Doctrine\Common\EventManager;
15
use Laminas\ServiceManager\ServiceManager;
16
use DoctrineORMModule\Service\ConfigurationFactory;
17
18
/**
19
 * @covers \DoctrineORMModule\Service\DBALConnectionFactory
20
 */
21
class DBALConnectionFactoryTest extends TestCase
22
{
23
    /**
24
     * @var ServiceManager
25
     */
26
    protected $serviceManager;
27
    /**
28
     * @var DBALConnectionFactory
29
     */
30
    protected $factory;
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function setUp() : void
36
    {
37
        $this->serviceManager = new ServiceManager();
38
        $this->factory = new DBALConnectionFactory('orm_default');
39
        $this->serviceManager->setService('doctrine.cache.array', new ArrayCache());
40
        $this->serviceManager->setService('doctrine.eventmanager.orm_default', new EventManager());
41
    }
42
43
    public function testNoConnectWithoutCustomMappingsAndCommentedTypes()
44
    {
45
        $config = [
46
            'doctrine' => [
47
                'connection' => [
48
                    'orm_default' => [
49
                        'driverClass'   => PDOSqliteDriver::class,
50
                        'params' => [
51
                            'memory' => true,
52
                        ],
53
                    ]
54
                ],
55
            ],
56
        ];
57
        $configurationMock = $this->getMockBuilder(Configuration::class)
0 ignored issues
show
Bug introduced by
The method disableOriginalConstructor cannot be called on $this->getMockBuilder(\D...M\Configuration::class) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
58
            ->disableOriginalConstructor()
59
            ->getMock();
60
61
        $this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock);
62
        $this->serviceManager->setService('config', $config);
63
        $this->serviceManager->setService('Configuration', $config);
64
65
        $dbal = $this->factory->createService($this->serviceManager);
66
        $this->assertFalse($dbal->isConnected());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DoctrineORMModule...LConnectionFactoryTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
    }
68
69
    public function testDoctrineMappingTypeReturnCorrectParent()
70
    {
71
        $config = [
72
            'doctrine' => [
73
                'connection' => [
74
                    'orm_default' => [
75
                        'driverClass'   => PDOSqliteDriver::class,
76
                        'params' => [
77
                            'memory' => true,
78
                        ],
79
                        'doctrineTypeMappings' => [
80
                            'money' => 'string',
81
                        ],
82
                    ],
83
                ],
84
            ],
85
        ];
86
        $configurationMock = $this->getMockBuilder(Configuration::class)
0 ignored issues
show
Bug introduced by
The method disableOriginalConstructor cannot be called on $this->getMockBuilder(\D...M\Configuration::class) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
87
            ->disableOriginalConstructor()
88
            ->getMock();
89
90
        $this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock);
91
        $this->serviceManager->setService('config', $config);
92
        $this->serviceManager->setService('Configuration', $config);
93
94
        $dbal = $this->factory->createService($this->serviceManager);
95
        $platform = $dbal->getDatabasePlatform();
96
        $this->assertSame('string', $platform->getDoctrineTypeMapping("money"));
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DoctrineORMModule...LConnectionFactoryTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
    }
98
99
    public function testDoctrineAddCustomCommentedType()
100
    {
101
        $config = [
102
            'doctrine' => [
103
                'connection' => [
104
                    'orm_default' => [
105
                        'driverClass'   => PDOSqliteDriver::class,
106
                        'params' => [
107
                            'memory' => true,
108
                        ],
109
                        'doctrineTypeMappings' => [
110
                            'money' => 'money',
111
                        ],
112
                        'doctrineCommentedTypes' => [
113
                            'money',
114
                        ],
115
                    ],
116
                ],
117
                'configuration' => [
118
                    'orm_default' => [
119
                        'types' => [
120
                            'money' => MoneyType::class,
121
                        ],
122
                    ],
123
                ],
124
            ],
125
        ];
126
        $this->serviceManager->setService('config', $config);
127
        $this->serviceManager->setService('Configuration', $config);
128
        $this->serviceManager->setService(
129
            'doctrine.driver.orm_default',
130
            $this->createMock(MappingDriver::class)
0 ignored issues
show
Documentation introduced by
$this->createMock(\Doctr...r\MappingDriver::class) is of type null, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
131
        );
132
        $configurationFactory = new ConfigurationFactory('orm_default');
133
        $this->serviceManager->setService(
134
            'doctrine.configuration.orm_default',
135
            $configurationFactory->createService($this->serviceManager)
136
        );
137
        $dbal = $this->factory->createService($this->serviceManager);
138
        $platform = $dbal->getDatabasePlatform();
139
        $type = Type::getType($platform->getDoctrineTypeMapping("money"));
140
141
        $this->assertInstanceOf(MoneyType::class, $type);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DoctrineORMModule...LConnectionFactoryTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
        $this->assertTrue($platform->isCommentedDoctrineType($type));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DoctrineORMModule...LConnectionFactoryTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
143
    }
144
145
    public function testGettingPlatformFromContainer()
146
    {
147
        $config = [
148
            'doctrine' => [
149
                'connection' => [
150
                    'orm_default' => [
151
                        'driverClass'   => PDOSqliteDriver::class,
152
                        'params' => [
153
                            'platform' => 'platform_service',
154
                        ],
155
                    ]
156
                ],
157
            ],
158
        ];
159
        $configurationMock = $this->getMockBuilder(Configuration::class)
0 ignored issues
show
Bug introduced by
The method disableOriginalConstructor cannot be called on $this->getMockBuilder(\D...M\Configuration::class) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
160
            ->disableOriginalConstructor()
161
            ->getMock();
162
163
        $platformMock = $this->getMockBuilder(AbstractPlatform::class)
0 ignored issues
show
Bug introduced by
The method disableOriginalConstructor cannot be called on $this->getMockBuilder(\D...bstractPlatform::class) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
164
            ->disableOriginalConstructor()
165
            ->getMock();
166
167
        $this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock);
168
        $this->serviceManager->setService('config', $config);
169
        $this->serviceManager->setService('Configuration', $config);
170
        $this->serviceManager->setService('platform_service', $platformMock);
171
172
        $dbal = $this->factory->createService($this->serviceManager);
173
        $platform = $dbal->getDatabasePlatform();
174
        $this->assertSame($platformMock, $platform);
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DoctrineORMModule...LConnectionFactoryTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
175
    }
176
177
    public function testWithoutUseSavepoints()
178
    {
179
        $config = [
180
            'doctrine' => [
181
                'connection' => [
182
                    'orm_default' => [
183
                        'driverClass'   => PDOSqliteDriver::class,
184
                        'params' => [
185
                            'memory' => true,
186
                        ],
187
                    ]
188
                ],
189
            ],
190
        ];
191
        $configurationMock = $this->getMockBuilder(Configuration::class)
0 ignored issues
show
Bug introduced by
The method disableOriginalConstructor cannot be called on $this->getMockBuilder(\D...M\Configuration::class) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
192
            ->disableOriginalConstructor()
193
            ->getMock();
194
195
        $this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock);
196
        $this->serviceManager->setService('config', $config);
197
        $this->serviceManager->setService('Configuration', $config);
198
199
        $dbal = $this->factory->createService($this->serviceManager);
200
        $this->assertFalse($dbal->getNestTransactionsWithSavepoints());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DoctrineORMModule...LConnectionFactoryTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
201
    }
202
203
    public function testWithUseSavepoints()
204
    {
205
        $config = [
206
            'doctrine' => [
207
                'connection' => [
208
                    'orm_default' => [
209
                        'driverClass'   => PDOSqliteDriver::class,
210
                        'use_savepoints' => true,
211
                        'params' => [
212
                            'memory' => true,
213
                        ],
214
                    ]
215
                ],
216
            ],
217
        ];
218
        $configurationMock = $this->getMockBuilder(Configuration::class)
0 ignored issues
show
Bug introduced by
The method disableOriginalConstructor cannot be called on $this->getMockBuilder(\D...M\Configuration::class) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
219
            ->disableOriginalConstructor()
220
            ->getMock();
221
222
        $this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock);
223
        $this->serviceManager->setService('config', $config);
224
        $this->serviceManager->setService('Configuration', $config);
225
226
        $dbal = $this->factory->createService($this->serviceManager);
227
        $this->assertTrue($dbal->getNestTransactionsWithSavepoints());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DoctrineORMModule...LConnectionFactoryTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
228
    }
229
}
230