testing database.
This commit is contained in:
parent
4167a2953c
commit
917d4cb1e4
|
@ -25,7 +25,10 @@ class Connection {
|
||||||
* @param PDO $pdo
|
* @param PDO $pdo
|
||||||
* @return void
|
* @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.
|
* Execute a SQL query against the connection and return a scalar result.
|
||||||
|
|
|
@ -15,7 +15,10 @@ class Manager {
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct($config) { $this->config = $config; }
|
public function __construct($config)
|
||||||
|
{
|
||||||
|
$this->config = $config;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a database connection.
|
* Get a database connection.
|
||||||
|
@ -25,8 +28,8 @@ public function __construct($config) { $this->config = $config; }
|
||||||
*
|
*
|
||||||
* Note: Database connections are managed as singletons.
|
* Note: Database connections are managed as singletons.
|
||||||
*
|
*
|
||||||
* @param string $connection
|
* @param string $connection
|
||||||
* @return Database\Connection
|
* @return Connection
|
||||||
*/
|
*/
|
||||||
public function connection($connection = null)
|
public function connection($connection = null)
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,98 @@
|
||||||
|
<?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';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue