LoggingTest.php 2.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace Doctrine\Tests\DBAL\Connection;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Logging\SQLLogger;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use PHPUnit\Framework\TestCase;

final class LoggingTest extends TestCase
{
15
    public function testLogExecuteQuery(): void
16 17 18 19 20 21 22
    {
        $driverConnection = $this->createStub(DriverConnection::class);

        $this->createConnection($driverConnection, 'SELECT * FROM table')
            ->executeQuery('SELECT * FROM table');
    }

23
    public function testLogExecuteStatement(): void
24 25 26 27 28
    {
        $this->createConnection(
            $this->createStub(DriverConnection::class),
            'UPDATE table SET foo = ?'
        )
29
            ->executeStatement('UPDATE table SET foo = ?');
30 31
    }

32
    public function testLogPrepareExecute(): void
33 34 35 36 37 38 39 40 41 42
    {
        $driverConnection = $this->createStub(DriverConnection::class);
        $driverConnection->method('prepare')
            ->willReturn($this->createStub(Statement::class));

        $this->createConnection($driverConnection, 'UPDATE table SET foo = ?')
            ->prepare('UPDATE table SET foo = ?')
            ->execute();
    }

43
    private function createConnection(DriverConnection $driverConnection, string $expectedSQL): Connection
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    {
        $driver = $this->createStub(Driver::class);
        $driver->method('connect')
            ->willReturn($driverConnection);
        $driver->method('getDatabasePlatform')
            ->willReturn($this->createMock(AbstractPlatform::class));

        $logger = $this->createMock(SQLLogger::class);
        $logger->expects($this->once())
            ->method('startQuery')
            ->with($this->equalTo($expectedSQL), $this->equalTo([]));
        $logger->expects($this->at(1))
            ->method('stopQuery');

        $connection = new Connection([], $driver);
        $connection->getConfiguration()->setSQLLogger($logger);

        return $connection;
    }
}