Completed
Push — 2.11.x ( c77492...50927b )
by Grégoire
23s queued 17s
created

LoggingTest::createConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 19
rs 9.7998
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Connection;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Driver;
7
use Doctrine\DBAL\Driver\Connection as DriverConnection;
8
use Doctrine\DBAL\Driver\Statement;
9
use Doctrine\DBAL\Logging\SQLLogger;
10
use Doctrine\DBAL\Platforms\AbstractPlatform;
11
use PHPUnit\Framework\TestCase;
12
13
final class LoggingTest extends TestCase
14
{
15
    public function testLogExecuteQuery() : void
16
    {
17
        $driverConnection = $this->createStub(DriverConnection::class);
18
        $driverConnection->method('query')
19
            ->willReturn($this->createStub(Statement::class));
20
21
        $this->createConnection($driverConnection, 'SELECT * FROM table')
22
            ->executeQuery('SELECT * FROM table');
23
    }
24
25
    public function testLogExecuteUpdate() : void
26
    {
27
        $this->createConnection(
28
            $this->createStub(DriverConnection::class),
29
            'UPDATE table SET foo = ?'
30
        )
31
            ->executeUpdate('UPDATE table SET foo = ?');
32
    }
33
34
    public function testLogPrepareExecute() : void
35
    {
36
        $driverConnection = $this->createStub(DriverConnection::class);
37
        $driverConnection->method('prepare')
38
            ->willReturn($this->createStub(Statement::class));
39
40
        $this->createConnection($driverConnection, 'UPDATE table SET foo = ?')
41
            ->prepare('UPDATE table SET foo = ?')
42
            ->execute();
43
    }
44
45
    private function createConnection(DriverConnection $driverConnection, string $expectedSQL) : Connection
46
    {
47
        $driver = $this->createStub(Driver::class);
48
        $driver->method('connect')
49
            ->willReturn($driverConnection);
50
        $driver->method('getDatabasePlatform')
51
            ->willReturn($this->createMock(AbstractPlatform::class));
52
53
        $logger = $this->createMock(SQLLogger::class);
54
        $logger->expects($this->once())
55
            ->method('startQuery')
56
            ->with($this->equalTo($expectedSQL), $this->equalTo([]));
57
        $logger->expects($this->at(1))
58
            ->method('stopQuery');
59
60
        $connection = new Connection([], $driver);
61
        $connection->getConfiguration()->setSQLLogger($logger);
62
63
        return $connection;
64
    }
65
}
66