TestSetup.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 Zend
  16. * @package Zend_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(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . '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. /**
  38. * @category Zend
  39. * @package Zend_Db
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. * @group Zend_Db
  44. */
  45. abstract class Zend_Db_TestSetup extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * @var Zend_Db_TestUtil
  49. */
  50. protected $_util = null;
  51. /**
  52. * @var Zend_Db_Adapter_Abstract
  53. */
  54. protected $_db = null;
  55. public abstract function getDriver();
  56. /**
  57. * Subclasses should call parent::setUp() before
  58. * doing their own logic, e.g. creating metadata.
  59. */
  60. public function setUp()
  61. {
  62. $this->_setUpTestUtil();
  63. $this->_setUpAdapter();
  64. $this->_util->setUp($this->_db);
  65. }
  66. /**
  67. * Get a TestUtil class for the current RDBMS brand.
  68. */
  69. protected function _setUpTestUtil()
  70. {
  71. $driver = $this->getDriver();
  72. $utilClass = "Zend_Db_TestUtil_{$driver}";
  73. Zend_Loader::loadClass($utilClass);
  74. $this->_util = new $utilClass();
  75. }
  76. /**
  77. * Open a new database connection
  78. */
  79. protected function _setUpAdapter()
  80. {
  81. $this->_db = Zend_Db::factory($this->getDriver(), $this->_util->getParams());
  82. try {
  83. $conn = $this->_db->getConnection();
  84. } catch (Zend_Exception $e) {
  85. $this->_db = null;
  86. $this->assertType('Zend_Db_Adapter_Exception', $e,
  87. 'Expecting Zend_Db_Adapter_Exception, got ' . get_class($e));
  88. $this->markTestSkipped($e->getMessage());
  89. }
  90. }
  91. /**
  92. * Subclasses should call parent::tearDown() after
  93. * doing their own logic, e.g. deleting metadata.
  94. */
  95. public function tearDown()
  96. {
  97. $this->_util->tearDown();
  98. $this->_db->closeConnection();
  99. $this->_db = null;
  100. }
  101. }