DbTableTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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_Session
  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_Session_SaveHandler_DbTable
  24. */
  25. require_once 'Zend/Session/SaveHandler/DbTable.php';
  26. /**
  27. * Unit testing for Zend_Session_SaveHandler_DbTable include all tests for
  28. * regular session handling
  29. *
  30. * @category Zend
  31. * @package Zend_Session
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Session
  36. * @group Zend_Db_Table
  37. */
  38. class Zend_Session_SaveHandler_DbTableTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * @var array
  42. */
  43. protected $_saveHandlerTableConfig = array(
  44. 'name' => 'sessions',
  45. 'primary' => array(
  46. 'id',
  47. 'save_path',
  48. 'name',
  49. ),
  50. Zend_Session_SaveHandler_DbTable::MODIFIED_COLUMN => 'modified',
  51. Zend_Session_SaveHandler_DbTable::LIFETIME_COLUMN => 'lifetime',
  52. Zend_Session_SaveHandler_DbTable::DATA_COLUMN => 'data',
  53. Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT => array(
  54. Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT_SESSION_ID,
  55. Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT_SESSION_SAVE_PATH,
  56. Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT_SESSION_NAME,
  57. ),
  58. );
  59. /**
  60. * @var Zend_Db_Adapter_Abstract
  61. */
  62. protected $_db;
  63. /**
  64. * Array to collect used Zend_Session_SaveHandler_DbTable objects, so they are not
  65. * destroyed before all tests are done and session is not closed
  66. *
  67. * @var array
  68. */
  69. protected $_usedSaveHandlers = array();
  70. /**
  71. * Setup performed prior to each test method
  72. *
  73. * @return void
  74. */
  75. public function setUp()
  76. {
  77. $this->_setupDb($this->_saveHandlerTableConfig['primary']);
  78. }
  79. /**
  80. * Tear-down operations performed after each test method
  81. *
  82. * @return void
  83. */
  84. public function tearDown()
  85. {
  86. if ($this->_db instanceof Zend_Db_Adapter_Abstract) {
  87. $this->_dropTable();
  88. }
  89. }
  90. public function testConfigPrimaryAssignmentFullConfig()
  91. {
  92. $this->_usedSaveHandlers[] =
  93. $saveHandler = new Zend_Session_SaveHandler_DbTable($this->_saveHandlerTableConfig);
  94. /**
  95. * @todo Test something other than that an exception is not thrown
  96. */
  97. }
  98. public function testConstructorThrowsExceptionGivenConfigAsNull()
  99. {
  100. try {
  101. $this->_usedSaveHandlers[] =
  102. $saveHandler = new Zend_Session_SaveHandler_DbTable(null);
  103. $this->fail('Expected Zend_Session_SaveHandler_Exception not thrown');
  104. } catch (Zend_Session_SaveHandler_Exception $e) {
  105. $this->assertContains('$config must be', $e->getMessage());
  106. }
  107. }
  108. public function testTableNameSchema()
  109. {
  110. $config = $this->_saveHandlerTableConfig;
  111. $config['name'] = 'schema.session';
  112. $this->_usedSaveHandlers[] =
  113. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  114. }
  115. public function testTableEmptyNamePullFromSavePath()
  116. {
  117. $config = $this->_saveHandlerTableConfig;
  118. unset($config['name']);
  119. try {
  120. $savePath = ini_get('session.save_path');
  121. ini_set('session.save_path', dirname(__FILE__));
  122. $this->_usedSaveHandlers[] =
  123. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  124. $this->fail();
  125. } catch (Zend_Session_SaveHandler_Exception $e) {
  126. ini_set('session.save_path', $savePath);
  127. /**
  128. * @todo Test something other than that an exception is thrown
  129. */
  130. }
  131. }
  132. public function testPrimaryAssignmentIdNotSet()
  133. {
  134. $this->setExpectedException('Zend_Session_SaveHandler_Exception');
  135. $config = $this->_saveHandlerTableConfig;
  136. $config['primary'] = array('id');
  137. $config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT]
  138. = Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT_SESSION_SAVE_PATH;
  139. $this->_usedSaveHandlers[] =
  140. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  141. /**
  142. * @todo Test something other than that an exception is thrown
  143. */
  144. }
  145. public function testPrimaryAssignmentNotArray()
  146. {
  147. $config = $this->_saveHandlerTableConfig;
  148. $config['primary'] = array('id');
  149. $config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT]
  150. = Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT_SESSION_ID;
  151. $this->_usedSaveHandlers[] =
  152. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  153. /**
  154. * @todo Test something other than that an exception is not thrown
  155. */
  156. }
  157. public function testModifiedColumnNotSet()
  158. {
  159. $this->setExpectedException('Zend_Session_SaveHandler_Exception');
  160. $config = $this->_saveHandlerTableConfig;
  161. unset($config[Zend_Session_SaveHandler_DbTable::MODIFIED_COLUMN]);
  162. $this->_usedSaveHandlers[] =
  163. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  164. /**
  165. * @todo Test something other than that an exception is thrown
  166. */
  167. }
  168. public function testLifetimeColumnNotSet()
  169. {
  170. $this->setExpectedException('Zend_Session_SaveHandler_Exception');
  171. $config = $this->_saveHandlerTableConfig;
  172. unset($config[Zend_Session_SaveHandler_DbTable::LIFETIME_COLUMN]);
  173. $this->_usedSaveHandlers[] =
  174. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  175. /**
  176. * @todo Test something other than that an exception is thrown
  177. */
  178. }
  179. public function testDataColumnNotSet()
  180. {
  181. $this->setExpectedException('Zend_Session_SaveHandler_Exception');
  182. $config = $this->_saveHandlerTableConfig;
  183. unset($config[Zend_Session_SaveHandler_DbTable::DATA_COLUMN]);
  184. $this->_usedSaveHandlers[] =
  185. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  186. /**
  187. * @todo Test something other than that an exception is thrown
  188. */
  189. }
  190. public function testDifferentArraySize()
  191. {
  192. //different number of args between primary and primaryAssignment
  193. try {
  194. $config = $this->_saveHandlerTableConfig;
  195. array_pop($config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT]);
  196. $this->_usedSaveHandlers[] =
  197. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  198. $this->fail();
  199. } catch (Zend_Session_SaveHandler_Exception $e) {
  200. /**
  201. * @todo Test something other than that an exception is thrown
  202. */
  203. }
  204. }
  205. public function testEmptyPrimaryAssignment()
  206. {
  207. //test the default - no primaryAssignment
  208. $config = $this->_saveHandlerTableConfig;
  209. unset($config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT]);
  210. $config['primary'] = $config['primary'][0];
  211. $this->_usedSaveHandlers[] =
  212. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  213. /**
  214. * @todo Test something other than that an exception is not thrown
  215. */
  216. }
  217. public function testSessionIdPresent()
  218. {
  219. //test that the session Id must be in the primary assignment config
  220. try {
  221. $config = $this->_saveHandlerTableConfig;
  222. $config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT] = array(
  223. Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT_SESSION_NAME,
  224. );
  225. $this->_usedSaveHandlers[] =
  226. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  227. $this->fail();
  228. } catch (Zend_Session_SaveHandler_Exception $e) {
  229. /**
  230. * @todo Test something other than that an exception is thrown
  231. */
  232. }
  233. }
  234. public function testModifiedColumnDefined()
  235. {
  236. //test the default - no primaryAssignment
  237. try {
  238. $config = $this->_saveHandlerTableConfig;
  239. unset($config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT]);
  240. unset($config[Zend_Session_SaveHandler_DbTable::MODIFIED_COLUMN]);
  241. $this->_usedSaveHandlers[] =
  242. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  243. $this->fail();
  244. } catch (Zend_Session_SaveHandler_Exception $e) {
  245. /**
  246. * @todo Test something other than that an exception is thrown
  247. */
  248. }
  249. }
  250. public function testLifetimeColumnDefined()
  251. {
  252. //test the default - no primaryAssignment
  253. try {
  254. $config = $this->_saveHandlerTableConfig;
  255. unset($config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT]);
  256. unset($config[Zend_Session_SaveHandler_DbTable::LIFETIME_COLUMN]);
  257. $this->_usedSaveHandlers[] =
  258. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  259. $this->fail();
  260. } catch (Zend_Session_SaveHandler_Exception $e) {
  261. /**
  262. * @todo Test something other than that an exception is thrown
  263. */
  264. }
  265. }
  266. public function testDataColumnDefined()
  267. {
  268. //test the default - no primaryAssignment
  269. try {
  270. $config = $this->_saveHandlerTableConfig;
  271. unset($config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT]);
  272. unset($config[Zend_Session_SaveHandler_DbTable::DATA_COLUMN]);
  273. $this->_usedSaveHandlers[] =
  274. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  275. $this->fail();
  276. } catch (Zend_Session_SaveHandler_Exception $e) {
  277. /**
  278. * @todo Test something other than that an exception is thrown
  279. */
  280. }
  281. }
  282. public function testLifetime()
  283. {
  284. $config = $this->_saveHandlerTableConfig;
  285. unset($config['lifetime']);
  286. $this->_usedSaveHandlers[] =
  287. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  288. $this->assertSame($saveHandler->getLifetime(), (int) ini_get('session.gc_maxlifetime'),
  289. 'lifetime must default to session.gc_maxlifetime'
  290. );
  291. $config = $this->_saveHandlerTableConfig;
  292. $lifetime = $config['lifetime'] = 1242;
  293. $this->_usedSaveHandlers[] =
  294. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  295. $this->assertSame($lifetime, $saveHandler->getLifetime());
  296. }
  297. public function testOverrideLifetime()
  298. {
  299. try {
  300. $config = $this->_saveHandlerTableConfig;
  301. $config['overrideLifetime'] = true;
  302. $this->_usedSaveHandlers[] =
  303. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  304. } catch (Zend_Session_SaveHandler_Exception $e) {
  305. /**
  306. * @todo Test something other than that an exception is thrown
  307. */
  308. }
  309. $this->assertTrue($saveHandler->getOverrideLifetime(), '');
  310. }
  311. public function testSessionSaving()
  312. {
  313. $this->_dropTable();
  314. $config = $this->_saveHandlerTableConfig;
  315. unset($config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT]);
  316. $config['primary'] = array($config['primary'][0]);
  317. $this->_setupDb($config['primary']);
  318. $this->_usedSaveHandlers[] =
  319. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  320. Zend_Session::setSaveHandler($saveHandler);
  321. Zend_Session::start();
  322. /**
  323. * @see Zend_Session_Namespace
  324. */
  325. require_once 'Zend/Session/Namespace.php';
  326. $session = new Zend_Session_Namespace('SaveHandler');
  327. $session->testArray = $this->_saveHandlerTableConfig;
  328. $tmp = array('SaveHandler' => serialize(array('testArray' => $this->_saveHandlerTableConfig)));
  329. $testAgainst = '';
  330. foreach ($tmp as $key => $val) {
  331. $testAgainst .= $key . "|" . $val;
  332. }
  333. session_write_close();
  334. foreach ($this->_db->query('SELECT * FROM Sessions')->fetchAll() as $row) {
  335. $this->assertSame($row[$config[Zend_Session_SaveHandler_DbTable::DATA_COLUMN]],
  336. $testAgainst, 'Data was not saved properly'
  337. );
  338. }
  339. }
  340. public function testReadWrite()
  341. {
  342. $config = $this->_saveHandlerTableConfig;
  343. unset($config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT]);
  344. $config['primary'] = array($config['primary'][0]);
  345. $this->_setupDb($config['primary']);
  346. $this->_usedSaveHandlers[] =
  347. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  348. $id = '242';
  349. $this->assertTrue($saveHandler->write($id, serialize($config)));
  350. $this->assertSame($config, unserialize($saveHandler->read($id)));
  351. }
  352. public function testReadWriteComplex()
  353. {
  354. $config = $this->_saveHandlerTableConfig;
  355. $this->_setupDb($config['primary']);
  356. $this->_usedSaveHandlers[] =
  357. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  358. $saveHandler->open('savepath', 'sessionname');
  359. $id = '242';
  360. $this->assertTrue($saveHandler->write($id, serialize($config)));
  361. $this->assertSame($config, unserialize($saveHandler->read($id)));
  362. }
  363. public function testReadWriteTwice()
  364. {
  365. $config = $this->_saveHandlerTableConfig;
  366. unset($config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT]);
  367. $config['primary'] = array($config['primary'][0]);
  368. $this->_setupDb($config['primary']);
  369. $this->_usedSaveHandlers[] =
  370. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  371. $id = '242';
  372. $this->assertTrue($saveHandler->write($id, serialize($config)));
  373. $this->assertSame($config, unserialize($saveHandler->read($id)));
  374. $this->assertTrue($saveHandler->write($id, serialize($config)));
  375. $this->assertSame($config, unserialize($saveHandler->read($id)));
  376. }
  377. public function testReadWriteTwiceAndExpire()
  378. {
  379. $config = $this->_saveHandlerTableConfig;
  380. unset($config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT]);
  381. $config['primary'] = array($config['primary'][0]);
  382. $config['lifetime'] = 1;
  383. $this->_setupDb($config['primary']);
  384. $this->_usedSaveHandlers[] =
  385. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  386. $id = '242';
  387. $this->assertTrue($saveHandler->write($id, serialize($config)));
  388. $this->assertSame($config, unserialize($saveHandler->read($id)));
  389. $this->assertTrue($saveHandler->write($id, serialize($config)));
  390. sleep(2);
  391. $this->assertSame(false, unserialize($saveHandler->read($id)));
  392. }
  393. public function testReadWriteThreeTimesAndGc()
  394. {
  395. if (getenv('TRAVIS')) {
  396. $this->markTestSkipped(
  397. 'Test randomly fail on Travis CI.'
  398. );
  399. }
  400. $config = $this->_saveHandlerTableConfig;
  401. unset($config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT]);
  402. $config['primary'] = array($config['primary'][0]);
  403. $config['lifetime'] = 1;
  404. $this->_setupDb($config['primary']);
  405. $this->_usedSaveHandlers[] =
  406. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  407. $id = 242;
  408. $this->assertTrue($saveHandler->write($id, serialize($config)));
  409. $this->assertSame($config, unserialize($saveHandler->read($id)));
  410. $id++;
  411. $this->assertTrue($saveHandler->write($id, serialize($config)));
  412. $this->assertSame($config, unserialize($saveHandler->read($id)));
  413. $id++;
  414. $this->assertTrue($saveHandler->write($id, serialize($config)));
  415. $this->assertSame($config, unserialize($saveHandler->read($id)));
  416. foreach ($this->_db->query('SELECT * FROM Sessions')->fetchAll() as $row) {
  417. $this->assertSame($config, unserialize($row['data']));
  418. }
  419. sleep(2);
  420. $saveHandler->gc(false);
  421. foreach ($this->_db->query('SELECT * FROM Sessions')->fetchAll() as $row) {
  422. //should be empty!
  423. $this->fail();
  424. }
  425. }
  426. public function testSetLifetime()
  427. {
  428. $config = $this->_saveHandlerTableConfig;
  429. unset($config[Zend_Session_SaveHandler_DbTable::PRIMARY_ASSIGNMENT]);
  430. $config['primary'] = array($config['primary'][0]);
  431. $config['lifetime'] = 1;
  432. $this->_setupDb($config['primary']);
  433. $this->_usedSaveHandlers[] =
  434. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  435. $this->assertSame(1, $saveHandler->getLifetime());
  436. $saveHandler->setLifetime(27);
  437. $this->assertSame(27, $saveHandler->getLifetime());
  438. }
  439. public function testZendConfig()
  440. {
  441. $this->_usedSaveHandlers[] =
  442. $saveHandler = new Zend_Session_SaveHandler_DbTable(new Zend_Config($this->_saveHandlerTableConfig));
  443. /**
  444. * @todo Test something other than that an exception is not thrown
  445. */
  446. }
  447. /**
  448. * @group 9294
  449. */
  450. public function testDestroyWithAutoQuoteIdentifiersEnabledAndDisabled()
  451. {
  452. $id = uniqid();
  453. $config = $this->_saveHandlerTableConfig;
  454. $configDb = array(
  455. 'options' => array(
  456. 'autoQuoteIdentifiers' => false,
  457. ),
  458. 'profiler' => true
  459. );
  460. $this->_setupDb($config['primary'], $configDb);
  461. $config['db'] = $this->_db;
  462. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  463. $saveHandler->destroy($id);
  464. $lastQuery = $this->_db
  465. ->getProfiler()
  466. ->getLastQueryProfile()
  467. ->getQuery();
  468. $partQueryExpected = "WHERE (id = '$id') AND (save_path = '') AND (name = '')";
  469. $this->assertContains($partQueryExpected, $lastQuery);
  470. $configDb = array(
  471. 'options' => array(
  472. 'autoQuoteIdentifiers' => true,
  473. ),
  474. 'profiler' => true
  475. );
  476. $this->_setupDb($config['primary'], $configDb);
  477. $config['db'] = $this->_db;
  478. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  479. $saveHandler->destroy($id);
  480. $lastQuery = $this->_db
  481. ->getProfiler()
  482. ->getLastQueryProfile()
  483. ->getQuery();
  484. $partQueryExpected = "WHERE (\"id\" = '$id') AND (\"save_path\" = '') AND (\"name\" = '')";
  485. $this->assertContains($partQueryExpected, $lastQuery);
  486. }
  487. /**
  488. * @group 9294
  489. */
  490. public function testGcWithAutoQuoteIdentifiersEnabledAndDisabled()
  491. {
  492. $config = $this->_saveHandlerTableConfig;
  493. $configDb = array(
  494. 'options' => array(
  495. 'autoQuoteIdentifiers' => false,
  496. ),
  497. 'profiler' => true
  498. );
  499. $this->_setupDb($config['primary'], $configDb);
  500. $config['db'] = $this->_db;
  501. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  502. $saveHandler->gc(false);
  503. $lastQuery = $this->_db
  504. ->getProfiler()
  505. ->getLastQueryProfile()
  506. ->getQuery();
  507. $partQueryExpected = "WHERE (modified + lifetime < ";
  508. $this->assertContains($partQueryExpected, $lastQuery);
  509. $configDb = array(
  510. 'options' => array(
  511. 'autoQuoteIdentifiers' => true,
  512. ),
  513. 'profiler' => true
  514. );
  515. $this->_setupDb($config['primary'], $configDb);
  516. $config['db'] = $this->_db;
  517. $saveHandler = new Zend_Session_SaveHandler_DbTable($config);
  518. $saveHandler->gc(false);
  519. $lastQuery = $this->_db
  520. ->getProfiler()
  521. ->getLastQueryProfile()
  522. ->getQuery();
  523. $partQueryExpected = "WHERE (\"modified\" + \"lifetime\" < ";
  524. $this->assertContains($partQueryExpected, $lastQuery);
  525. }
  526. /**
  527. * Sets up the database connection and creates the table for session data
  528. *
  529. * @param array $primary
  530. * @return void
  531. */
  532. protected function _setupDb(array $primary, array $config = array())
  533. {
  534. if (!extension_loaded('pdo_sqlite')) {
  535. $this->markTestSkipped('The pdo_sqlite extension must be available and enabled for this test');
  536. }
  537. $this->_db = Zend_Db::factory('Pdo_Sqlite', array('dbname' => ':memory:') + $config);
  538. Zend_Db_Table_Abstract::setDefaultAdapter($this->_db);
  539. $query = array();
  540. $query[] = 'CREATE TABLE `Sessions` ( ';
  541. $query[] = '`id` varchar(32) NOT NULL, ';
  542. if (in_array('save_path', $primary)) {
  543. $query[] = '`save_path` varchar(32) NOT NULL, ';
  544. }
  545. if (in_array('name', $primary)) {
  546. $query[] = '`name` varchar(32) NOT NULL, ';
  547. }
  548. $query[] = '`modified` int(11) default NULL, ';
  549. $query[] = '`lifetime` int(11) default NULL, ';
  550. $query[] = '`data` text, ';
  551. $query[] = 'PRIMARY KEY (' . implode(', ', $primary) . ') ';
  552. $query[] = ');';
  553. $this->_db->query(implode("\n", $query));
  554. }
  555. /**
  556. * Drops the database table for session data
  557. *
  558. * @return void
  559. */
  560. protected function _dropTable()
  561. {
  562. $this->_db->query('DROP TABLE Sessions');
  563. }
  564. }
  565. /**
  566. * This class is used by Zend_Session_SaveHandler_AllTests to produce one skip message when pdo_sqlite is unavailable
  567. */
  568. class Zend_Session_SaveHandler_DbTableTestSkip extends PHPUnit_Framework_TestCase
  569. {
  570. public function testNothing()
  571. {
  572. $this->markTestSkipped('The pdo_sqlite extension must be available and enabled for this test');
  573. }
  574. }