Completed
Pull Request — 3.0.x (#3943)
by Sergei
62:37
created

testRestoresErrorHandlerOnException()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 3
nop 0
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
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');
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...
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