| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Db
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * @see Zend_Db_TestSetup
- */
- require_once 'Zend/Db/TestSetup.php';
- /**
- * @category Zend
- * @package Zend_Db
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Db
- * @group Zend_Db_Profiler
- */
- abstract class Zend_Db_Profiler_TestCommon extends Zend_Db_TestSetup
- {
- public function setUp()
- {
- parent::setUp();
- $this->_db->getProfiler()->setEnabled(true);
- }
- public function tearDown()
- {
- if($this->_db instanceof Zend_Db_Adapter_Abstract) {
- $this->_db->getProfiler()->setEnabled(false);
- }
- parent::tearDown();
- }
- public function testProfilerPreparedStatement()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- // prepare a query
- $select = $this->_db->select()
- ->from('zfbugs')
- ->where("$bug_id = 2");
- $stmt = $this->_db->prepare($select->__toString());
- // execute query a first time
- $stmt->execute();
- $results = $stmt->fetchAll();
- $this->assertTrue(is_array($results));
- $this->assertEquals(2, $results[0]['bug_id']);
- // analyze query profiles
- $profiles = $this->_db->getProfiler()->getQueryProfiles();
- $this->assertTrue(is_array($profiles));
- $this->assertEquals(1, count($profiles), 'Expected to find 1 profile');
- $qp = $profiles[0];
- $this->assertTrue($qp instanceof Zend_Db_Profiler_Query);
- // execute query a second time
- $stmt->execute();
- $results = $stmt->fetchAll();
- $this->assertTrue(is_array($results));
- $this->assertEquals(2, $results[0]['bug_id']);
- // analyze query profiles
- $profiles = $this->_db->getProfiler()->getQueryProfiles();
- $this->assertTrue(is_array($profiles));
- $this->assertEquals(2, count($profiles), 'Expected to find 2 profiles');
- $qp = $profiles[1];
- $this->assertTrue($qp instanceof Zend_Db_Profiler_Query);
- $this->assertNotSame($profiles[0], $profiles[1]);
- $this->assertEquals($profiles[0]->getQuery(), $profiles[1]->getQuery());
- }
- public function testProfilerPreparedStatementWithParams()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $bug_status = $this->_db->quoteIdentifier('bug_status', true);
- // prepare a query
- $select = $this->_db->select()
- ->from('zfbugs')
- ->where("$bug_id = ? AND $bug_status = ?");
- $stmt = $this->_db->prepare($select->__toString());
- // execute query a first time
- $stmt->execute(array(2, 'VERIFIED'));
- $results = $stmt->fetchAll();
- $this->assertTrue(is_array($results));
- $this->assertEquals(2, $results[0]['bug_id']);
- // analyze query profiles
- $profiles = $this->_db->getProfiler()->getQueryProfiles();
- $this->assertTrue(is_array($profiles));
- $this->assertEquals(1, count($profiles), 'Expected to find 1 profile');
- $qp = $profiles[0];
- $this->assertTrue($qp instanceof Zend_Db_Profiler_Query);
- // analyze query in the profile
- $sql = $qp->getQuery();
- $this->assertContains(" = ?", $sql);
- $params = $qp->getQueryParams();
- $this->assertTrue(is_array($params));
- $this->assertEquals(array(1 => 2, 2 => 'VERIFIED'), $params);
- // execute query a second time
- $stmt->execute(array(3, 'FIXED'));
- $results = $stmt->fetchAll();
- $this->assertTrue(is_array($results));
- $this->assertEquals(3, $results[0]['bug_id']);
- // analyze query profiles
- $profiles = $this->_db->getProfiler()->getQueryProfiles();
- $this->assertTrue(is_array($profiles));
- $this->assertEquals(2, count($profiles), 'Expected to find 2 profiles');
- $qp = $profiles[1];
- $this->assertTrue($qp instanceof Zend_Db_Profiler_Query);
- $this->assertNotSame($profiles[0], $profiles[1]);
- $this->assertEquals($profiles[0]->getQuery(), $profiles[1]->getQuery());
- // analyze query in the profile
- $sql = $qp->getQuery();
- $this->assertContains(" = ?", $sql);
- $params = $qp->getQueryParams();
- $this->assertTrue(is_array($params));
- $this->assertEquals(array(1 => 3, 2 => 'FIXED'), $params);
- $this->assertNotSame($profiles[0], $profiles[1]);
- $this->assertEquals($profiles[0]->getQuery(), $profiles[1]->getQuery());
- }
- public function testProfilerPreparedStatementWithBoundParams()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $bug_status = $this->_db->quoteIdentifier('bug_status', true);
- // prepare a query
- $select = $this->_db->select()
- ->from('zfbugs')
- ->where("$bug_id = ? AND $bug_status = ?");
- $stmt = $this->_db->prepare($select->__toString());
- // execute query a first time
- $id = 1;
- $status = 'NEW';
- $this->assertTrue($stmt->bindParam(1, $id));
- $this->assertTrue($stmt->bindParam(2, $status));
- $id = 2;
- $status = 'VERIFIED';
- $stmt->execute();
- $results = $stmt->fetchAll();
- $this->assertTrue(is_array($results));
- $this->assertEquals(2, $results[0]['bug_id']);
- // analyze query profiles
- $profiles = $this->_db->getProfiler()->getQueryProfiles();
- $this->assertTrue(is_array($profiles), 'Expected array, got '.gettype($profiles));
- $this->assertEquals(1, count($profiles), 'Expected to find 1 profile');
- $qp = $profiles[0];
- $this->assertTrue($qp instanceof Zend_Db_Profiler_Query);
- // analyze query in the profile
- $sql = $qp->getQuery();
- $this->assertContains(" = ?", $sql);
- $params = $qp->getQueryParams();
- $this->assertTrue(is_array($params));
- $this->assertEquals(array(1 => 2, 2 => 'VERIFIED'), $params);
- // execute query a second time
- $id = 3;
- $status = 'FIXED';
- $stmt->execute();
- $results = $stmt->fetchAll();
- $this->assertTrue(is_array($results));
- $this->assertEquals(3, $results[0]['bug_id']);
- // analyze query profiles
- $profiles = $this->_db->getProfiler()->getQueryProfiles();
- $this->assertTrue(is_array($profiles), 'Expected array, got '.gettype($profiles));
- $this->assertEquals(2, count($profiles), 'Expected to find 2 profiles');
- $qp = $profiles[1];
- $this->assertTrue($qp instanceof Zend_Db_Profiler_Query);
- // analyze query in the profile
- $sql = $qp->getQuery();
- $this->assertContains(" = ?", $sql);
- $params = $qp->getQueryParams();
- $this->assertTrue(is_array($params));
- $this->assertEquals(array(1 => 3, 2 => 'FIXED'), $params);
- }
- /**
- * Ensures that setFilterQueryType() actually filters
- *
- * @return void
- */
- protected function _testProfilerSetFilterQueryTypeCommon($queryType)
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs', true);
- $bug_status = $this->_db->quoteIdentifier('bug_status', true);
- $prof = $this->_db->getProfiler();
- $prof->setEnabled(true);
- $this->assertSame($prof->setFilterQueryType($queryType), $prof);
- $this->assertEquals($queryType, $prof->getFilterQueryType());
- $this->_db->query("SELECT * FROM $bugs");
- $this->_db->query("INSERT INTO $bugs ($bug_status) VALUES (?)", array('NEW'));
- $this->_db->query("DELETE FROM $bugs");
- $this->_db->query("UPDATE $bugs SET $bug_status = ?", array('FIXED'));
- $qps = $prof->getQueryProfiles();
- $this->assertTrue(is_array($qps), 'Expecting some query profiles, got none');
- foreach ($qps as $qp) {
- $qtype = $qp->getQueryType();
- $this->assertEquals($queryType, $qtype,
- "Found query type $qtype, which should have been filtered out");
- }
- $prof->setEnabled(false);
- }
- public function testProfilerSetFilterQueryTypeInsert()
- {
- $this->_testProfilerSetFilterQueryTypeCommon(Zend_Db_Profiler::INSERT);
- }
- public function testProfilerSetFilterQueryTypeUpdate()
- {
- $this->_testProfilerSetFilterQueryTypeCommon(Zend_Db_Profiler::UPDATE);
- }
- public function testProfilerSetFilterQueryTypeDelete()
- {
- $this->_testProfilerSetFilterQueryTypeCommon(Zend_Db_Profiler::DELETE);
- }
- public function testProfilerSetFilterQueryTypeSelect()
- {
- $this->_testProfilerSetFilterQueryTypeCommon(Zend_Db_Profiler::SELECT);
- }
- }
|