From a7a8f31a25dac171ddd6779b5c2160af447c11f9 Mon Sep 17 00:00:00 2001 From: jeroendedauw Date: Sun, 27 Jul 2014 11:22:48 +0200 Subject: [PATCH 1/2] foreach AS -> foreach as --- .../Tests/DBAL/Functional/ExceptionTest.php | 22 +++++++++---------- .../DBAL/Functional/ModifyLimitQueryTest.php | 2 +- .../Tests/DBAL/Functional/PortabilityTest.php | 2 +- .../Tests/DBAL/Functional/ResultCacheTest.php | 10 ++++----- .../SchemaManagerFunctionalTestCase.php | 6 ++--- .../DBAL/Functional/TemporaryTableTest.php | 4 ++-- .../DBAL/Functional/TypeConversionTest.php | 2 +- .../Tests/DBAL/Functional/WriteTest.php | 2 +- .../DBAL/Schema/Platforms/MySQLSchemaTest.php | 2 +- .../Doctrine/Tests/DbalFunctionalTestCase.php | 4 ++-- tests/Doctrine/Tests/TestUtil.php | 4 ++-- 11 files changed, 30 insertions(+), 30 deletions(-) diff --git a/tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php b/tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php index 885b41f10cb..1af6879e8e5 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php @@ -20,7 +20,7 @@ public function testPrimaryConstraintViolationException() $table->addColumn('id', 'integer', array()); $table->setPrimaryKey(array('id')); - foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) { + foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) as $sql) { $this->_conn->executeQuery($sql); } @@ -46,10 +46,10 @@ public function testTableExistsException() $table->setPrimaryKey(array('id')); $this->setExpectedException('\Doctrine\DBAL\Exception\TableExistsException'); - foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) { + foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) as $sql) { $this->_conn->executeQuery($sql); } - foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) { + foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) as $sql) { $this->_conn->executeQuery($sql); } } @@ -72,7 +72,7 @@ public function testForeignKeyContraintViolationException() $owningTable->setPrimaryKey(array('id')); $owningTable->addForeignKeyConstraint($table, array('constraint_id'), array('id')); - foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) { + foreach ($schema->toSql($this->_conn->getDatabasePlatform()) as $sql) { $this->_conn->executeQuery($sql); } @@ -92,7 +92,7 @@ public function testNotNullConstraintViolationException() $table->addColumn('value', 'integer', array('notnull' => true)); $table->setPrimaryKey(array('id')); - foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) { + foreach ($schema->toSql($this->_conn->getDatabasePlatform()) as $sql) { $this->_conn->executeQuery($sql); } @@ -107,7 +107,7 @@ public function testInvalidFieldNameException() $table = $schema->createTable("bad_fieldname_table"); $table->addColumn('id', 'integer', array()); - foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) { + foreach ($schema->toSql($this->_conn->getDatabasePlatform()) as $sql) { $this->_conn->executeQuery($sql); } @@ -125,7 +125,7 @@ public function testNonUniqueFieldNameException() $table2 = $schema->createTable("ambiguous_list_table_2"); $table2->addColumn('id', 'integer'); - foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) { + foreach ($schema->toSql($this->_conn->getDatabasePlatform()) as $sql) { $this->_conn->executeQuery($sql); } @@ -142,7 +142,7 @@ public function testUniqueConstraintViolationException() $table->addColumn('id', 'integer'); $table->addUniqueIndex(array('id')); - foreach ($schema->toSql($this->_conn->getDatabasePlatform()) AS $sql) { + foreach ($schema->toSql($this->_conn->getDatabasePlatform()) as $sql) { $this->_conn->executeQuery($sql); } @@ -157,7 +157,7 @@ public function testSyntaxErrorException() $table->addColumn('id', 'integer', array()); $table->setPrimaryKey(array('id')); - foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) { + foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) as $sql) { $this->_conn->executeQuery($sql); } @@ -195,7 +195,7 @@ public function testConnectionExceptionSqLite($mode, $exceptionClass) $table->addColumn('id', 'integer'); $this->setExpectedException($exceptionClass); - foreach ($schema->toSql($conn->getDatabasePlatform()) AS $sql) { + foreach ($schema->toSql($conn->getDatabasePlatform()) as $sql) { $conn->executeQuery($sql); } } @@ -236,7 +236,7 @@ public function testConnectionException($params) $this->setExpectedException('Doctrine\DBAL\Exception\ConnectionException'); - foreach ($schema->toSql($conn->getDatabasePlatform()) AS $sql) { + foreach ($schema->toSql($conn->getDatabasePlatform()) as $sql) { $conn->executeQuery($sql); } } diff --git a/tests/Doctrine/Tests/DBAL/Functional/ModifyLimitQueryTest.php b/tests/Doctrine/Tests/DBAL/Functional/ModifyLimitQueryTest.php index b93c1d0f50e..2af46431617 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ModifyLimitQueryTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ModifyLimitQueryTest.php @@ -116,7 +116,7 @@ public function assertLimitResult($expectedResults, $sql, $limit, $offset, $dete { $p = $this->_conn->getDatabasePlatform(); $data = array(); - foreach ($this->_conn->fetchAll($p->modifyLimitQuery($sql, $limit, $offset)) AS $row) { + foreach ($this->_conn->fetchAll($p->modifyLimitQuery($sql, $limit, $offset)) as $row) { $row = array_change_key_case($row, CASE_LOWER); $data[] = $row['test_int']; } diff --git a/tests/Doctrine/Tests/DBAL/Functional/PortabilityTest.php b/tests/Doctrine/Tests/DBAL/Functional/PortabilityTest.php index 2484b49495b..5a1a803863d 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/PortabilityTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/PortabilityTest.php @@ -109,7 +109,7 @@ public function testConnFetchMode() public function assertFetchResultRows($rows) { $this->assertEquals(2, count($rows)); - foreach ($rows AS $row) { + foreach ($rows as $row) { $this->assertFetchResultRow($row); } } diff --git a/tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php b/tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php index 23c5bf1a437..8f83f574fdc 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php @@ -31,7 +31,7 @@ public function setUp() } $this->_conn->executeUpdate('DELETE FROM caching'); - foreach ($this->expectedResult AS $row) { + foreach ($this->expectedResult as $row) { $this->_conn->insert('caching', $row); } @@ -50,7 +50,7 @@ public function testCacheFetchAssoc() public function testFetchNum() { $expectedResult = array(); - foreach ($this->expectedResult AS $v) { + foreach ($this->expectedResult as $v) { $expectedResult[] = array_values($v); } $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_NUM); @@ -59,7 +59,7 @@ public function testFetchNum() public function testFetchBoth() { $expectedResult = array(); - foreach ($this->expectedResult AS $v) { + foreach ($this->expectedResult as $v) { $expectedResult[] = array_merge($v, array_values($v)); } $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_BOTH); @@ -68,7 +68,7 @@ public function testFetchBoth() public function testFetchColumn() { $expectedResult = array(); - foreach ($this->expectedResult AS $v) { + foreach ($this->expectedResult as $v) { $expectedResult[] = array_shift($v); } $this->assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, \PDO::FETCH_COLUMN); @@ -77,7 +77,7 @@ public function testFetchColumn() public function testMixingFetch() { $numExpectedResult = array(); - foreach ($this->expectedResult AS $v) { + foreach ($this->expectedResult as $v) { $numExpectedResult[] = array_values($v); } $stmt = $this->_conn->executeQuery("SELECT * FROM caching ORDER BY test_int ASC", array(), array(), new QueryCacheProfile(10, "testcachekey")); diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php index 30087324f20..492f4fb1d9c 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php @@ -69,7 +69,7 @@ public function testListSequences() $this->assertInternalType('array', $sequences, 'listSequences() should return an array.'); $foundSequence = null; - foreach($sequences AS $sequence) { + foreach($sequences as $sequence) { $this->assertInstanceOf('Doctrine\DBAL\Schema\Sequence', $sequence, 'Array elements of listSequences() should be Sequence instances.'); if(strtolower($sequence->getName()) == 'list_sequences_test_seq') { $foundSequence = $sequence; @@ -104,7 +104,7 @@ public function testListTables() $this->assertTrue(count($tables) > 0, "List Tables has to find at least one table named 'list_tables_test'."); $foundTable = false; - foreach ($tables AS $table) { + foreach ($tables as $table) { $this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table); if (strtolower($table->getName()) == 'list_tables_test') { $foundTable = true; @@ -722,7 +722,7 @@ protected function getTestCompositeTable($name) protected function assertHasTable($tables, $tableName) { $foundTable = false; - foreach ($tables AS $table) { + foreach ($tables as $table) { $this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table, 'No Table instance was found in tables array.'); if (strtolower($table->getName()) == 'list_tables_test_new_name') { $foundTable = true; diff --git a/tests/Doctrine/Tests/DBAL/Functional/TemporaryTableTest.php b/tests/Doctrine/Tests/DBAL/Functional/TemporaryTableTest.php index 665022d0787..70fceddb020 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/TemporaryTableTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/TemporaryTableTest.php @@ -50,7 +50,7 @@ public function testDropTemporaryTableNotAutoCommitTransaction() $table->addColumn("id", "integer"); $table->setPrimaryKey(array('id')); - foreach ($platform->getCreateTableSQL($table) AS $sql) { + foreach ($platform->getCreateTableSQL($table) as $sql) { $this->_conn->executeQuery($sql); } @@ -87,7 +87,7 @@ public function testCreateTemporaryTableNotAutoCommitTransaction() $table->addColumn("id", "integer"); $table->setPrimaryKey(array('id')); - foreach ($platform->getCreateTableSQL($table) AS $sql) { + foreach ($platform->getCreateTableSQL($table) as $sql) { $this->_conn->executeQuery($sql); } diff --git a/tests/Doctrine/Tests/DBAL/Functional/TypeConversionTest.php b/tests/Doctrine/Tests/DBAL/Functional/TypeConversionTest.php index ed0604e693f..99c4d29b0e3 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/TypeConversionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/TypeConversionTest.php @@ -36,7 +36,7 @@ public function setUp() $table->setPrimaryKey(array('id')); try { - foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) { + foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) as $sql) { $this->_conn->executeQuery($sql); } } catch(\Exception $e) { diff --git a/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php b/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php index dca9a74ae9f..6f5900b8b9a 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php @@ -18,7 +18,7 @@ public function setUp() $table->addColumn('test_string', 'string', array('notnull' => false)); $table->setPrimaryKey(array('id')); - foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) { + foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) as $sql) { $this->_conn->executeQuery($sql); } } catch(\Exception $e) { diff --git a/tests/Doctrine/Tests/DBAL/Schema/Platforms/MySQLSchemaTest.php b/tests/Doctrine/Tests/DBAL/Schema/Platforms/MySQLSchemaTest.php index 9755c992df5..a6c389e0ffc 100644 --- a/tests/Doctrine/Tests/DBAL/Schema/Platforms/MySQLSchemaTest.php +++ b/tests/Doctrine/Tests/DBAL/Schema/Platforms/MySQLSchemaTest.php @@ -55,7 +55,7 @@ public function testGenerateForeignKeySQL() $tableOld->addUnnamedForeignKeyConstraint('test_foreign', array('foo_id'), array('foo_id')); $sqls = array(); - foreach ($tableOld->getForeignKeys() AS $fk) { + foreach ($tableOld->getForeignKeys() as $fk) { $sqls[] = $this->platform->getCreateForeignKeySQL($fk, $tableOld); } diff --git a/tests/Doctrine/Tests/DbalFunctionalTestCase.php b/tests/Doctrine/Tests/DbalFunctionalTestCase.php index 27dd1495d78..db299edcb0f 100644 --- a/tests/Doctrine/Tests/DbalFunctionalTestCase.php +++ b/tests/Doctrine/Tests/DbalFunctionalTestCase.php @@ -49,7 +49,7 @@ protected function onNotSuccessfulTest(\Exception $e) if(isset($this->_sqlLoggerStack->queries) && count($this->_sqlLoggerStack->queries)) { $queries = ""; $i = count($this->_sqlLoggerStack->queries); - foreach (array_reverse($this->_sqlLoggerStack->queries) AS $query) { + foreach (array_reverse($this->_sqlLoggerStack->queries) as $query) { $params = array_map(function($p) { if (is_object($p)) return get_class($p); else return "'".$p."'"; }, $query['params'] ?: array()); $queries .= ($i+1).". SQL: '".$query['sql']."' Params: ".implode(", ", $params).PHP_EOL; $i--; @@ -57,7 +57,7 @@ protected function onNotSuccessfulTest(\Exception $e) $trace = $e->getTrace(); $traceMsg = ""; - foreach($trace AS $part) { + foreach($trace as $part) { if(isset($part['file'])) { if(strpos($part['file'], "PHPUnit/") !== false) { // Beginning with PHPUnit files we don't print the trace anymore. diff --git a/tests/Doctrine/Tests/TestUtil.php b/tests/Doctrine/Tests/TestUtil.php index c756c47cd65..f46449730b2 100644 --- a/tests/Doctrine/Tests/TestUtil.php +++ b/tests/Doctrine/Tests/TestUtil.php @@ -60,7 +60,7 @@ public static function getConnection() $schema = $sm->createSchema(); $stmts = $schema->toDropSql($realConn->getDatabasePlatform()); - foreach ($stmts AS $stmt) { + foreach ($stmts as $stmt) { $realConn->exec($stmt); } } @@ -80,7 +80,7 @@ public static function getConnection() if (isset($GLOBALS['db_event_subscribers'])) { $evm = $conn->getEventManager(); - foreach (explode(",", $GLOBALS['db_event_subscribers']) AS $subscriberClass) { + foreach (explode(",", $GLOBALS['db_event_subscribers']) as $subscriberClass) { $subscriberInstance = new $subscriberClass(); $evm->addEventSubscriber($subscriberInstance); } From a27733a078482d58d1025e3e3b669a4efaccc965 Mon Sep 17 00:00:00 2001 From: jeroendedauw Date: Sun, 27 Jul 2014 13:30:15 +0200 Subject: [PATCH 2/2] Split the methods in TestUtil more and improve naming --- tests/Doctrine/Tests/TestUtil.php | 121 +++++++++++++++++------------- 1 file changed, 70 insertions(+), 51 deletions(-) diff --git a/tests/Doctrine/Tests/TestUtil.php b/tests/Doctrine/Tests/TestUtil.php index f46449730b2..570a70ad2c1 100644 --- a/tests/Doctrine/Tests/TestUtil.php +++ b/tests/Doctrine/Tests/TestUtil.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\DriverManager; /** @@ -32,61 +33,23 @@ class TestUtil * 1) Each invocation of this method returns a NEW database connection. * 2) The database is dropped and recreated to ensure it's clean. * - * @return \Doctrine\DBAL\Connection The database connection instance. + * @return Connection The database connection instance. */ public static function getConnection() { - if (self::hasRequiredConnectionParams()) { - $realDbParams = self::getConnectionParams(); - $tmpDbParams = self::getTmpConnectionParams(); - - $realConn = DriverManager::getConnection($realDbParams); - - // Connect to tmpdb in order to drop and create the real test db. - $tmpConn = DriverManager::getConnection($tmpDbParams); - - $platform = $tmpConn->getDatabasePlatform(); - - if ($platform->supportsCreateDropDatabase()) { - $dbname = $realConn->getDatabase(); - $realConn->close(); - - $tmpConn->getSchemaManager()->dropAndCreateDatabase($dbname); - - $tmpConn->close(); - } else { - $sm = $realConn->getSchemaManager(); + $conn = DriverManager::getConnection(self::getConnectionParams()); - $schema = $sm->createSchema(); - $stmts = $schema->toDropSql($realConn->getDatabasePlatform()); + self::addDbEventSubscribers($conn); - foreach ($stmts as $stmt) { - $realConn->exec($stmt); - } - } - - $conn = DriverManager::getConnection($realDbParams, null, null); - } else { - $params = array( - 'driver' => 'pdo_sqlite', - 'memory' => true - ); - if (isset($GLOBALS['db_path'])) { - $params['path'] = $GLOBALS['db_path']; - unlink($GLOBALS['db_path']); - } - $conn = DriverManager::getConnection($params); - } + return $conn; + } - if (isset($GLOBALS['db_event_subscribers'])) { - $evm = $conn->getEventManager(); - foreach (explode(",", $GLOBALS['db_event_subscribers']) as $subscriberClass) { - $subscriberInstance = new $subscriberClass(); - $evm->addEventSubscriber($subscriberInstance); - } + private static function getConnectionParams() { + if (self::hasRequiredConnectionParams()) { + return self::getSpecifiedConnectionParams(); } - return $conn; + return self::getFallbackConnectionParams(); } private static function hasRequiredConnectionParams() @@ -108,7 +71,63 @@ private static function hasRequiredConnectionParams() ); } - private static function getTmpConnectionParams() + private static function getSpecifiedConnectionParams() { + $realDbParams = self::getParamsForMainConnection(); + $tmpDbParams = self::getParamsForTemporaryConnection(); + + $realConn = DriverManager::getConnection($realDbParams); + + // Connect to tmpdb in order to drop and create the real test db. + $tmpConn = DriverManager::getConnection($tmpDbParams); + + $platform = $tmpConn->getDatabasePlatform(); + + if ($platform->supportsCreateDropDatabase()) { + $dbname = $realConn->getDatabase(); + $realConn->close(); + + $tmpConn->getSchemaManager()->dropAndCreateDatabase($dbname); + + $tmpConn->close(); + } else { + $sm = $realConn->getSchemaManager(); + + $schema = $sm->createSchema(); + $stmts = $schema->toDropSql($realConn->getDatabasePlatform()); + + foreach ($stmts as $stmt) { + $realConn->exec($stmt); + } + } + + return $realDbParams; + } + + private static function getFallbackConnectionParams() { + $params = array( + 'driver' => 'pdo_sqlite', + 'memory' => true + ); + + if (isset($GLOBALS['db_path'])) { + $params['path'] = $GLOBALS['db_path']; + unlink($GLOBALS['db_path']); + } + + return $params; + } + + private static function addDbEventSubscribers(Connection $conn) { + if (isset($GLOBALS['db_event_subscribers'])) { + $evm = $conn->getEventManager(); + foreach (explode(",", $GLOBALS['db_event_subscribers']) as $subscriberClass) { + $subscriberInstance = new $subscriberClass(); + $evm->addEventSubscriber($subscriberInstance); + } + } + } + + private static function getParamsForTemporaryConnection() { $connectionParams = array( 'driver' => $GLOBALS['tmpdb_type'], @@ -134,7 +153,7 @@ private static function getTmpConnectionParams() return $connectionParams; } - private static function getConnectionParams() + private static function getParamsForMainConnection() { $connectionParams = array( 'driver' => $GLOBALS['db_type'], @@ -157,10 +176,10 @@ private static function getConnectionParams() } /** - * @return \Doctrine\DBAL\Connection + * @return Connection */ public static function getTempConnection() { - return DriverManager::getConnection(self::getTmpConnectionParams()); + return DriverManager::getConnection(self::getParamsForTemporaryConnection()); } }