ConnectionsTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\data;
  9. use Exception;
  10. use lithium\data\Connections;
  11. use lithium\data\source\Http;
  12. use lithium\data\source\Mock;
  13. use lithium\data\source\database\adapter\MySql;
  14. use lithium\data\source\database\adapter\PostgreSql;
  15. class ConnectionsTest extends \lithium\test\Unit {
  16. public $config = array(
  17. 'type' => 'Mock',
  18. 'adapter' => null,
  19. 'host' => 'localhost',
  20. 'login' => '--user--',
  21. 'password' => '--pass--',
  22. 'database' => 'db'
  23. );
  24. protected $_port = null;
  25. protected $_backup = array();
  26. public function setUp() {
  27. if (!$this->_backup) {
  28. foreach (Connections::get() as $conn) {
  29. $this->_backup[$conn] = Connections::get($conn, array('config' => true));
  30. }
  31. }
  32. Connections::reset();
  33. }
  34. public function tearDown() {
  35. Connections::reset();
  36. Connections::config($this->_backup);
  37. }
  38. public function testConnectionCreate() {
  39. $result = Connections::add('conn-test', array('type' => 'Mock') + $this->config);
  40. $expected = $this->config + array('type' => 'Mock');
  41. $this->assertEqual($expected, $result);
  42. $result = Connections::get('conn-test');
  43. $this->assertTrue($result instanceof Mock);
  44. $result = Connections::add('conn-test-2', $this->config);
  45. $this->assertEqual($expected, $result);
  46. $result = Connections::get('conn-test-2');
  47. $this->assertTrue($result instanceof Mock);
  48. }
  49. public function testConnectionGetAndReset() {
  50. Connections::add('conn-test', $this->config);
  51. Connections::add('conn-test-2', $this->config);
  52. $this->assertEqual(array('conn-test', 'conn-test-2'), Connections::get());
  53. $enabled = (MySql::enabled() || PostgreSql::enabled());
  54. $this->skipIf(!$enabled, 'MySql or PostgreSQL is not enabled');
  55. if (MySql::enabled()) {
  56. $this->_port = 3306;
  57. }
  58. if (PostgreSql::enabled()) {
  59. $this->_port = 5432;
  60. }
  61. $msg = "Cannot connect to localhost:{$this->_port}";
  62. $this->skipIf(!$this->_canConnect('localhost', $this->_port), $msg);
  63. $expected = $this->config + array('type' => 'database', 'filters' => array());
  64. $this->assertEqual($expected, Connections::get('conn-test', array('config' => true)));
  65. $this->assertNull(Connections::reset());
  66. $this->assertFalse(Connections::get());
  67. $this->assertTrue(is_array(Connections::get()));
  68. }
  69. public function testConnectionAutoInstantiation() {
  70. Connections::add('conn-test', $this->config);
  71. Connections::add('conn-test-2', $this->config);
  72. $result = Connections::get('conn-test');
  73. $this->assertTrue($result instanceof Mock);
  74. $result = Connections::get('conn-test');
  75. $this->assertTrue($result instanceof Mock);
  76. $this->assertNull(Connections::get('conn-test-2', array('autoCreate' => false)));
  77. }
  78. public function testInvalidConnection() {
  79. $this->assertNull(Connections::get('conn-invalid'));
  80. }
  81. public function testStreamConnection() {
  82. $config = array(
  83. 'type' => 'Http',
  84. 'socket' => 'Stream',
  85. 'host' => 'localhost',
  86. 'login' => 'root',
  87. 'password' => '',
  88. 'port' => '80'
  89. );
  90. Connections::add('stream-test', $config);
  91. $result = Connections::get('stream-test');
  92. $this->assertTrue($result instanceof Http);
  93. Connections::config(array('stream-test' => false));
  94. }
  95. public function testErrorExceptions() {
  96. $config = array(
  97. 'adapter' => 'None',
  98. 'type' => 'Error'
  99. );
  100. Connections::add('NoConnection', $config);
  101. $result = false;
  102. try {
  103. Connections::get('NoConnection');
  104. } catch(Exception $e) {
  105. $result = true;
  106. }
  107. $this->assertTrue($result, 'Exception is not thrown');
  108. }
  109. public function testGetNullAdapter() {
  110. Connections::reset();
  111. $this->assertTrue(Connections::get(false) instanceof Mock);
  112. }
  113. protected function _canConnect($host, $port) {
  114. $success = false;
  115. set_error_handler(function() {});
  116. if ($conn = fsockopen($host, $port)) {
  117. fclose($conn);
  118. $success = true;
  119. }
  120. restore_error_handler();
  121. return $success;
  122. }
  123. }
  124. ?>