diff --git a/core/tests/Drupal/Tests/Core/Database/ConnectionTest.php b/core/tests/Drupal/Tests/Core/Database/ConnectionTest.php index c1b65cb..20667ee 100644 --- a/core/tests/Drupal/Tests/Core/Database/ConnectionTest.php +++ b/core/tests/Drupal/Tests/Core/Database/ConnectionTest.php @@ -2,7 +2,7 @@ /** * @file - * Contains Drupal\system\Tests\Database\ConnectionTest. + * Contains Drupal\Tests\Core\Database\ConnectionTest. */ namespace Drupal\Tests\Core\Database; @@ -39,12 +39,12 @@ public static function getInfo() { * - Expected result from Connection::tablePrefix(). */ public function providerPrefixRoundTrip() { - return array ( - array ( - array( '' => 'test_' ), + return array( + array( + array('' => 'test_'), 'test_', ), - array ( + array( array( 'fooTable' => 'foo_', 'barTable' => 'bar_', @@ -62,19 +62,19 @@ public function providerPrefixRoundTrip() { * * @dataProvider providerPrefixRoundTrip */ - public function testPrefixRoundTrip($expected, $prefixInfo) { - $mockPDO = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); - $connection = new StubConnection($mockPDO, array()); + public function testPrefixRoundTrip($expected, $prefix_info) { + $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); + $connection = new StubConnection($mock_pdo, array()); // setPrefix() is protected, so we make it accessible with reflection. $reflection = new \ReflectionClass('Drupal\Tests\Core\Database\Stub\StubConnection'); - $setPrefix = $reflection->getMethod('setPrefix'); - $setPrefix->setAccessible(TRUE); + $set_prefix = $reflection->getMethod('setPrefix'); + $set_prefix->setAccessible(TRUE); // Set the prefix data. - $setPrefix->invokeArgs($connection, array($prefixInfo)); + $set_prefix->invokeArgs($connection, array($prefix_info)); // Check the round-trip. - foreach ($expected as $table=>$prefix) { + foreach ($expected as $table => $prefix) { $this->assertEquals($prefix, $connection->tablePrefix($table)); } } @@ -89,15 +89,15 @@ public function testPrefixRoundTrip($expected, $prefixInfo) { * - Query to be prefixed. */ public function providerTestPrefixTables() { - return array ( - array ( + return array( + array( 'SELECT * FROM test_table', 'test_', 'SELECT * FROM {table}', ), - array ( + array( 'SELECT * FROM first_table JOIN second_thingie', - array ( + array( 'table' => 'first_', 'thingie' => 'second_', ), @@ -111,9 +111,9 @@ public function providerTestPrefixTables() { * * @dataProvider providerTestPrefixTables */ - public function testPrefixTables($expected, $prefixInfo, $query) { - $mockPDO = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); - $connection = new StubConnection($mockPDO, array('prefix' => $prefixInfo)); + public function testPrefixTables($expected, $prefix_info, $query) { + $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); + $connection = new StubConnection($mock_pdo, array('prefix' => $prefix_info)); $this->assertEquals($expected, $connection->prefixTables($query)); } @@ -126,13 +126,13 @@ public function testPrefixTables($expected, $prefixInfo, $query) { * - String to escape. */ public function providerEscapeMethods() { - return array ( - array ('thing', 'thing'), - array ('_item', '_item'), - array ('item_', 'item_'), - array ('_item_', '_item_'), - array ('', '!@#$%^&*()-=+'), - array ('123', '!1@2#3'), + return array( + array('thing', 'thing'), + array('_item', '_item'), + array('item_', 'item_'), + array('_item_', '_item_'), + array('', '!@#$%^&*()-=+'), + array('123', '!1@2#3'), ); } @@ -146,8 +146,8 @@ public function providerEscapeMethods() { * @todo Separate test method for each escape method? */ public function testEscapeMethods($expected, $name) { - $mockPDO = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); - $connection = new StubConnection($mockPDO, array()); + $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); + $connection = new StubConnection($mock_pdo, array()); $this->assertEquals($expected, $connection->escapeDatabase($name)); $this->assertEquals($expected, $connection->escapeTable($name)); $this->assertEquals($expected, $connection->escapeField($name)); @@ -165,7 +165,7 @@ public function testEscapeMethods($expected, $name) { * - Class name without namespace. */ public function providerGetDriverClass() { - return array ( + return array( array( 'nonexistent_class', 'stub', @@ -193,8 +193,8 @@ public function providerGetDriverClass() { * @dataProvider providerGetDriverClass */ public function testGetDriverClass($expected, $driver, $namespace, $class) { - $mockPDO = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); - $connection = new StubConnection($mockPDO, array('namespace' => $namespace)); + $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); + $connection = new StubConnection($mock_pdo, array('namespace' => $namespace)); // Set the driver using our stub class' public property. $connection->driver = $driver; $this->assertEquals($expected, $connection->getDriverClass($class)); @@ -210,7 +210,7 @@ public function testGetDriverClass($expected, $driver, $namespace, $class) { * - Namespace for connection. */ public function providerSchema() { - return array ( + return array( array( 'Drupal\\Tests\\Core\\Database\\Stub\\Driver\\Schema', 'stub', @@ -225,8 +225,8 @@ public function providerSchema() { * @dataProvider providerSchema */ public function testSchema($expected, $driver, $namespace) { - $mockPDO = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); - $connection = new StubConnection($mockPDO, array('namespace' => $namespace)); + $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); + $connection = new StubConnection($mock_pdo, array('namespace' => $namespace)); $connection->driver = $driver; $this->assertInstanceOf($expected, $connection->schema()); } @@ -235,12 +235,12 @@ public function testSchema($expected, $driver, $namespace) { * Test Connection::destroy(). */ public function testDestroy() { - $mockPDO = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); + $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); // Mocking StubConnection gives us access to the $schema attribute. $connection = $this->getMock( 'Drupal\Tests\Core\Database\Stub\StubConnection', NULL, - array($mockPDO, array('namespace' => 'Drupal\\Tests\\Core\\Database\\Stub\\Driver')) + array($mock_pdo, array('namespace' => 'Drupal\\Tests\\Core\\Database\\Stub\\Driver')) ); // Generate a schema object in order to verify that we've NULLed it later. $this->assertInstanceOf( @@ -261,9 +261,18 @@ public function testDestroy() { */ public function providerMakeComments() { return array( - array ('/* */ ', array ('')), - array ('/* Exploit * / DROP TABLE node; -- */ ', array('Exploit * / DROP TABLE node; --')), - array ('/* Exploit DROP TABLE node; --; another comment */ ', array('Exploit */ DROP TABLE node; --', 'another comment')), + array( + '/* */ ', + array(''), + ), + array( + '/* Exploit * / DROP TABLE node; -- */ ', + array('Exploit * / DROP TABLE node; --'), + ), + array( + '/* Exploit DROP TABLE node; --; another comment */ ', + array('Exploit */ DROP TABLE node; --', 'another comment'), + ), ); } @@ -272,10 +281,10 @@ public function providerMakeComments() { * * @dataProvider providerMakeComments */ - public function testMakeComments($expected, $commentArray) { - $mockPDO = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); - $connection = new StubConnection($mockPDO, array()); - $this->assertEquals($expected, $connection->makeComment($commentArray)); + public function testMakeComments($expected, $comment_array) { + $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); + $connection = new StubConnection($mock_pdo, array()); + $this->assertEquals($expected, $connection->makeComment($comment_array)); } /** @@ -288,9 +297,9 @@ public function testMakeComments($expected, $commentArray) { */ public function providerFilterComments() { return array( - array ('', ''), - array ('Exploit * / DROP TABLE node; --', 'Exploit * / DROP TABLE node; --'), - array ('Exploit DROP TABLE node; --', 'Exploit */ DROP TABLE node; --'), + array('', ''), + array('Exploit * / DROP TABLE node; --', 'Exploit * / DROP TABLE node; --'), + array('Exploit DROP TABLE node; --', 'Exploit */ DROP TABLE node; --'), ); } @@ -300,17 +309,17 @@ public function providerFilterComments() { * @dataProvider providerFilterComments */ public function testFilterComments($expected, $comment) { - $mockPDO = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); - $connection = new StubConnection($mockPDO, array()); + $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO'); + $connection = new StubConnection($mock_pdo, array()); // filterComment() is protected, so we make it accessible with reflection. $reflection = new \ReflectionClass('Drupal\Tests\Core\Database\Stub\StubConnection'); - $filterComment = $reflection->getMethod('filterComment'); - $filterComment->setAccessible(TRUE); + $filter_comment = $reflection->getMethod('filterComment'); + $filter_comment->setAccessible(TRUE); $this->assertEquals( $expected, - $filterComment->invokeArgs($connection, array($comment)) + $filter_comment->invokeArgs($connection, array($comment)) ); } diff --git a/core/tests/Drupal/Tests/Core/Database/Stub/StubPDO.php b/core/tests/Drupal/Tests/Core/Database/Stub/StubPDO.php index 5653053..c0ea2be 100644 --- a/core/tests/Drupal/Tests/Core/Database/Stub/StubPDO.php +++ b/core/tests/Drupal/Tests/Core/Database/Stub/StubPDO.php @@ -11,9 +11,24 @@ * A stub of \PDO for testing purposes. * * We override the constructor method so that PHPUnit can mock the \PDO class. + * \PDO itself can't be mocked, so we have to create a subclass. This subclass + * is being used to unit test Connection, so we don't need a functional database + * but we do need a mock \PDO object. + * + * @see Drupal\Tests\Core\Database\ConnectionTest + * @see Drupal\Core\Database\Connection + * @see http://stackoverflow.com/questions/3138946/mocking-the-pdo-object-using-phpunit */ class StubPDO extends \PDO { + /** + * Construction method. + * + * We override this construction method with a no-op in order to mock \PDO + * under unit tests. + * + * @see http://stackoverflow.com/questions/3138946/mocking-the-pdo-object-using-phpunit + */ public function __construct() { // No-op. }