TestSetup.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. 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 Zend
  45. * @package Zend_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. * @group Zend_Db
  50. */
  51. abstract class Zend_Db_TestSetup extends PHPUnit_Framework_TestCase
  52. {
  53. /**
  54. * @var Zend_Db_TestUtil
  55. */
  56. protected $_util = null;
  57. /**
  58. * @var Zend_Db_Adapter_Abstract
  59. */
  60. protected $_db = null;
  61. public abstract function getDriver();
  62. /**
  63. * Subclasses should call parent::setUp() before
  64. * doing their own logic, e.g. creating metadata.
  65. */
  66. public function setUp()
  67. {
  68. $this->_setUpTestUtil();
  69. $this->_setUpAdapter();
  70. $this->_util->setUp($this->_db);
  71. }
  72. /**
  73. * Get a TestUtil class for the current RDBMS brand.
  74. */
  75. protected function _setUpTestUtil()
  76. {
  77. $driver = $this->getDriver();
  78. $utilClass = "Zend_Db_TestUtil_{$driver}";
  79. Zend_Loader::loadClass($utilClass);
  80. $this->_util = new $utilClass();
  81. }
  82. /**
  83. * Open a new database connection
  84. */
  85. protected function _setUpAdapter()
  86. {
  87. $this->_db = Zend_Db::factory($this->getDriver(), $this->_util->getParams());
  88. try {
  89. $conn = $this->_db->getConnection();
  90. } catch (Zend_Exception $e) {
  91. $this->_db = null;
  92. $this->assertType('Zend_Db_Adapter_Exception', $e,
  93. 'Expecting Zend_Db_Adapter_Exception, got ' . get_class($e));
  94. $this->markTestSkipped($e->getMessage());
  95. }
  96. }
  97. /**
  98. * Subclasses should call parent::tearDown() after
  99. * doing their own logic, e.g. deleting metadata.
  100. */
  101. public function tearDown()
  102. {
  103. $this->_util->tearDown();
  104. $this->_db->closeConnection();
  105. $this->_db = null;
  106. }
  107. }