Common.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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-2008 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_Db_Expr
  24. */
  25. require_once 'Zend/Db/Expr.php';
  26. /**
  27. * PHPUnit_Util_Filter
  28. */
  29. require_once 'PHPUnit/Util/Filter.php';
  30. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  31. /**
  32. * @category Zend
  33. * @package Zend_Db
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. abstract class Zend_Db_TestUtil_Common
  39. {
  40. /**
  41. * @var Zend_Db_Adapter_Abstract
  42. */
  43. protected $_db = null;
  44. /**
  45. * @var array
  46. */
  47. protected $_tables = array();
  48. /**
  49. * @var array
  50. */
  51. protected $_sequences = array();
  52. protected function _getSqlCreateTable($tableName)
  53. {
  54. return 'CREATE TABLE ' . $this->_db->quoteIdentifier($tableName, true);
  55. }
  56. protected function _getSqlCreateTableType()
  57. {
  58. return null;
  59. }
  60. protected function _getSqlDropTable($tableName)
  61. {
  62. return 'DROP TABLE ' . $this->_db->quoteIdentifier($tableName, true);
  63. }
  64. public function getSqlType($type)
  65. {
  66. return $type;
  67. }
  68. public function createTable($tableId, array $columns = array())
  69. {
  70. if (!$columns) {
  71. $columns = $this->{'_getColumns'.$tableId}();
  72. }
  73. $tableName = $this->getTableName($tableId);
  74. $this->dropTable($tableName);
  75. if (isset($this->_tables[$tableName])) {
  76. return;
  77. }
  78. $sql = $this->_getSqlCreateTable($tableName);
  79. if (!$sql) {
  80. return;
  81. }
  82. $sql .= " (\n\t";
  83. $pKey = null;
  84. $pKeys = array();
  85. if (isset($columns['PRIMARY KEY'])) {
  86. $pKey = $columns['PRIMARY KEY'];
  87. unset($columns['PRIMARY KEY']);
  88. foreach (explode(',', $pKey) as $pKeyCol) {
  89. $pKeys[] = $this->_db->quoteIdentifier($pKeyCol, true);
  90. }
  91. $pKey = implode(', ', $pKeys);
  92. }
  93. foreach ($columns as $columnName => $type) {
  94. $col[] = $this->_db->quoteIdentifier($columnName, true) . ' ' . $this->getSqlType($type);
  95. }
  96. if ($pKey) {
  97. $col[] = "PRIMARY KEY ($pKey)";
  98. }
  99. $sql .= implode(",\n\t", $col);
  100. $sql .= "\n)" . $this->_getSqlCreateTableType();
  101. $result = $this->_rawQuery($sql);
  102. if ($result === false) {
  103. throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
  104. }
  105. $this->_tables[$tableName] = true;
  106. }
  107. public function dropTable($tableName = null)
  108. {
  109. if (!$tableName) {
  110. foreach ($this->_tableName as $tab) {
  111. $this->dropTable($tab);
  112. }
  113. return;
  114. }
  115. $sql = $this->_getSqlDropTable($tableName);
  116. if (!$sql) {
  117. return;
  118. }
  119. $result = $this->_rawQuery($sql);
  120. if ($result === false) {
  121. throw new Zend_Db_Exception("DROP TABLE statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
  122. }
  123. unset($this->_tables[$tableName]);
  124. }
  125. protected function _getSqlCreateSequence($sequenceName)
  126. {
  127. return null;
  128. }
  129. protected function _getSqlDropSequence($sequenceName)
  130. {
  131. return null;
  132. }
  133. public function createSequence($sequenceName)
  134. {
  135. $this->dropSequence($sequenceName);
  136. if (isset($this->_sequences[$sequenceName])) {
  137. return;
  138. }
  139. $sql = $this->_getSqlCreateSequence($sequenceName);
  140. if (!$sql) {
  141. return;
  142. }
  143. $result = $this->_rawQuery($sql);
  144. if ($result === false) {
  145. throw new Zend_Db_Exception("CREATE SEQUENCE statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
  146. }
  147. $this->_sequences[$sequenceName] = true;
  148. }
  149. public function dropSequence($sequenceName = null)
  150. {
  151. if (!$sequenceName) {
  152. foreach (array_keys($this->_sequences) as $seq) {
  153. $this->dropSequence($seq);
  154. }
  155. return;
  156. }
  157. $sql = $this->_getSqlDropSequence($sequenceName);
  158. if (!$sql) {
  159. return;
  160. }
  161. $result = $this->_rawQuery($sql);
  162. if ($result === false) {
  163. throw new Zend_Db_Exception("DROP SEQUENCE statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
  164. }
  165. unset($this->_sequences[$sequenceName]);
  166. }
  167. public function getParams(array $constants = array())
  168. {
  169. $params = array();
  170. foreach ($constants as $key => $constant) {
  171. if (defined($constant)) {
  172. $params[$key] = constant($constant);
  173. }
  174. }
  175. return $params;
  176. }
  177. public function getSchema()
  178. {
  179. $param = $this->getParams();
  180. if (isset($param['dbname']) && strpos($param['dbname'], ':') === false) {
  181. return $param['dbname'];
  182. }
  183. return null;
  184. }
  185. protected $_tableName = array(
  186. 'Accounts' => 'zfaccounts',
  187. 'Products' => 'zfproducts',
  188. 'Bugs' => 'zfbugs',
  189. 'BugsProducts' => 'zfbugs_products',
  190. 'noquote' => 'zfnoquote',
  191. 'noprimarykey' => 'zfnoprimarykey',
  192. 'Documents' => 'zfdocuments',
  193. 'Price' => 'zfprice',
  194. 'AltBugsProducts' => 'zfalt_bugs_products'
  195. );
  196. public function getTableName($tableId)
  197. {
  198. if (!isset($this->_tableName)) {
  199. throw new Exception("Invalid table id '$tableId'");
  200. }
  201. if (array_key_exists($tableId, $this->_tableName)) {
  202. return $this->_tableName[$tableId];
  203. } else {
  204. return $tableId;
  205. }
  206. }
  207. protected function _getColumnsBugs()
  208. {
  209. return array(
  210. 'bug_id' => 'IDENTITY',
  211. 'bug_description' => 'VARCHAR(100)',
  212. 'bug_status' => 'VARCHAR(20)',
  213. 'created_on' => 'DATETIME',
  214. 'updated_on' => 'DATETIME',
  215. 'reported_by' => 'VARCHAR(100)',
  216. 'assigned_to' => 'VARCHAR(100)',
  217. 'verified_by' => 'VARCHAR(100)'
  218. );
  219. }
  220. protected function _getColumnsAccounts()
  221. {
  222. return array(
  223. 'account_name' => 'VARCHAR(100) NOT NULL',
  224. 'PRIMARY KEY' => 'account_name'
  225. );
  226. }
  227. protected function _getColumnsProducts()
  228. {
  229. return array(
  230. 'product_id' => 'IDENTITY',
  231. 'product_name' => 'VARCHAR(100)'
  232. );
  233. }
  234. protected function _getColumnsBugsProducts()
  235. {
  236. return array(
  237. 'bug_id' => 'INTEGER NOT NULL',
  238. 'product_id' => 'INTEGER NOT NULL',
  239. 'PRIMARY KEY' => 'bug_id,product_id'
  240. );
  241. }
  242. protected function _getColumnsDocuments()
  243. {
  244. return array(
  245. 'doc_id' => 'INTEGER NOT NULL',
  246. 'doc_clob' => 'CLOB',
  247. 'doc_blob' => 'BLOB',
  248. 'PRIMARY KEY' => 'doc_id'
  249. );
  250. }
  251. protected function _getColumnsPrice()
  252. {
  253. return array(
  254. 'product_id' => 'INTEGER NOT NULL',
  255. 'price_name' => 'VARCHAR(100)',
  256. 'price_total' => 'DECIMAL(10,2) NOT NULL',
  257. 'PRIMARY KEY' => 'product_id'
  258. );
  259. }
  260. protected function _getDataAccounts()
  261. {
  262. return array(
  263. array('account_name' => 'mmouse'),
  264. array('account_name' => 'dduck'),
  265. array('account_name' => 'goofy'),
  266. );
  267. }
  268. protected function _getDataBugs()
  269. {
  270. return array(
  271. array(
  272. 'bug_description' => 'System needs electricity to run',
  273. 'bug_status' => 'NEW',
  274. 'created_on' => '2007-04-01',
  275. 'updated_on' => '2007-04-01',
  276. 'reported_by' => 'goofy',
  277. 'assigned_to' => 'mmouse',
  278. 'verified_by' => 'dduck'
  279. ),
  280. array(
  281. 'bug_description' => 'Implement Do What I Mean function',
  282. 'bug_status' => 'VERIFIED',
  283. 'created_on' => '2007-04-02',
  284. 'updated_on' => '2007-04-02',
  285. 'reported_by' => 'goofy',
  286. 'assigned_to' => 'mmouse',
  287. 'verified_by' => 'dduck'
  288. ),
  289. array(
  290. 'bug_description' => 'Where are my keys?',
  291. 'bug_status' => 'FIXED',
  292. 'created_on' => '2007-04-03',
  293. 'updated_on' => '2007-04-03',
  294. 'reported_by' => 'dduck',
  295. 'assigned_to' => 'mmouse',
  296. 'verified_by' => 'dduck'
  297. ),
  298. array(
  299. 'bug_description' => 'Bug no product',
  300. 'bug_status' => 'INCOMPLETE',
  301. 'created_on' => '2007-04-04',
  302. 'updated_on' => '2007-04-04',
  303. 'reported_by' => 'mmouse',
  304. 'assigned_to' => 'goofy',
  305. 'verified_by' => 'dduck'
  306. )
  307. );
  308. }
  309. protected function _getDataProducts()
  310. {
  311. return array(
  312. array('product_name' => 'Windows'),
  313. array('product_name' => 'Linux'),
  314. array('product_name' => 'OS X'),
  315. );
  316. }
  317. protected function _getDataBugsProducts()
  318. {
  319. return array(
  320. array(
  321. 'bug_id' => 1,
  322. 'product_id' => 1
  323. ),
  324. array(
  325. 'bug_id' => 1,
  326. 'product_id' => 2
  327. ),
  328. array(
  329. 'bug_id' => 1,
  330. 'product_id' => 3
  331. ),
  332. array(
  333. 'bug_id' => 2,
  334. 'product_id' => 3
  335. ),
  336. array(
  337. 'bug_id' => 3,
  338. 'product_id' => 2
  339. ),
  340. array(
  341. 'bug_id' => 3,
  342. 'product_id' => 3
  343. ),
  344. );
  345. }
  346. protected function _getDataDocuments()
  347. {
  348. return array (
  349. array(
  350. 'doc_id' => 1,
  351. 'doc_clob' => 'this is the clob that never ends...'.
  352. 'this is the clob that never ends...'.
  353. 'this is the clob that never ends...',
  354. 'doc_blob' => 'this is the blob that never ends...'.
  355. 'this is the blob that never ends...'.
  356. 'this is the blob that never ends...'
  357. )
  358. );
  359. }
  360. protected function _getDataPrice()
  361. {
  362. return array(
  363. array(
  364. 'product_id' => 1,
  365. 'price_name' => 'Price 1',
  366. 'price_total' => 200.45
  367. )
  368. );
  369. }
  370. public function populateTable($tableId)
  371. {
  372. $tableName = $this->getTableName($tableId);
  373. $data = $this->{'_getData'.$tableId}();
  374. foreach ($data as $row) {
  375. $sql = 'INSERT INTO ' . $this->_db->quoteIdentifier($tableName, true);
  376. $cols = array();
  377. $vals = array();
  378. foreach ($row as $col => $val) {
  379. $cols[] = $this->_db->quoteIdentifier($col, true);
  380. if ($val instanceof Zend_Db_Expr) {
  381. $vals[] = $val->__toString();
  382. } else {
  383. $vals[] = $this->_db->quote($val);
  384. }
  385. }
  386. $sql .= ' (' . implode(', ', $cols) . ')';
  387. $sql .= ' VALUES (' . implode(', ', $vals) . ')';
  388. $result = $this->_rawQuery($sql);
  389. if ($result === false) {
  390. throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
  391. }
  392. }
  393. }
  394. protected function _getSqlCreateView($viewName)
  395. {
  396. return 'CREATE VIEW ' . $this->_db->quoteIdentifier($viewName, true);
  397. }
  398. protected function _getSqlDropView($viewName)
  399. {
  400. return 'DROP VIEW ' . $this->_db->quoteIdentifier($viewName, true);
  401. }
  402. public function createView()
  403. {
  404. $sql = $this->_getSqlCreateView('temp_view')
  405. . ' AS SELECT * FROM '
  406. . $this->_db->quoteIdentifier('zfbugs', true);
  407. $result = $this->_rawQuery($sql);
  408. if ($result === false) {
  409. throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
  410. }
  411. }
  412. public function dropView()
  413. {
  414. $sql = $this->_getSqlDropView('temp_view');
  415. if (!$sql) {
  416. return;
  417. }
  418. $result = $this->_rawQuery($sql);
  419. if ($result === false) {
  420. throw new Zend_Db_Exception("Statement failed:\n$sql\nError: " . $this->_db->getConnection()->error);
  421. }
  422. }
  423. public function setUp(Zend_Db_Adapter_Abstract $db)
  424. {
  425. $this->setAdapter($db);
  426. $this->createTable('Accounts');
  427. $this->populateTable('Accounts');
  428. $this->createTable('Products');
  429. $this->populateTable('Products');
  430. $this->createTable('Bugs');
  431. $this->populateTable('Bugs');
  432. $this->createTable('BugsProducts');
  433. $this->populateTable('BugsProducts');
  434. $this->createTable('Documents');
  435. $this->populateTable('Documents');
  436. $this->createTable('Price');
  437. $this->populateTable('Price');
  438. $this->createView();
  439. }
  440. public function setAdapter(Zend_Db_Adapter_Abstract $db)
  441. {
  442. $this->_db = $db;
  443. }
  444. public function tearDown()
  445. {
  446. $this->dropView();
  447. $this->dropTable();
  448. $this->dropSequence();
  449. $this->_db->closeConnection();
  450. }
  451. protected abstract function _rawQuery($sql);
  452. }