1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
<?php
namespace Doctrine\DBAL\Tests;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Exception as DriverException;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Logging\SQLLogger;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Statement;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class StatementTest extends TestCase
{
/** @var Connection&MockObject */
private Connection $conn;
/** @var Configuration&MockObject */
private Configuration $configuration;
/** @var DriverStatement&MockObject */
private DriverStatement $driverStatement;
protected function setUp(): void
{
$this->driverStatement = $this->createMock(DriverStatement::class);
$driver = $this->createMock(Driver::class);
$this->conn = $this->getMockBuilder(Connection::class)
->setConstructorArgs([[], $driver])
->getMock();
$this->configuration = $this->createMock(Configuration::class);
$this->conn->expects(self::any())
->method('getConfiguration')
->willReturn($this->configuration);
$this->conn->expects(self::any())
->method('getDriver')
->willReturn($driver);
}
public function testExecuteCallsLoggerStartQueryWithParametersWhenValuesBound(): void
{
$name = 'foo';
$var = 'bar';
$type = ParameterType::STRING;
$values = [$name => $var];
$types = [$name => $type];
$sql = '';
$logger = $this->createMock(SQLLogger::class);
$logger->expects(self::once())
->method('startQuery')
->with(self::equalTo($sql), self::equalTo($values), self::equalTo($types));
$this->configuration->expects(self::once())
->method('getSQLLogger')
->willReturn($logger);
$statement = new Statement($this->conn, $this->driverStatement, $sql);
$statement->bindValue($name, $var, $type);
$statement->execute();
}
public function testExecuteCallsLoggerStartQueryWithParametersWhenParamsPassedToExecute(): void
{
$name = 'foo';
$var = 'bar';
$values = [$name => $var];
$types = [];
$sql = '';
$logger = $this->createMock(SQLLogger::class);
$logger->expects(self::once())
->method('startQuery')
->with(self::equalTo($sql), self::equalTo($values), self::equalTo($types));
$this->configuration->expects(self::once())
->method('getSQLLogger')
->willReturn($logger);
$statement = new Statement($this->conn, $this->driverStatement, $sql);
$statement->execute($values);
}
public function testExecuteCallsStartQueryWithTheParametersBoundViaBindParam(): void
{
$name = 'foo';
$var = 'bar';
$values = [$name => $var];
$types = [$name => ParameterType::STRING];
$sql = '';
$logger = $this->createMock(SQLLogger::class);
$logger->expects(self::once())
->method('startQuery')
->with(self::equalTo($sql), self::equalTo($values), self::equalTo($types));
$this->configuration->expects(self::once())
->method('getSQLLogger')
->willReturn($logger);
$statement = new Statement($this->conn, $this->driverStatement, $sql);
$statement->bindParam($name, $var);
$statement->execute();
}
public function testExecuteCallsLoggerStopQueryOnException(): void
{
$logger = $this->createMock(SQLLogger::class);
$this->configuration->expects(self::once())
->method('getSQLLogger')
->willReturn($logger);
$logger->expects(self::once())
->method('startQuery');
$logger->expects(self::once())
->method('stopQuery');
$this->driverStatement->expects(self::once())
->method('execute')
->will(self::throwException(
$this->createMock(DriverException::class),
));
$statement = new Statement($this->conn, $this->driverStatement, '');
$this->expectException(Exception::class);
$statement->execute();
}
}
|