Unverified Commit 77ecc637 authored by Marco Pivetta's avatar Marco Pivetta Committed by GitHub

Merge pull request #3474 from morozov/remove-more-mocks

Remove more hard-coded mock classes
parents 2080599b afbae7ef
...@@ -22,7 +22,6 @@ use Doctrine\DBAL\Logging\EchoSQLLogger; ...@@ -22,7 +22,6 @@ use Doctrine\DBAL\Logging\EchoSQLLogger;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\Tests\DbalTestCase; use Doctrine\Tests\DbalTestCase;
use Doctrine\Tests\Mocks\DriverMock;
use Doctrine\Tests\Mocks\DriverStatementMock; use Doctrine\Tests\Mocks\DriverStatementMock;
use Doctrine\Tests\Mocks\ServerInfoAwareConnectionMock; use Doctrine\Tests\Mocks\ServerInfoAwareConnectionMock;
use Doctrine\Tests\Mocks\VersionAwarePlatformDriverMock; use Doctrine\Tests\Mocks\VersionAwarePlatformDriverMock;
...@@ -165,10 +164,22 @@ class ConnectionTest extends DbalTestCase ...@@ -165,10 +164,22 @@ class ConnectionTest extends DbalTestCase
public function testEventManagerPassedToPlatform() public function testEventManagerPassedToPlatform()
{ {
$driverMock = new DriverMock(); $eventManager = new EventManager();
$connection = new Connection($this->params, $driverMock);
self::assertInstanceOf(EventManager::class, $connection->getDatabasePlatform()->getEventManager()); /** @var AbstractPlatform|MockObject $driver */
self::assertSame($connection->getEventManager(), $connection->getDatabasePlatform()->getEventManager()); $platform = $this->createMock(AbstractPlatform::class);
$platform->expects($this->once())
->method('setEventManager')
->with($eventManager);
/** @var Driver|MockObject $driver */
$driver = $this->createMock(Driver::class);
$driver->expects($this->any())
->method('getDatabasePlatform')
->willReturn($platform);
$connection = new Connection($this->params, $driver, null, $eventManager);
$connection->getDatabasePlatform();
} }
/** /**
......
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
namespace Doctrine\Tests\DBAL; namespace Doctrine\Tests\DBAL;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Connections\MasterSlaveConnection; use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver as DrizzlePDOMySqlDriver; use Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver as DrizzlePDOMySqlDriver;
use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySQLDriver; use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySQLDriver;
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver; use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver;
...@@ -13,11 +15,10 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; ...@@ -13,11 +15,10 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Sharding\PoolingShardConnection; use Doctrine\DBAL\Sharding\PoolingShardConnection;
use Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser; use Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser;
use Doctrine\Tests\DbalTestCase; use Doctrine\Tests\DbalTestCase;
use Doctrine\Tests\Mocks\ConnectionMock;
use Doctrine\Tests\Mocks\DriverMock;
use PDO; use PDO;
use stdClass; use stdClass;
use function extension_loaded; use function extension_loaded;
use function get_class;
use function in_array; use function in_array;
use function is_array; use function is_array;
...@@ -92,7 +93,8 @@ class DriverManagerTest extends DbalTestCase ...@@ -92,7 +93,8 @@ class DriverManagerTest extends DbalTestCase
*/ */
public function testCustomWrapper() public function testCustomWrapper()
{ {
$wrapperClass = ConnectionMock::class; $wrapper = $this->createMock(Connection::class);
$wrapperClass = get_class($wrapper);
$options = [ $options = [
'pdo' => new PDO('sqlite::memory:'), 'pdo' => new PDO('sqlite::memory:'),
...@@ -238,6 +240,9 @@ class DriverManagerTest extends DbalTestCase ...@@ -238,6 +240,9 @@ class DriverManagerTest extends DbalTestCase
public function databaseUrls() public function databaseUrls()
{ {
$driver = $this->createMock(Driver::class);
$driverClass = get_class($driver);
return [ return [
'simple URL' => [ 'simple URL' => [
'mysql://foo:bar@localhost/baz', 'mysql://foo:bar@localhost/baz',
...@@ -406,14 +411,14 @@ class DriverManagerTest extends DbalTestCase ...@@ -406,14 +411,14 @@ class DriverManagerTest extends DbalTestCase
'URL without scheme but custom driver' => [ 'URL without scheme but custom driver' => [
[ [
'url' => '//foo:bar@localhost/baz', 'url' => '//foo:bar@localhost/baz',
'driverClass' => DriverMock::class, 'driverClass' => $driverClass,
], ],
[ [
'user' => 'foo', 'user' => 'foo',
'password' => 'bar', 'password' => 'bar',
'host' => 'localhost', 'host' => 'localhost',
'dbname' => 'baz', 'dbname' => 'baz',
'driverClass' => DriverMock::class, 'driverClass' => $driverClass,
], ],
], ],
'URL without scheme but default PDO driver and default driver' => [ 'URL without scheme but default PDO driver and default driver' => [
...@@ -434,14 +439,14 @@ class DriverManagerTest extends DbalTestCase ...@@ -434,14 +439,14 @@ class DriverManagerTest extends DbalTestCase
[ [
'url' => '//foo:bar@localhost/baz', 'url' => '//foo:bar@localhost/baz',
'driver' => 'pdo_mysql', 'driver' => 'pdo_mysql',
'driverClass' => DriverMock::class, 'driverClass' => $driverClass,
], ],
[ [
'user' => 'foo', 'user' => 'foo',
'password' => 'bar', 'password' => 'bar',
'host' => 'localhost', 'host' => 'localhost',
'dbname' => 'baz', 'dbname' => 'baz',
'driverClass' => DriverMock::class, 'driverClass' => $driverClass,
], ],
], ],
'URL with default PDO driver' => [ 'URL with default PDO driver' => [
...@@ -473,7 +478,7 @@ class DriverManagerTest extends DbalTestCase ...@@ -473,7 +478,7 @@ class DriverManagerTest extends DbalTestCase
'URL with default custom driver' => [ 'URL with default custom driver' => [
[ [
'url' => 'mysql://foo:bar@localhost/baz', 'url' => 'mysql://foo:bar@localhost/baz',
'driverClass' => DriverMock::class, 'driverClass' => $driverClass,
], ],
[ [
'user' => 'foo', 'user' => 'foo',
...@@ -501,7 +506,7 @@ class DriverManagerTest extends DbalTestCase ...@@ -501,7 +506,7 @@ class DriverManagerTest extends DbalTestCase
[ [
'url' => 'mysql://foo:bar@localhost/baz', 'url' => 'mysql://foo:bar@localhost/baz',
'driver' => 'sqlite', 'driver' => 'sqlite',
'driverClass' => DriverMock::class, 'driverClass' => $driverClass,
], ],
[ [
'user' => 'foo', 'user' => 'foo',
...@@ -516,7 +521,7 @@ class DriverManagerTest extends DbalTestCase ...@@ -516,7 +521,7 @@ class DriverManagerTest extends DbalTestCase
'url' => 'mysql://foo:bar@localhost/baz', 'url' => 'mysql://foo:bar@localhost/baz',
'pdo' => true, 'pdo' => true,
'driver' => 'sqlite', 'driver' => 'sqlite',
'driverClass' => DriverMock::class, 'driverClass' => $driverClass,
], ],
[ [
'user' => 'foo', 'user' => 'foo',
......
<?php
namespace Doctrine\Tests\Mocks;
use Doctrine\DBAL\Connection;
use function is_string;
class ConnectionMock extends Connection
{
/** @var DatabasePlatformMock */
private $platformMock;
/** @var int */
private $lastInsertId = 0;
/** @var string[][] */
private $inserts = [];
/**
* {@inheritDoc}
*/
public function __construct(array $params, $driver, $config = null, $eventManager = null)
{
$this->platformMock = new DatabasePlatformMock();
parent::__construct($params, $driver, $config, $eventManager);
}
public function getDatabasePlatform()
{
return $this->platformMock;
}
/**
* {@inheritDoc}
*/
public function insert($tableName, array $data, array $types = [])
{
$this->inserts[$tableName][] = $data;
}
public function lastInsertId($seqName = null)
{
return $this->lastInsertId;
}
public function quote($input, $type = null)
{
if (is_string($input)) {
return "'" . $input . "'";
}
return $input;
}
public function setLastInsertId($id)
{
$this->lastInsertId = $id;
}
public function getInserts()
{
return $this->inserts;
}
public function reset()
{
$this->inserts = [];
$this->lastInsertId = 0;
}
}
<?php
namespace Doctrine\Tests\Mocks;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class DatabasePlatformMock extends AbstractPlatform
{
/** @var string */
private $sequenceNextValSql = '';
/** @var bool */
private $prefersIdentityColumns = true;
/** @var bool */
private $prefersSequences = false;
public function prefersIdentityColumns()
{
return $this->prefersIdentityColumns;
}
public function prefersSequences()
{
return $this->prefersSequences;
}
public function getSequenceNextValSQL($sequenceName)
{
return $this->sequenceNextValSql;
}
/**
* {@inheritDoc}
*/
public function getBooleanTypeDeclarationSQL(array $field)
{
}
/**
* {@inheritDoc}
*/
public function getIntegerTypeDeclarationSQL(array $field)
{
}
/**
* {@inheritDoc}
*/
public function getBigIntTypeDeclarationSQL(array $field)
{
}
/**
* {@inheritDoc}
*/
public function getSmallIntTypeDeclarationSQL(array $field)
{
}
/**
* {@inheritDoc}
*/
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
{
}
/**
* {@inheritDoc}
*/
public function getVarcharTypeDeclarationSQL(array $field)
{
}
/**
* {@inheritDoc}
*/
public function getClobTypeDeclarationSQL(array $field)
{
}
/* MOCK API */
/**
* @param bool $prefersIdentityColumns
*/
public function setPrefersIdentityColumns($prefersIdentityColumns)
{
$this->prefersIdentityColumns = $prefersIdentityColumns;
}
public function setPrefersSequences($bool)
{
$this->prefersSequences = $bool;
}
public function setSequenceNextValSql($sql)
{
$this->sequenceNextValSql = $sql;
}
public function getName()
{
return 'mock';
}
protected function initializeDoctrineTypeMappings()
{
}
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{
}
/**
* {@inheritDoc}
*/
public function getBlobTypeDeclarationSQL(array $field)
{
throw DBALException::notSupported(__METHOD__);
}
}
<?php
namespace Doctrine\Tests\Mocks;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Throwable;
class DriverMock implements Driver
{
/** @var DatabasePlatformMock */
private $platformMock;
/** @var AbstractSchemaManager */
private $schemaManagerMock;
/**
* {@inheritDoc}
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{
return new DriverConnectionMock();
}
public function getDatabasePlatform()
{
if (! $this->platformMock) {
$this->platformMock = new DatabasePlatformMock();
}
return $this->platformMock;
}
public function getSchemaManager(Connection $conn)
{
if ($this->schemaManagerMock === null) {
return new SchemaManagerMock($conn);
}
return $this->schemaManagerMock;
}
public function setDatabasePlatform(AbstractPlatform $platform)
{
$this->platformMock = $platform;
}
public function setSchemaManager(AbstractSchemaManager $sm)
{
$this->schemaManagerMock = $sm;
}
public function getName()
{
return 'mock';
}
public function getDatabase(Connection $conn)
{
return;
}
public function convertExceptionCode(Throwable $exception)
{
return 0;
}
}
<?php
namespace Doctrine\Tests\Mocks;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
class SchemaManagerMock extends AbstractSchemaManager
{
public function __construct(Connection $conn)
{
parent::__construct($conn);
}
protected function _getPortableTableColumnDefinition($tableColumn)
{
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment