Commit 917d4cb1 authored by Taylor Otwell's avatar Taylor Otwell

testing database.

parent 4167a295
......@@ -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.
......
......@@ -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 @@ class Manager {
*
* Note: Database connections are managed as singletons.
*
* @param string $connection
* @return Database\Connection
* @param string $connection
* @return Connection
*/
public function connection($connection = null)
{
......
<?php
use Laravel\Database\Manager;
class DatabaseManagerTest extends PHPUnit_Framework_TestCase {
public function testWhenCallingConnectionMethodForNonEstablishedConnectionNewConnectionIsReturned()
{
$manager = new Manager($this->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
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