PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/share/test/data/pdo/statement-test.php

https://github.com/php-tox/tox
PHP | 783 lines | 632 code | 52 blank | 99 comment | 1 complexity | 2e606f476902404aef06ae538e7afbd0 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Defines the test case for Tox\Data\Pdo\Statement.
  4. *
  5. * This file is part of Tox.
  6. *
  7. * Tox is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Tox is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Tox. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @copyright Š 2012-2013 PHP-Tox.org
  21. * @license GNU General Public License, version 3
  22. */
  23. namespace Tox\Data\Pdo;
  24. use PHPUnit_Framework_TestCase;
  25. require_once __DIR__ . '/../../../../src/core/assembly.php';
  26. require_once __DIR__ . '/../../../../src/data/ipdostatement.php';
  27. require_once __DIR__ . '/../../../../src/data/pdo/statement.php';
  28. require_once __DIR__ . '/../../../../src/core/exception.php';
  29. require_once __DIR__ . '/../../../../src/data/pdo/closedstatementexception.php';
  30. require_once __DIR__ . '/../../../../src/data/pdo/executingfailureexception.php';
  31. require_once __DIR__ . '/../../../../src/data/pdo/columnbindingfailureexception.php';
  32. require_once __DIR__ . '/../../../../src/data/pdo/parambindingfailureexception.php';
  33. require_once __DIR__ . '/../../../../src/data/pdo/valuebindingfailureexception.php';
  34. require_once __DIR__ . '/../../../../src/data/pdo/cursorclosingfailureexception.php';
  35. require_once __DIR__ . '/../../../../src/data/pdo/rowsetiteratingfailureexception.php';
  36. require_once __DIR__ . '/../../../../src/data/pdo/attributesettingfailureexception.php';
  37. require_once __DIR__ . '/../../../../src/data/pdo/fetchmodesettingfailureexception.php';
  38. require_once __DIR__ . '/../../../../src/data/pdo/executedstatementexception.php';
  39. require_once __DIR__ . '/../../../../src/data/isource.php';
  40. require_once __DIR__ . '/../../../../src/data/ipdo.php';
  41. use Tox\Data;
  42. use Exception;
  43. /**
  44. * Tests Tox\Data\Pdo\Statement.
  45. *
  46. * @internal
  47. *
  48. * @package tox.data.pdo
  49. * @author Snakevil Zen <zsnakevil@gmail.com>
  50. */
  51. class StatementTest extends PHPUnit_Framework_TestCase
  52. {
  53. /**
  54. * Stores the mocking data object for testing.
  55. *
  56. * @var Data\IPdo
  57. */
  58. protected $pdo;
  59. protected function setUp()
  60. {
  61. $this->pdo = $this->getMock('Tox\\Data\\IPdo');
  62. }
  63. public function testStringCasting()
  64. {
  65. $s_sql = microtime();
  66. $o_stmt = $this->getMockForAbstractClass(
  67. 'Tox\\Data\\Pdo\\Statement',
  68. array($this->pdo, $s_sql)
  69. );
  70. $this->assertEquals($s_sql, (string) $o_stmt);
  71. }
  72. public function testSha1AlgorithmUsedForId()
  73. {
  74. $o_stmt = $this->getMockForAbstractClass(
  75. 'Tox\\Data\\Pdo\\Statement',
  76. array($this->pdo, microtime())
  77. );
  78. $this->assertRegExp('@^[\\da-z]{40}$@i', $o_stmt->getId());
  79. }
  80. public function testRetrievingPdo()
  81. {
  82. $o_stmt = $this->getMockForAbstractClass(
  83. 'Tox\\Data\\Pdo\\Statement',
  84. array($this->pdo, microtime())
  85. );
  86. $this->assertSame($this->pdo, $o_stmt->getPdo());
  87. }
  88. public function testRetrievingType()
  89. {
  90. $o_stmt = $this->getMockForAbstractClass(
  91. 'Tox\\Data\\Pdo\\Statement',
  92. array($this->pdo, microtime())
  93. );
  94. $this->assertNull($o_stmt->getType());
  95. }
  96. public function testMagicMethods()
  97. {
  98. $s_value = microtime();
  99. $o_stmt = $this->getMock(
  100. 'Tox\\Data\\Pdo\\Statement',
  101. array('getId', 'getType', 'getPdo'),
  102. array(),
  103. '',
  104. false
  105. );
  106. $o_stmt->expects($this->once())->method('getId')
  107. ->will($this->returnValue($s_value));
  108. $o_stmt->expects($this->once())->method('getType')
  109. ->will($this->returnValue($s_value));
  110. $o_stmt->expects($this->once())->method('getPdo')
  111. ->will($this->returnValue($s_value));
  112. $this->assertEquals($s_value, $o_stmt->id);
  113. $this->assertEquals($s_value, $o_stmt->type);
  114. $this->assertEquals($s_value, $o_stmt->pdo);
  115. }
  116. public function testLazyLoadingForBindColumn()
  117. {
  118. $i_key = rand();
  119. $s_val1 = microtime();
  120. $i_val2 = rand();
  121. $i_val3 = rand();
  122. $f_val4 = 4 + microtime(true);
  123. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  124. $o_stmt1->expects($this->once())->method('bindColumn')
  125. ->with(
  126. $this->equalTo($i_key),
  127. $this->equalTo($s_val1),
  128. $this->equalTo($i_val2),
  129. $this->equalTo($i_val3),
  130. $this->equalTo($f_val4)
  131. )->will($this->returnValue(microtime()));
  132. $this->pdo->expects($this->once())->method('realize')
  133. ->will($this->returnValue($o_stmt1));
  134. $o_stmt2 = $this->getMockForAbstractClass(
  135. 'Tox\\Data\\Pdo\\Statement',
  136. array($this->pdo, microtime())
  137. );
  138. $this->assertSame($o_stmt2, $o_stmt2->bindColumn($i_key, $s_val1, $i_val2, $i_val3, $f_val4));
  139. }
  140. public function testLazyLoadingForBindParam()
  141. {
  142. $i_key = rand();
  143. $s_val1 = microtime();
  144. $i_val2 = rand();
  145. $i_val3 = rand();
  146. $f_val4 = 4 + microtime(true);
  147. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  148. $o_stmt1->expects($this->once())->method('bindParam')
  149. ->with(
  150. $this->equalTo($i_key),
  151. $this->equalTo($s_val1),
  152. $this->equalTo($i_val2),
  153. $this->equalTo($i_val3),
  154. $this->equalTo($f_val4)
  155. )->will($this->returnValue(microtime()));
  156. $this->pdo->expects($this->once())->method('realize')
  157. ->will($this->returnValue($o_stmt1));
  158. $o_stmt2 = $this->getMockForAbstractClass(
  159. 'Tox\\Data\\Pdo\\Statement',
  160. array($this->pdo, microtime())
  161. );
  162. $this->assertSame($o_stmt2, $o_stmt2->bindParam($i_key, $s_val1, $i_val2, $i_val3, $f_val4));
  163. }
  164. public function testLazyLoadingForBindValue()
  165. {
  166. $this->pdo->expects($this->never())->method('realize');
  167. $o_stmt = $this->getMockForAbstractClass(
  168. 'Tox\\Data\\Pdo\\Statement',
  169. array($this->pdo, microtime())
  170. );
  171. $this->assertSame($o_stmt, $o_stmt->bindValue(microtime(), microtime()));
  172. }
  173. public function testLazyLoadingForCloseCursor()
  174. {
  175. $this->pdo->expects($this->never())->method('realize');
  176. $o_stmt = $this->getMockForAbstractClass(
  177. 'Tox\\Data\\Pdo\\Statement',
  178. array($this->pdo, microtime())
  179. );
  180. $o_stmt->closeCursor();
  181. }
  182. public function testLazyLoadingForColumnCount()
  183. {
  184. $m_ret = microtime();
  185. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  186. $o_stmt1->expects($this->once())->method('columnCount')
  187. ->will($this->returnValue($m_ret));
  188. $this->pdo->expects($this->once())->method('realize')
  189. ->will($this->returnValue($o_stmt1));
  190. $o_stmt2 = $this->getMockForAbstractClass(
  191. 'Tox\\Data\\Pdo\\Statement',
  192. array($this->pdo, microtime())
  193. );
  194. $this->assertEquals($m_ret, $o_stmt2->columnCount());
  195. }
  196. public function testLazyLoadingForDebugDumpParams()
  197. {
  198. $m_ret = microtime();
  199. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  200. $o_stmt1->expects($this->once())->method('debugDumpParams')
  201. ->will($this->returnValue($m_ret));
  202. $this->pdo->expects($this->once())->method('realize')
  203. ->will($this->returnValue($o_stmt1));
  204. $o_stmt2 = $this->getMockForAbstractClass(
  205. 'Tox\\Data\\Pdo\\Statement',
  206. array($this->pdo, microtime())
  207. );
  208. $this->assertEquals($m_ret, $o_stmt2->debugDumpParams());
  209. }
  210. public function testLazyLoadingForExecute()
  211. {
  212. $m_ret = microtime();
  213. $a_pars = array(microtime());
  214. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  215. $o_stmt1->expects($this->once())->method('execute')
  216. ->with($this->equalTo($a_pars))
  217. ->will($this->returnValue($m_ret));
  218. $this->pdo->expects($this->once())->method('realize')
  219. ->will($this->returnValue($o_stmt1));
  220. $o_stmt2 = $this->getMockForAbstractClass(
  221. 'Tox\\Data\\Pdo\\Statement',
  222. array($this->pdo, microtime())
  223. );
  224. $this->assertSame($o_stmt2, $o_stmt2->execute($a_pars));
  225. }
  226. public function testLazyLoadingForFetch()
  227. {
  228. $m_ret = microtime();
  229. $m_val1 = microtime();
  230. $m_val2 = microtime();
  231. $m_val3 = microtime();
  232. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  233. $o_stmt1->expects($this->once())->method('fetch')
  234. ->with($this->equalTo($m_val1), $this->equalTo($m_val2), $this->equalTo($m_val3))
  235. ->will($this->returnValue($m_ret));
  236. $this->pdo->expects($this->once())->method('realize')
  237. ->will($this->returnValue($o_stmt1));
  238. $o_stmt2 = $this->getMockForAbstractClass(
  239. 'Tox\\Data\\Pdo\\Statement',
  240. array($this->pdo, microtime())
  241. );
  242. $this->assertEquals($m_ret, $o_stmt2->fetch($m_val1, $m_val2, $m_val3));
  243. }
  244. public function testLazyLoadingForFetchAll()
  245. {
  246. $m_ret = microtime();
  247. $m_val1 = microtime();
  248. $m_val2 = microtime();
  249. $m_val3 = microtime();
  250. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  251. $o_stmt1->expects($this->once())->method('fetchAll')
  252. ->with($this->equalTo($m_val1), $this->equalTo($m_val2), $this->equalTo($m_val3))
  253. ->will($this->returnValue($m_ret));
  254. $this->pdo->expects($this->once())->method('realize')
  255. ->will($this->returnValue($o_stmt1));
  256. $o_stmt2 = $this->getMockForAbstractClass(
  257. 'Tox\\Data\\Pdo\\Statement',
  258. array($this->pdo, microtime())
  259. );
  260. $this->assertEquals($m_ret, $o_stmt2->fetchAll($m_val1, $m_val2, $m_val3));
  261. }
  262. public function testLazyLoadingForFetchColumn()
  263. {
  264. $m_ret = microtime();
  265. $m_val1 = microtime();
  266. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  267. $o_stmt1->expects($this->once())->method('fetchColumn')
  268. ->with($this->equalTo($m_val1))
  269. ->will($this->returnValue($m_ret));
  270. $this->pdo->expects($this->once())->method('realize')
  271. ->will($this->returnValue($o_stmt1));
  272. $o_stmt2 = $this->getMockForAbstractClass(
  273. 'Tox\\Data\\Pdo\\Statement',
  274. array($this->pdo, microtime())
  275. );
  276. $this->assertEquals($m_ret, $o_stmt2->fetchColumn($m_val1));
  277. }
  278. public function testLazyLoadingForFetchObject()
  279. {
  280. $m_ret = microtime();
  281. $m_val1 = microtime();
  282. $m_val2 = microtime();
  283. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  284. $o_stmt1->expects($this->once())->method('fetchObject')
  285. ->with($this->equalTo($m_val1), $this->equalTo($m_val2))
  286. ->will($this->returnValue($m_ret));
  287. $this->pdo->expects($this->once())->method('realize')
  288. ->will($this->returnValue($o_stmt1));
  289. $o_stmt2 = $this->getMockForAbstractClass(
  290. 'Tox\\Data\\Pdo\\Statement',
  291. array($this->pdo, microtime())
  292. );
  293. $this->assertEquals($m_ret, $o_stmt2->fetchObject($m_val1, $m_val2));
  294. }
  295. public function testLazyLoadingForGetAttribute()
  296. {
  297. $m_ret = microtime();
  298. $m_val1 = microtime();
  299. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  300. $o_stmt1->expects($this->once())->method('getAttribute')
  301. ->with($this->equalTo(intval($m_val1)))
  302. ->will($this->returnValue($m_ret));
  303. $this->pdo->expects($this->once())->method('realize')
  304. ->will($this->returnValue($o_stmt1));
  305. $o_stmt2 = $this->getMockForAbstractClass(
  306. 'Tox\\Data\\Pdo\\Statement',
  307. array($this->pdo, microtime())
  308. );
  309. $this->assertEquals($m_ret, $o_stmt2->getAttribute($m_val1));
  310. }
  311. public function testLazyLoadingForGetColumnMeta()
  312. {
  313. $m_ret = microtime();
  314. $m_val1 = microtime();
  315. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  316. $o_stmt1->expects($this->once())->method('getColumnMeta')
  317. ->with($this->equalTo($m_val1))
  318. ->will($this->returnValue($m_ret));
  319. $this->pdo->expects($this->once())->method('realize')
  320. ->will($this->returnValue($o_stmt1));
  321. $o_stmt2 = $this->getMockForAbstractClass(
  322. 'Tox\\Data\\Pdo\\Statement',
  323. array($this->pdo, microtime())
  324. );
  325. $this->assertEquals($m_ret, $o_stmt2->getColumnMeta($m_val1));
  326. }
  327. public function testLazyLoadingForNextRowset()
  328. {
  329. $m_ret = microtime();
  330. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  331. $o_stmt1->expects($this->once())->method('nextRowset')
  332. ->will($this->returnValue($m_ret));
  333. $this->pdo->expects($this->once())->method('realize')
  334. ->will($this->returnValue($o_stmt1));
  335. $o_stmt2 = $this->getMockForAbstractClass(
  336. 'Tox\\Data\\Pdo\\Statement',
  337. array($this->pdo, microtime())
  338. );
  339. $this->assertSame($o_stmt2, $o_stmt2->nextRowset());
  340. }
  341. public function testLazyLoadingForRowCount()
  342. {
  343. $m_ret = microtime();
  344. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  345. $o_stmt1->expects($this->once())->method('rowCount')
  346. ->will($this->returnValue($m_ret));
  347. $this->pdo->expects($this->once())->method('realize')
  348. ->will($this->returnValue($o_stmt1));
  349. $o_stmt2 = $this->getMockForAbstractClass(
  350. 'Tox\\Data\\Pdo\\Statement',
  351. array($this->pdo, microtime())
  352. );
  353. $this->assertEquals($m_ret, $o_stmt2->rowCount());
  354. }
  355. public function testLazyLoadingForSetAttribute()
  356. {
  357. $this->pdo->expects($this->never())->method('realize');
  358. $o_stmt = $this->getMockForAbstractClass(
  359. 'Tox\\Data\\Pdo\\Statement',
  360. array($this->pdo, microtime())
  361. );
  362. $this->assertSame($o_stmt, $o_stmt->setAttribute(microtime(), microtime()));
  363. }
  364. public function testLazyLoadingForSetFetchMode()
  365. {
  366. $this->pdo->expects($this->never())->method('realize');
  367. $o_stmt = $this->getMockForAbstractClass(
  368. 'Tox\\Data\\Pdo\\Statement',
  369. array($this->pdo, microtime())
  370. );
  371. $this->assertSame($o_stmt, $o_stmt->setFetchMode(microtime()));
  372. }
  373. /**
  374. * @depends testLazyLoadingForGetAttribute
  375. * @depends testLazyLoadingForSetAttribute
  376. */
  377. public function testLazyLoadingForGettingSetAttribute()
  378. {
  379. $i_attr = rand();
  380. $s_value = microtime();
  381. $o_stmt = $this->getMock(
  382. 'Tox\\Data\\Pdo\\Statement',
  383. array('realize'),
  384. array($this->pdo, microtime())
  385. );
  386. $o_stmt->expects($this->never())->method('realize');
  387. $this->assertEquals($s_value, $o_stmt->setAttribute($i_attr, $s_value)->getAttribute($i_attr));
  388. }
  389. /**
  390. * @depends testLazyLoadingForCloseCursor
  391. * @depends testLazyLoadingForFetch
  392. * @expectedException Tox\Data\Pdo\ClosedStatementException
  393. */
  394. public function testFetchForbiddenAfterCloseCursor()
  395. {
  396. $this->pdo->expects($this->never())->method('realize');
  397. $o_stmt = $this->getMockForAbstractClass(
  398. 'Tox\\Data\\Pdo\\Statement',
  399. array($this->pdo, microtime())
  400. );
  401. $o_stmt->closeCursor()->fetch();
  402. }
  403. /**
  404. * @depends testLazyLoadingForBindParam
  405. * @expectedException Tox\Data\Pdo\ExecutedStatementException
  406. */
  407. public function testBindParamForbiddenAfterExecute()
  408. {
  409. $s_key = microtime();
  410. $s_val = microtime();
  411. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  412. $this->pdo->expects($this->once())->method('realize')
  413. ->will($this->returnValue($o_stmt1));
  414. $o_stmt2 = $this->getMockForAbstractClass(
  415. 'Tox\\Data\\Pdo\\Statement',
  416. array($this->pdo, microtime())
  417. );
  418. $o_stmt2->execute()->bindParam($s_key, $s_val);
  419. }
  420. /**
  421. * @depends testLazyLoadingForBindValue
  422. * @expectedException Tox\Data\Pdo\ExecutedStatementException
  423. */
  424. public function testBindValueForbiddenAfterExecute()
  425. {
  426. $s_key = microtime();
  427. $s_val = microtime();
  428. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  429. $this->pdo->expects($this->once())->method('realize')
  430. ->will($this->returnValue($o_stmt1));
  431. $o_stmt2 = $this->getMockForAbstractClass(
  432. 'Tox\\Data\\Pdo\\Statement',
  433. array($this->pdo, microtime())
  434. );
  435. $o_stmt2->execute()->bindValue($s_key, $s_val);
  436. }
  437. public function testOmissibleExecuting()
  438. {
  439. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  440. $o_stmt1->expects($this->at(0))->method('execute');
  441. $o_stmt1->expects($this->at(1))->method('fetch');
  442. $this->pdo->expects($this->once())->method('realize')
  443. ->will($this->returnValue($o_stmt1));
  444. $o_stmt2 = $this->getMockForAbstractClass(
  445. 'Tox\\Data\\Pdo\\Statement',
  446. array($this->pdo, microtime())
  447. );
  448. $o_stmt2->fetch();
  449. }
  450. /**
  451. * @depends testOmissibleExecuting
  452. * @depends testLazyLoadingForBindValue
  453. * @depends testLazyLoadingForSetAttribute
  454. * @depends testLazyLoadingForSetFetchMode
  455. */
  456. public function testBindingsWouldBeAffectedOnFetching()
  457. {
  458. $s_key1 = microtime();
  459. $i_key2 = rand();
  460. $f_val1 = 1 + microtime(true);
  461. $f_val2 = 2 + microtime(true);
  462. $f_val3 = 3 + microtime(true);
  463. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  464. $o_stmt1->expects($this->once())->method('bindValue')
  465. ->with($this->equalTo($s_key1), $this->equalTo($f_val1));
  466. $o_stmt1->expects($this->once())->method('setAttribute')
  467. ->with($this->equalTo($i_key2), $this->equalTo($f_val2));
  468. $o_stmt1->expects($this->once())->method('setFetchMode')
  469. ->with($this->equalTo(intval($f_val3)));
  470. $o_stmt1->expects($this->at(3))->method('execute');
  471. $o_stmt1->expects($this->at(4))->method('fetch');
  472. $this->pdo->expects($this->once())->method('realize')
  473. ->will($this->returnValue($o_stmt1));
  474. $o_stmt2 = $this->getMockForAbstractClass(
  475. 'Tox\\Data\\Pdo\\Statement',
  476. array($this->pdo, microtime())
  477. );
  478. $o_stmt2->bindValue($s_key1, $f_val1)
  479. ->setAttribute($i_key2, $f_val2)->setFetchMode($f_val3)
  480. ->fetch();
  481. }
  482. /**
  483. * @depends testBindingsWouldBeAffectedOnFetching
  484. */
  485. public function testBindParamOppositeBindValue()
  486. {
  487. $s_key1 = microtime();
  488. $s_key2 = microtime();
  489. $f_val1 = 1 + microtime(true);
  490. $f_val2 = 2 + microtime(true);
  491. $f_val3 = 3 + microtime(true);
  492. $f_val4 = 4 + microtime(true);
  493. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  494. $o_stmt1->expects($this->at(0))->method('bindParam')
  495. ->with($this->equalTo($s_key1), $this->equalTo($f_val2));
  496. $o_stmt1->expects($this->at(1))->method('bindParam')
  497. ->with($this->equalTo($s_key2), $this->equalTo($f_val3));
  498. $o_stmt1->expects($this->at(2))->method('bindValue')
  499. ->with($this->equalTo($s_key2), $this->equalTo($f_val4));
  500. $this->pdo->expects($this->once())->method('realize')
  501. ->will($this->returnValue($o_stmt1));
  502. $o_stmt2 = $this->getMockForAbstractClass(
  503. 'Tox\\Data\\Pdo\\Statement',
  504. array($this->pdo, microtime())
  505. );
  506. $o_stmt2->bindValue($s_key1, $f_val1)->bindParam($s_key1, $f_val2)
  507. ->bindParam($s_key2, $f_val3)->bindValue($s_key2, $f_val4);
  508. }
  509. /**
  510. * @depends testLazyLoadingForExecute
  511. */
  512. public function testMultipleExecuteIgnored()
  513. {
  514. $m_ret = microtime();
  515. $a_pars = array(microtime());
  516. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  517. $o_stmt1->expects($this->once())->method('execute')
  518. ->with($this->equalTo($a_pars));
  519. $this->pdo->expects($this->once())->method('realize')
  520. ->will($this->returnValue($o_stmt1));
  521. $o_stmt2 = $this->getMockForAbstractClass(
  522. 'Tox\\Data\\Pdo\\Statement',
  523. array($this->pdo, microtime())
  524. );
  525. $this->assertSame($o_stmt2, $o_stmt2->execute($a_pars)->execute(array()));
  526. }
  527. /**
  528. * @depends testLazyLoadingForBindColumn
  529. * @expectedException Tox\Data\Pdo\ColumnBindingFailureException
  530. */
  531. public function testFailureOfBindColumn()
  532. {
  533. $i_key = rand();
  534. $s_val = microtime();
  535. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  536. $o_stmt1->expects($this->once())->method('bindColumn')
  537. ->will($this->throwException(new Exception));
  538. $this->pdo->expects($this->once())->method('realize')
  539. ->will($this->returnValue($o_stmt1));
  540. $o_stmt2 = $this->getMockForAbstractClass(
  541. 'Tox\\Data\\Pdo\\Statement',
  542. array($this->pdo, microtime())
  543. );
  544. $o_stmt2->bindColumn($i_key, $s_val);
  545. }
  546. /**
  547. * @depends testLazyLoadingForBindParam
  548. * @expectedException Tox\Data\Pdo\ParamBindingFailureException
  549. */
  550. public function testFailureOfBindParam()
  551. {
  552. $s_key = microtime();
  553. $s_val = microtime();
  554. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  555. $o_stmt1->expects($this->once())->method('bindParam')
  556. ->will($this->throwException(new Exception));
  557. $this->pdo->expects($this->once())->method('realize')
  558. ->will($this->returnValue($o_stmt1));
  559. $o_stmt2 = $this->getMockForAbstractClass(
  560. 'Tox\\Data\\Pdo\\Statement',
  561. array($this->pdo, microtime())
  562. );
  563. $o_stmt2->bindParam($s_key, $s_val);
  564. }
  565. /**
  566. * @depends testLazyLoadingForBindValue
  567. * @expectedException Tox\Data\Pdo\ValueBindingFailureException
  568. */
  569. public function testFailureOfBindValue()
  570. {
  571. $s_key = microtime();
  572. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  573. $o_stmt1->expects($this->once())->method('bindValue')
  574. ->will($this->throwException(new Exception));
  575. $this->pdo->expects($this->once())->method('realize')
  576. ->will($this->returnValue($o_stmt1));
  577. $o_stmt2 = $this->getMockForAbstractClass(
  578. 'Tox\\Data\\Pdo\\Statement',
  579. array($this->pdo, microtime())
  580. );
  581. $o_stmt2->bindParam($s_key, $s_key)->bindValue($s_key, microtime());
  582. }
  583. /**
  584. * @depends testLazyLoadingForCloseCursor
  585. * @expectedException Tox\Data\Pdo\CursorClosingFailureException
  586. */
  587. public function testFailureOfCloseCursor()
  588. {
  589. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  590. $o_stmt1->expects($this->once())->method('closeCursor')
  591. ->will($this->throwException(new Exception));
  592. $this->pdo->expects($this->once())->method('realize')
  593. ->will($this->returnValue($o_stmt1));
  594. $o_stmt2 = $this->getMockForAbstractClass(
  595. 'Tox\\Data\\Pdo\\Statement',
  596. array($this->pdo, microtime())
  597. );
  598. $o_stmt2->fetch();
  599. $o_stmt2->closeCursor();
  600. }
  601. /**
  602. * @depends testLazyLoadingForExecute
  603. * @expectedException Tox\Data\Pdo\ExecutingFailureException
  604. */
  605. public function testFailureOfExecute()
  606. {
  607. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  608. $o_stmt1->expects($this->once())->method('execute')
  609. ->will($this->throwException(new Exception));
  610. $this->pdo->expects($this->once())->method('realize')
  611. ->will($this->returnValue($o_stmt1));
  612. $o_stmt2 = $this->getMockForAbstractClass(
  613. 'Tox\\Data\\Pdo\\Statement',
  614. array($this->pdo, microtime())
  615. );
  616. $o_stmt2->execute();
  617. }
  618. /**
  619. * @depends testLazyLoadingForNextRowset
  620. * @expectedException Tox\Data\Pdo\RowsetIteratingFailureException
  621. */
  622. public function testFailureOfNextRowset()
  623. {
  624. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  625. $o_stmt1->expects($this->once())->method('nextRowset')
  626. ->will($this->throwException(new Exception));
  627. $this->pdo->expects($this->once())->method('realize')
  628. ->will($this->returnValue($o_stmt1));
  629. $o_stmt2 = $this->getMockForAbstractClass(
  630. 'Tox\\Data\\Pdo\\Statement',
  631. array($this->pdo, microtime())
  632. );
  633. $o_stmt2->nextRowset();
  634. }
  635. /**
  636. * @depends testLazyLoadingForSetAttribute
  637. * @expectedException Tox\Data\Pdo\AttributeSettingFailureException
  638. */
  639. public function testFailureOfSetAttribute()
  640. {
  641. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  642. $o_stmt1->expects($this->once())->method('setAttribute')
  643. ->will($this->throwException(new Exception));
  644. $this->pdo->expects($this->once())->method('realize')
  645. ->will($this->returnValue($o_stmt1));
  646. $o_stmt2 = $this->getMockForAbstractClass(
  647. 'Tox\\Data\\Pdo\\Statement',
  648. array($this->pdo, microtime())
  649. );
  650. $o_stmt2->execute()->setAttribute(rand(), microtime());
  651. }
  652. /**
  653. * @depends testLazyLoadingForSetFetchMode
  654. * @expectedException Tox\Data\Pdo\FetchModeSettingFailureException
  655. */
  656. public function testFailureOfSetFetchMode()
  657. {
  658. $o_stmt1 = $this->getMock('Tox\\Data\\IPdoStatement');
  659. $o_stmt1->expects($this->once())->method('setFetchMode')
  660. ->will($this->throwException(new Exception));
  661. $this->pdo->expects($this->once())->method('realize')
  662. ->will($this->returnValue($o_stmt1));
  663. $o_stmt2 = $this->getMockForAbstractClass(
  664. 'Tox\\Data\\Pdo\\Statement',
  665. array($this->pdo, microtime())
  666. );
  667. $o_stmt2->execute()->setFetchMode(rand(), microtime());
  668. }
  669. public function testCount()
  670. {
  671. $i_cnt = rand(1, 10);
  672. $a_rows = array_fill(0, $i_cnt, array());
  673. $o_stmt = $this->getMock(
  674. 'Tox\\Data\\Pdo\\Statement',
  675. array('fetchAll'),
  676. array($this->pdo, microtime())
  677. );
  678. $o_stmt->expects($this->once())->method('fetchAll')
  679. ->will($this->returnValue($a_rows));
  680. $this->assertEquals($i_cnt, count($o_stmt));
  681. }
  682. /**
  683. * @depends testCount
  684. */
  685. public function testIteration()
  686. {
  687. $i_cnt = rand(1, 10);
  688. $a_rows = array();
  689. for ($ii = 0; $ii < $i_cnt; $ii++) {
  690. $a_rows[] = array(
  691. 'id' => microtime(),
  692. 'time' => microtime(true)
  693. );
  694. }
  695. $o_stmt = $this->getMock(
  696. 'Tox\\Data\\Pdo\\Statement',
  697. array('fetchAll'),
  698. array($this->pdo, microtime())
  699. );
  700. $o_stmt->expects($this->once())->method('fetchAll')
  701. ->will($this->returnValue($a_rows));
  702. foreach ($o_stmt as $ii => $jj) {
  703. $this->assertEquals($a_rows[$ii], $jj);
  704. }
  705. }
  706. public function testManufacturingPreparedStatement()
  707. {
  708. $s_val = microtime();
  709. $o_stmt = $this->getMock(
  710. 'Tox\\Data\\Pdo\\Statement',
  711. array('newPrepare', 'newQuery'),
  712. array($this->pdo, microtime())
  713. );
  714. $o_stmt->staticExpects($this->once())->method('newPrepare')
  715. ->will($this->returnValue($s_val));
  716. $this->assertEquals($s_val, $o_stmt::manufacture($this->pdo, Statement::TYPE_PREPARE, microtime()));
  717. }
  718. public function testManufacturingQueryStatement()
  719. {
  720. $s_val = microtime();
  721. $o_stmt = $this->getMock(
  722. 'Tox\\Data\\Pdo\\Statement',
  723. array('newPrepare', 'newQuery'),
  724. array($this->pdo, microtime())
  725. );
  726. $o_stmt->staticExpects($this->once())->method('newQuery')
  727. ->will($this->returnValue($s_val));
  728. $this->assertEquals($s_val, $o_stmt::manufacture($this->pdo, Statement::TYPE_QUERY, microtime()));
  729. }
  730. }
  731. // vi:ft=php fenc=utf-8 ff=unix ts=4 sts=4 et sw=4 fen fdm=indent fdl=1 tw=120