From 917d4cb1e48b1567ee7789bd1812ba5f1ff644dd Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 15 Sep 2011 23:03:47 -0500 Subject: [PATCH] testing database. --- laravel/database/connection.php | 5 +- laravel/database/manager.php | 9 ++- tests/Database/DatabaseManagerTest.php | 98 ++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 tests/Database/DatabaseManagerTest.php diff --git a/laravel/database/connection.php b/laravel/database/connection.php index cbacda52..bc7998c8 100644 --- a/laravel/database/connection.php +++ b/laravel/database/connection.php @@ -25,7 +25,10 @@ class Connection { * @param PDO $pdo * @return void */ - public function __construct(PDO $pdo) { $this->pdo = $pdo; } + public function __construct(PDO $pdo) + { + $this->pdo = $pdo; + } /** * Execute a SQL query against the connection and return a scalar result. diff --git a/laravel/database/manager.php b/laravel/database/manager.php index 2593d33f..33d0022c 100644 --- a/laravel/database/manager.php +++ b/laravel/database/manager.php @@ -15,7 +15,10 @@ class Manager { * @param array $config * @return void */ - public function __construct($config) { $this->config = $config; } + public function __construct($config) + { + $this->config = $config; + } /** * Get a database connection. @@ -25,8 +28,8 @@ public function __construct($config) { $this->config = $config; } * * Note: Database connections are managed as singletons. * - * @param string $connection - * @return Database\Connection + * @param string $connection + * @return Connection */ public function connection($connection = null) { diff --git a/tests/Database/DatabaseManagerTest.php b/tests/Database/DatabaseManagerTest.php new file mode 100644 index 00000000..17104a67 --- /dev/null +++ b/tests/Database/DatabaseManagerTest.php @@ -0,0 +1,98 @@ +getConfig()); + + $connection = $manager->connection(); + + $this->assertInstanceOf('PDOStub', $connection->pdo); + $this->assertInstanceOf('Laravel\\Database\\Connection', $connection); + } + + public function testConnectionMethodsReturnsSingletonConnections() + { + $manager = new Manager($this->getConfig()); + + $connection = $manager->connection(); + + $this->assertTrue($connection === $manager->connection()); + } + + public function testConnectionMethodOverridesDefaultWhenConnectionNameIsGiven() + { + $config = $this->getConfig(); + + $config['connectors']['something'] = function($config) {return new AnotherPDOStub;}; + + $manager = new Manager($config); + + $this->assertInstanceOf('AnotherPDOStub', $manager->connection('something')->pdo); + } + + public function testConfigurationArrayIsPassedToConnector() + { + $manager = new Manager($this->getConfig()); + + $this->assertEquals($manager->connection()->pdo->config, $this->getConfig()); + } + + /** + * @expectedException Exception + */ + public function testExceptionIsThrownIfConnectorIsNotDefined() + { + $manager = new Manager($this->getConfig()); + + $manager->connection('something'); + } + + public function testTableMethodCallsTableMethodOnConnection() + { + $manager = new Manager($this->getConfig()); + + $this->assertEquals($manager->table('users'), 'table'); + } + + // --------------------------------------------------------------------- + // Support Functions + // --------------------------------------------------------------------- + + private function getConfig() + { + return array('default' => 'test', 'connectors' => array('test' => function($config) {return new PDOStub($config);})); + } + +} + +// --------------------------------------------------------------------- +// Stubs +// --------------------------------------------------------------------- + +class PDOStub extends PDO { + + public $config; + + public function __construct($config = array()) { $this->config = $config; } + + public function table() + { + return 'table'; + } + +} + +class AnotherPDOStub extends PDO { + + public function __construct() {} + + public function table() + { + return 'anotherTable'; + } + +} \ No newline at end of file