DatabaseManagerTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. use Laravel\Database\Manager;
  3. class DatabaseManagerTest extends PHPUnit_Framework_TestCase {
  4. public function testWhenCallingConnectionMethodForNonEstablishedConnectionNewConnectionIsReturned()
  5. {
  6. $manager = new Manager($this->getConfig());
  7. $connection = $manager->connection();
  8. $this->assertInstanceOf('PDOStub', $connection->pdo);
  9. $this->assertInstanceOf('Laravel\\Database\\Connection', $connection);
  10. }
  11. public function testConnectionMethodsReturnsSingletonConnections()
  12. {
  13. $manager = new Manager($this->getConfig());
  14. $connection = $manager->connection();
  15. $this->assertTrue($connection === $manager->connection());
  16. }
  17. public function testConnectionMethodOverridesDefaultWhenConnectionNameIsGiven()
  18. {
  19. $config = $this->getConfig();
  20. $config['connectors']['something'] = function($config) {return new AnotherPDOStub;};
  21. $manager = new Manager($config);
  22. $this->assertInstanceOf('AnotherPDOStub', $manager->connection('something')->pdo);
  23. }
  24. public function testConfigurationArrayIsPassedToConnector()
  25. {
  26. $manager = new Manager($this->getConfig());
  27. $this->assertEquals($manager->connection()->pdo->config, $this->getConfig());
  28. }
  29. /**
  30. * @expectedException Exception
  31. */
  32. public function testExceptionIsThrownIfConnectorIsNotDefined()
  33. {
  34. $manager = new Manager($this->getConfig());
  35. $manager->connection('something');
  36. }
  37. public function testTableMethodCallsTableMethodOnConnection()
  38. {
  39. $manager = new Manager($this->getConfig());
  40. $this->assertEquals($manager->table('users'), 'table');
  41. }
  42. // ---------------------------------------------------------------------
  43. // Support Functions
  44. // ---------------------------------------------------------------------
  45. private function getConfig()
  46. {
  47. return array('default' => 'test', 'connectors' => array('test' => function($config) {return new PDOStub($config);}));
  48. }
  49. }
  50. // ---------------------------------------------------------------------
  51. // Stubs
  52. // ---------------------------------------------------------------------
  53. class PDOStub extends PDO {
  54. public $config;
  55. public function __construct($config = array()) { $this->config = $config; }
  56. public function table()
  57. {
  58. return 'table';
  59. }
  60. }
  61. class AnotherPDOStub extends PDO {
  62. public function __construct() {}
  63. public function table()
  64. {
  65. return 'anotherTable';
  66. }
  67. }