TestSetup.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category ZendX
  16. * @package ZendX_Db
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: $
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * @see Zend_Loader
  28. */
  29. require_once 'Zend/Loader.php';
  30. /**
  31. * @see Zend_Db
  32. */
  33. require_once 'Zend/Db.php';
  34. /**
  35. * PHPUnit_Framework_TestCase
  36. */
  37. require_once 'PHPUnit/Framework/TestCase.php';
  38. /**
  39. * PHPUnit_Util_Filter
  40. */
  41. require_once 'PHPUnit/Util/Filter.php';
  42. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  43. /**
  44. * @category ZendX
  45. * @package ZendX_Db
  46. * @subpackage UnitTests
  47. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  48. * @license http://framework.zend.com/license/new-bsd New BSD License
  49. */
  50. abstract class ZendX_Db_TestSetup extends PHPUnit_Framework_TestCase
  51. {
  52. /**
  53. * @var ZendX_Db_TestUtil
  54. */
  55. protected $_util = null;
  56. /**
  57. * @var Zend_Db_Adapter_Abstract
  58. */
  59. protected $_db = null;
  60. public abstract function getDriver();
  61. /**
  62. * Subclasses should call parent::setUp() before
  63. * doing their own logic, e.g. creating metadata.
  64. */
  65. public function setUp()
  66. {
  67. $this->_setUpTestUtil();
  68. $this->_setUpAdapter();
  69. $this->_util->setUp($this->_db);
  70. }
  71. /**
  72. * Get a TestUtil class for the current RDBMS brand.
  73. */
  74. protected function _setUpTestUtil()
  75. {
  76. $driver = $this->getDriver();
  77. $utilClass = "ZendX_Db_TestUtil_{$driver}";
  78. Zend_Loader::loadClass($utilClass);
  79. $this->_util = new $utilClass();
  80. }
  81. /**
  82. * Open a new database connection
  83. */
  84. protected function _setUpAdapter()
  85. {
  86. $params = $this->_util->getParams();
  87. $params['adapterNamespace'] = 'ZendX_Db_Adapter';
  88. $this->_db = Zend_Db::factory($this->getDriver(), $params);
  89. try {
  90. $conn = $this->_db->getConnection();
  91. } catch (Zend_Exception $e) {
  92. $this->_db = null;
  93. $this->assertType('Zend_Db_Adapter_Exception', $e,
  94. 'Expecting Zend_Db_Adapter_Exception, got ' . get_class($e));
  95. $this->markTestSkipped($e->getMessage());
  96. }
  97. }
  98. /**
  99. * Subclasses should call parent::tearDown() after
  100. * doing their own logic, e.g. deleting metadata.
  101. */
  102. public function tearDown()
  103. {
  104. $this->_util->tearDown();
  105. $this->_db = null;
  106. }
  107. }