| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 lmb_require('limb/dbal/src/filter/lmbAutoDbTransactionFilter.class.php'); 10 lmb_require('limb/filter_chain/src/lmbFilterChain.class.php'); 11 lmb_require('limb/dbal/src/lmbSimpleDb.class.php'); 12 13 Mock :: generate('lmbFilterChain', 'MockFilterChain'); 14 15 class FilterWorkingWithDbStub 16 { 17 var $sql; 18 var $exception; 19 20 function run($chain) 21 { 22 if($this->sql) 23 { 24 $stmt = lmbToolkit :: instance()->getDefaultDbConnection()->newStatement($this->sql); 25 $stmt->execute(); 26 } 27 28 if($this->exception) 29 throw $this->exception; 30 } 31 } 32 33 class lmbAutoDbTransactionFilterTest extends UnitTestCase 34 { 35 var $toolkit; 36 var $db; 37 38 function setUp() 39 { 40 $this->toolkit = lmbToolkit :: save(); 41 $this->conn = $this->toolkit->getDefaultDbConnection(); 42 $this->db = new lmbSimpleDb($this->conn); 43 $this->db->delete('test_db_table'); 44 } 45 46 function tearDown() 47 { 48 $this->db->delete('test_db_table'); 49 lmbToolkit :: restore(); 50 } 51 52 function testOldConnectionIsRestored() 53 { 54 $this->assertFalse($this->conn instanceof lmbAutoTransactionConnection); 55 56 $filter = new lmbAutoDbTransactionFilter(); 57 $chain = new MockFilterChain(); 58 $chain->expectOnce('next'); 59 $filter->run($chain); 60 61 $this->assertIdentical($this->conn, $this->toolkit->getDefaultDbConnection()); 62 } 63 64 function testAutoCommitTransaction() 65 { 66 $stub = new FilterWorkingWithDbStub(); 67 $stub->sql = "INSERT INTO test_db_table (title) VALUES ('hey')"; 68 69 $this->assertEqual($this->db->count('test_db_table'), 0); 70 71 $chain = new lmbFilterChain(); 72 $chain->registerFilter(new lmbAutoDbTransactionFilter()); 73 $chain->registerFilter($stub); 74 $chain->process(); 75 76 $this->conn->rollbackTransaction(); 77 78 $this->assertEqual($this->db->count('test_db_table'), 1); 79 $this->assertIdentical($this->conn, $this->toolkit->getDefaultDbConnection()); 80 } 81 82 function testRollBackOnException() 83 { 84 $stub = new FilterWorkingWithDbStub(); 85 $stub->sql = "INSERT INTO test_db_table (title) VALUES ('hey')"; 86 $stub->exception = new Exception('foo'); 87 88 $this->assertEqual($this->db->count('test_db_table'), 0); 89 90 $chain = new lmbFilterChain(); 91 $chain->registerFilter(new lmbAutoDbTransactionFilter()); 92 $chain->registerFilter($stub); 93 94 try 95 { 96 $chain->process(); 97 $this->assertTrue(false); 98 } 99 catch(Exception $e){} 100 101 $this->assertEqual($this->db->count('test_db_table'), 0); 102 $this->assertIdentical($this->conn, $this->toolkit->getDefaultDbConnection()); 103 } 104 105 } 106 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Aug 30 04:38:32 2008 | Cross-referenced by PHPXref 0.7 |