1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Tests\Driver\Mysqli; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Driver\Mysqli\HostRequired; |
6
|
|
|
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection; |
7
|
|
|
use Doctrine\DBAL\Driver\Mysqli\MysqliException; |
8
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
9
|
|
|
use Doctrine\DBAL\Tests\FunctionalTestCase; |
10
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
11
|
|
|
use function extension_loaded; |
12
|
|
|
use function restore_error_handler; |
13
|
|
|
use function set_error_handler; |
14
|
|
|
|
15
|
|
|
class MysqliConnectionTest extends FunctionalTestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The mysqli driver connection mock under test. |
19
|
|
|
* |
20
|
|
|
* @var MysqliConnection|MockObject |
21
|
|
|
*/ |
22
|
|
|
private $connectionMock; |
23
|
|
|
|
24
|
|
|
protected function setUp() : void |
25
|
|
|
{ |
26
|
|
|
if (! extension_loaded('mysqli')) { |
27
|
|
|
self::markTestSkipped('mysqli is not installed.'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
parent::setUp(); |
31
|
|
|
|
32
|
|
|
if (! $this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
33
|
|
|
$this->markTestSkipped('MySQL only test.'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->connectionMock = $this->getMockBuilder(MysqliConnection::class) |
37
|
|
|
->disableOriginalConstructor() |
38
|
|
|
->getMockForAbstractClass(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testDoesNotRequireQueryForServerVersion() : void |
42
|
|
|
{ |
43
|
|
|
self::assertFalse($this->connectionMock->requiresQueryForServerVersion()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testRestoresErrorHandlerOnException() : void |
47
|
|
|
{ |
48
|
|
|
$handler = static function () : bool { |
49
|
|
|
self::fail('Never expected this to be called'); |
|
|
|
|
50
|
|
|
}; |
51
|
|
|
$default_handler = set_error_handler($handler); |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
new MysqliConnection(['host' => '255.255.255.255'], 'user', 'pass'); |
55
|
|
|
self::fail('An exception was supposed to be raised'); |
56
|
|
|
} catch (MysqliException $e) { |
57
|
|
|
self::assertSame('Network is unreachable', $e->getMessage()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
self::assertSame($handler, set_error_handler($default_handler), 'Restoring error handler failed.'); |
61
|
|
|
restore_error_handler(); |
62
|
|
|
restore_error_handler(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testHostnameIsRequiredForPersistentConnection() : void |
66
|
|
|
{ |
67
|
|
|
$this->expectException(HostRequired::class); |
68
|
|
|
new MysqliConnection(['persistent' => 'true'], '', ''); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: