TestSetup.php 2.7 KB

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