Failed Conditions
Push — master ( ac0e13...24dbc4 )
by Sergei
22s queued 15s
created

testHostnameIsRequiredForPersistentConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Tests\Driver\Mysqli;
6
7
use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionError;
8
use Doctrine\DBAL\Driver\Mysqli\HostRequired;
9
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
10
use Doctrine\DBAL\Platforms\MySqlPlatform;
11
use Doctrine\DBAL\Tests\FunctionalTestCase;
12
use function extension_loaded;
13
use function restore_error_handler;
14
use function set_error_handler;
15
16
class MysqliConnectionTest extends FunctionalTestCase
17
{
18
    protected function setUp() : void
19
    {
20
        if (! extension_loaded('mysqli')) {
21
            self::markTestSkipped('mysqli is not installed.');
22
        }
23
24
        parent::setUp();
25
26
        if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
27
            return;
28
        }
29
30
        self::markTestSkipped('MySQL only test.');
31
    }
32
33
    public function testRestoresErrorHandlerOnException() : void
34
    {
35
        $handler = static function () : bool {
36
            self::fail('Never expected this to be called');
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

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:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
37
        };
38
        set_error_handler($handler);
39
40
        try {
41
            new MysqliConnection(['host' => '255.255.255.255'], 'user', 'pass');
42
            self::fail('An exception was supposed to be raised');
43
        } catch (ConnectionError $e) {
44
            // Do nothing
45
        }
46
47
        self::assertSame($handler, set_error_handler($handler), 'Restoring error handler failed.');
48
        restore_error_handler();
49
        restore_error_handler();
50
    }
51
52
    public function testHostnameIsRequiredForPersistentConnection() : void
53
    {
54
        $this->expectException(HostRequired::class);
55
        new MysqliConnection(['persistent' => 'true'], '', '');
56
    }
57
}
58