[limb-svn] r6696 - in 3.x/trunk/limb/dbal: src/drivers tests/cases/non-driver
svn at limb-project.com
svn at limb-project.com
Fri Jan 18 10:54:57 MSK 2008
Author: serega
Date: 2008-01-18 10:54:57 +0300 (Fri, 18 Jan 2008)
New Revision: 6696
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6696
Added:
3.x/trunk/limb/dbal/src/drivers/lmbAuditDbConnection.class.php
3.x/trunk/limb/dbal/tests/cases/non-driver/lmbAuditDbConnectionTest.class.php
Log:
-- added lmbAuditDbConnection wrapper for db connection that will be used in tests to ensure what certain sql queries are performed
Added: 3.x/trunk/limb/dbal/src/drivers/lmbAuditDbConnection.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/drivers/lmbAuditDbConnection.class.php (rev 0)
+++ 3.x/trunk/limb/dbal/src/drivers/lmbAuditDbConnection.class.php 2008-01-18 07:54:57 UTC (rev 6696)
@@ -0,0 +1,55 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+lmb_require('limb/dbal/src/drivers/lmbDbConnection.interface.php');
+lmb_require('limb/core/src/lmbDecorator.class.php');
+
+lmbDecorator :: generate('lmbDbConnection', 'lmbDbConnectionDecorator');
+
+/**
+ * class lmbAuditDbConnection.
+ * Uses in tests to ensure performed sql queries.
+ * @package dbal
+ * @version $Id$
+ */
+class lmbAuditDbConnection extends lmbDbConnectionDecorator
+{
+ protected $queries = null;
+
+ function execute($sql)
+ {
+ $this->queries[] = $sql;
+ return parent :: execute($sql);
+ }
+
+ function count()
+ {
+ return sizeof($this->queries);
+ }
+
+ function reset()
+ {
+ $this->queries = array();
+ }
+
+ function getQueries($reg_exp = '')
+ {
+ if(!$reg_exp)
+ return $this->queries;
+
+ $res = array();
+ foreach($this->queries as $query)
+ {
+ if(preg_match('/' . $reg_exp . '/i', $query))
+ $res[] = $query;
+ }
+ return $res;
+ }
+}
+
+
Added: 3.x/trunk/limb/dbal/tests/cases/non-driver/lmbAuditDbConnectionTest.class.php
===================================================================
--- 3.x/trunk/limb/dbal/tests/cases/non-driver/lmbAuditDbConnectionTest.class.php (rev 0)
+++ 3.x/trunk/limb/dbal/tests/cases/non-driver/lmbAuditDbConnectionTest.class.php 2008-01-18 07:54:57 UTC (rev 6696)
@@ -0,0 +1,59 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+lmb_require('limb/dbal/src/drivers/lmbAuditDbConnection.class.php');
+lmb_require('limb/dbal/src/drivers/lmbDbConnection.interface.php');
+
+Mock :: generate('lmbDbConnection', 'MockDbConnection');
+
+class lmbAuditDbConnectionTest extends UnitTestCase
+{
+ protected $wrapped;
+ protected $connection;
+
+ function setUp()
+ {
+ $this->wrapped = new MockDbConnection();
+ $this->connection = new lmbAuditDbConnection($this->wrapped);
+ }
+
+ function testExecuteIncreasesQueryCounter()
+ {
+ $sql = 'Some sql query';
+ $this->wrapped->expectOnce('execute', array($sql));
+ $this->connection->execute($sql);
+
+ $this->assertEqual($this->connection->count(), 1);
+ }
+
+ function testResetQueryCounter()
+ {
+ $sql = 'Some sql query';
+ $this->connection->execute($sql);
+ $this->connection->execute($sql);
+
+ $this->assertEqual($this->connection->count(), 2);
+
+ $this->connection->reset();
+
+ $this->assertEqual($this->connection->count(), 0);
+ }
+
+ function testGetQueries()
+ {
+ $sql1 = 'SELECT program.* FROM program';
+ $sql2 = 'select program.* FROM program';
+ $sql3 = 'select course.* FROM course';
+ $this->connection->execute($sql1);
+ $this->connection->execute($sql2);
+ $this->connection->execute($sql3);
+
+ $this->assertEqual(count($this->connection->getQueries('select program.*')), 2);
+ }
+}
+
More information about the limb-svn
mailing list