From afbae7efa5f12c54d319aba215ad53fff1a5e7a0 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sun, 24 Feb 2019 10:05:49 -0800 Subject: [PATCH] Remove more hard-coded mock classes --- tests/Doctrine/Tests/DBAL/ConnectionTest.php | 21 ++- .../Doctrine/Tests/DBAL/DriverManagerTest.php | 25 ++-- tests/Doctrine/Tests/Mocks/ConnectionMock.php | 70 ---------- .../Tests/Mocks/DatabasePlatformMock.php | 121 ------------------ tests/Doctrine/Tests/Mocks/DriverMock.php | 68 ---------- .../Tests/Mocks/SchemaManagerMock.php | 18 --- 6 files changed, 31 insertions(+), 292 deletions(-) delete mode 100644 tests/Doctrine/Tests/Mocks/ConnectionMock.php delete mode 100644 tests/Doctrine/Tests/Mocks/DatabasePlatformMock.php delete mode 100644 tests/Doctrine/Tests/Mocks/DriverMock.php delete mode 100644 tests/Doctrine/Tests/Mocks/SchemaManagerMock.php diff --git a/tests/Doctrine/Tests/DBAL/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/ConnectionTest.php index 3eed3a72650..c036bd89885 100644 --- a/tests/Doctrine/Tests/DBAL/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/ConnectionTest.php @@ -22,7 +22,6 @@ use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\Tests\DbalTestCase; -use Doctrine\Tests\Mocks\DriverMock; use Doctrine\Tests\Mocks\DriverStatementMock; use Doctrine\Tests\Mocks\ServerInfoAwareConnectionMock; use Doctrine\Tests\Mocks\VersionAwarePlatformDriverMock; @@ -165,10 +164,22 @@ public function testConnectDispatchEvent() public function testEventManagerPassedToPlatform() { - $driverMock = new DriverMock(); - $connection = new Connection($this->params, $driverMock); - self::assertInstanceOf(EventManager::class, $connection->getDatabasePlatform()->getEventManager()); - self::assertSame($connection->getEventManager(), $connection->getDatabasePlatform()->getEventManager()); + $eventManager = new EventManager(); + + /** @var AbstractPlatform|MockObject $driver */ + $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(); } /** diff --git a/tests/Doctrine/Tests/DBAL/DriverManagerTest.php b/tests/Doctrine/Tests/DBAL/DriverManagerTest.php index 0fe228aa45a..0346405cb82 100644 --- a/tests/Doctrine/Tests/DBAL/DriverManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/DriverManagerTest.php @@ -2,8 +2,10 @@ namespace Doctrine\Tests\DBAL; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connections\MasterSlaveConnection; use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver as DrizzlePDOMySqlDriver; use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySQLDriver; use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver; @@ -13,11 +15,10 @@ use Doctrine\DBAL\Sharding\PoolingShardConnection; use Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser; use Doctrine\Tests\DbalTestCase; -use Doctrine\Tests\Mocks\ConnectionMock; -use Doctrine\Tests\Mocks\DriverMock; use PDO; use stdClass; use function extension_loaded; +use function get_class; use function in_array; use function is_array; @@ -92,7 +93,8 @@ public function testCustomPlatform() */ public function testCustomWrapper() { - $wrapperClass = ConnectionMock::class; + $wrapper = $this->createMock(Connection::class); + $wrapperClass = get_class($wrapper); $options = [ 'pdo' => new PDO('sqlite::memory:'), @@ -238,6 +240,9 @@ public function testDatabaseUrl($url, $expected) public function databaseUrls() { + $driver = $this->createMock(Driver::class); + $driverClass = get_class($driver); + return [ 'simple URL' => [ 'mysql://foo:bar@localhost/baz', @@ -406,14 +411,14 @@ public function databaseUrls() 'URL without scheme but custom driver' => [ [ 'url' => '//foo:bar@localhost/baz', - 'driverClass' => DriverMock::class, + 'driverClass' => $driverClass, ], [ 'user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', - 'driverClass' => DriverMock::class, + 'driverClass' => $driverClass, ], ], 'URL without scheme but default PDO driver and default driver' => [ @@ -434,14 +439,14 @@ public function databaseUrls() [ 'url' => '//foo:bar@localhost/baz', 'driver' => 'pdo_mysql', - 'driverClass' => DriverMock::class, + 'driverClass' => $driverClass, ], [ 'user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', - 'driverClass' => DriverMock::class, + 'driverClass' => $driverClass, ], ], 'URL with default PDO driver' => [ @@ -473,7 +478,7 @@ public function databaseUrls() 'URL with default custom driver' => [ [ 'url' => 'mysql://foo:bar@localhost/baz', - 'driverClass' => DriverMock::class, + 'driverClass' => $driverClass, ], [ 'user' => 'foo', @@ -501,7 +506,7 @@ public function databaseUrls() [ 'url' => 'mysql://foo:bar@localhost/baz', 'driver' => 'sqlite', - 'driverClass' => DriverMock::class, + 'driverClass' => $driverClass, ], [ 'user' => 'foo', @@ -516,7 +521,7 @@ public function databaseUrls() 'url' => 'mysql://foo:bar@localhost/baz', 'pdo' => true, 'driver' => 'sqlite', - 'driverClass' => DriverMock::class, + 'driverClass' => $driverClass, ], [ 'user' => 'foo', diff --git a/tests/Doctrine/Tests/Mocks/ConnectionMock.php b/tests/Doctrine/Tests/Mocks/ConnectionMock.php deleted file mode 100644 index 157584f09f7..00000000000 --- a/tests/Doctrine/Tests/Mocks/ConnectionMock.php +++ /dev/null @@ -1,70 +0,0 @@ -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; - } -} diff --git a/tests/Doctrine/Tests/Mocks/DatabasePlatformMock.php b/tests/Doctrine/Tests/Mocks/DatabasePlatformMock.php deleted file mode 100644 index 690809e308a..00000000000 --- a/tests/Doctrine/Tests/Mocks/DatabasePlatformMock.php +++ /dev/null @@ -1,121 +0,0 @@ -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__); - } -} diff --git a/tests/Doctrine/Tests/Mocks/DriverMock.php b/tests/Doctrine/Tests/Mocks/DriverMock.php deleted file mode 100644 index 9b5f4245472..00000000000 --- a/tests/Doctrine/Tests/Mocks/DriverMock.php +++ /dev/null @@ -1,68 +0,0 @@ -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; - } -} diff --git a/tests/Doctrine/Tests/Mocks/SchemaManagerMock.php b/tests/Doctrine/Tests/Mocks/SchemaManagerMock.php deleted file mode 100644 index 6eccd68609e..00000000000 --- a/tests/Doctrine/Tests/Mocks/SchemaManagerMock.php +++ /dev/null @@ -1,18 +0,0 @@ -