Completed
Pull Request — master (#3807)
by Sergei
61:21
created

testRestoresErrorHandlerOnException()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
rs 9.9332
c 0
b 0
f 0
cc 2
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Driver\Mysqli;
6
7
use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionError;
8
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
9
use Doctrine\DBAL\Platforms\MySqlPlatform;
10
use Doctrine\Tests\DbalFunctionalTestCase;
11
use PHPUnit\Framework\MockObject\MockObject;
12
use function extension_loaded;
13
use function restore_error_handler;
14
use function set_error_handler;
15
16
class MysqliConnectionTest extends DbalFunctionalTestCase
17
{
18
    /**
19
     * The mysqli driver connection mock under test.
20
     *
21
     * @var MysqliConnection|MockObject
22
     */
23
    private $connectionMock;
24
25
    protected function setUp() : void
26
    {
27
        if (! extension_loaded('mysqli')) {
28
            $this->markTestSkipped('mysqli is not installed.');
29
        }
30
31
        parent::setUp();
32
33
        if (! $this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
34
            $this->markTestSkipped('MySQL only test.');
35
        }
36
37
        $this->connectionMock = $this->getMockBuilder(MysqliConnection::class)
38
            ->disableOriginalConstructor()
39
            ->getMockForAbstractClass();
40
    }
41
42
    public function testRestoresErrorHandlerOnException() : void
43
    {
44
        $handler         = static function () : bool {
45
            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...
46
        };
47
        $default_handler = set_error_handler($handler);
0 ignored issues
show
Unused Code introduced by
The assignment to $default_handler is dead and can be removed.
Loading history...
48
49
        try {
50
            new MysqliConnection(['host' => '255.255.255.255'], 'user', 'pass');
51
            self::fail('An exception was supposed to be raised');
52
        } catch (ConnectionError $e) {
53
            // Do nothing
54
        }
55
56
        self::assertSame($handler, set_error_handler($handler), 'Restoring error handler failed.');
57
        restore_error_handler();
58
        restore_error_handler();
59
    }
60
}
61