1
0
mirror of synced 2024-12-13 14:56:01 +03:00
doctrine2/tests/Db/ProfilerTestCase.php

177 lines
7.9 KiB
PHP
Raw Normal View History

2006-12-28 00:27:25 +03:00
<?php
2007-02-05 15:06:41 +03:00
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
2007-06-20 02:26:41 +04:00
* Doctrine_Connection_Profiler_TestCase
2007-02-05 15:06:41 +03:00
*
* @package Doctrine
* @subpackage Doctrine_Db
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
2007-06-20 02:26:41 +04:00
class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase
2007-02-05 15:06:41 +03:00
{
2007-06-20 02:26:41 +04:00
public function prepareTables()
2007-02-05 15:06:41 +03:00
{}
public function prepareData()
{}
2006-12-28 00:27:25 +03:00
2007-02-05 15:06:41 +03:00
public function testQuery()
{
2007-06-20 02:26:41 +04:00
$this->conn = Doctrine_Manager::getInstance()->openConnection(array('sqlite::memory:'));
2006-12-28 00:27:25 +03:00
2007-06-20 02:26:41 +04:00
$this->profiler = new Doctrine_Connection_Profiler();
2006-12-28 00:27:25 +03:00
2007-06-20 02:26:41 +04:00
$this->conn->setListener($this->profiler);
2006-12-28 00:27:25 +03:00
2007-06-20 02:26:41 +04:00
$this->conn->exec('CREATE TABLE test (id INT)');
2006-12-28 00:27:25 +03:00
$this->assertEqual($this->profiler->lastEvent()->getQuery(), 'CREATE TABLE test (id INT)');
$this->assertTrue($this->profiler->lastEvent()->hasEnded());
2007-06-20 02:26:41 +04:00
$this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXEC);
2006-12-28 00:27:25 +03:00
$this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
2007-06-20 02:26:41 +04:00
$this->assertEqual($this->conn->count(), 1);
2006-12-28 00:27:25 +03:00
}
2007-02-05 15:06:41 +03:00
public function testPrepareAndExecute()
{
2006-12-28 00:27:25 +03:00
2007-06-20 02:26:41 +04:00
$stmt = $this->conn->prepare('INSERT INTO test (id) VALUES (?)');
2006-12-28 00:27:25 +03:00
$event = $this->profiler->lastEvent();
$this->assertEqual($event->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertTrue($this->profiler->lastEvent()->hasEnded());
2007-01-21 21:31:51 +03:00
$this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::PREPARE);
2006-12-28 00:27:25 +03:00
$this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
$stmt->execute(array(1));
$this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertTrue($this->profiler->lastEvent()->hasEnded());
2007-01-21 21:31:51 +03:00
$this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE);
2006-12-28 00:27:25 +03:00
$this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
2007-06-20 02:26:41 +04:00
$this->assertEqual($this->conn->count(), 2);
2006-12-28 00:27:25 +03:00
}
2007-02-05 15:06:41 +03:00
public function testMultiplePrepareAndExecute()
{
2006-12-28 00:27:25 +03:00
2007-06-20 02:26:41 +04:00
$stmt = $this->conn->prepare('INSERT INTO test (id) VALUES (?)');
2006-12-28 00:27:25 +03:00
$this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertTrue($this->profiler->lastEvent()->hasEnded());
2007-01-21 21:31:51 +03:00
$this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::PREPARE);
2006-12-28 00:27:25 +03:00
$this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
2007-06-20 02:26:41 +04:00
$stmt2 = $this->conn->prepare('INSERT INTO test (id) VALUES (?)');
2006-12-28 00:27:25 +03:00
$this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertTrue($this->profiler->lastEvent()->hasEnded());
2007-01-21 21:31:51 +03:00
$this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::PREPARE);
2006-12-28 00:27:25 +03:00
$this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
$stmt->execute(array(1));
$stmt2->execute(array(1));
$this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertTrue($this->profiler->lastEvent()->hasEnded());
2007-01-21 21:31:51 +03:00
$this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE);
2006-12-28 00:27:25 +03:00
$this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
2007-06-20 02:26:41 +04:00
$this->assertEqual($this->conn->count(), 4);
2006-12-28 00:27:25 +03:00
}
public function testExecuteStatementMultipleTimes()
2007-02-05 15:06:41 +03:00
{
2006-12-28 00:27:25 +03:00
try {
2007-06-20 02:26:41 +04:00
$stmt = $this->conn->prepare('INSERT INTO test (id) VALUES (?)');
2006-12-28 00:27:25 +03:00
$stmt->execute(array(1));
$stmt->execute(array(1));
$this->pass();
} catch(Doctrine_Db_Exception $e) {
$this->fail($e->__toString());
}
$this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertTrue($this->profiler->lastEvent()->hasEnded());
2007-01-21 21:31:51 +03:00
$this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE);
2006-12-28 00:27:25 +03:00
$this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
$this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
$this->assertTrue($this->profiler->lastEvent()->hasEnded());
2007-01-21 21:31:51 +03:00
$this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE);
2006-12-28 00:27:25 +03:00
$this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
}
2007-02-05 15:06:41 +03:00
public function testTransactionRollback()
{
2006-12-28 00:27:25 +03:00
try {
2007-06-20 02:26:41 +04:00
$this->conn->beginTransaction();
2006-12-28 00:27:25 +03:00
$this->pass();
} catch(Doctrine_Db_Exception $e) {
$this->fail($e->__toString());
}
$this->assertEqual($this->profiler->lastEvent()->getQuery(), null);
$this->assertTrue($this->profiler->lastEvent()->hasEnded());
2007-01-21 21:31:51 +03:00
$this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::BEGIN);
2006-12-28 00:27:25 +03:00
$this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
try {
2007-06-20 02:26:41 +04:00
$this->conn->rollback();
2006-12-28 00:27:25 +03:00
$this->pass();
} catch(Doctrine_Db_Exception $e) {
$this->fail($e->__toString());
}
$this->assertEqual($this->profiler->lastEvent()->getQuery(), null);
$this->assertTrue($this->profiler->lastEvent()->hasEnded());
2007-01-21 21:31:51 +03:00
$this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::ROLLBACK);
2006-12-28 00:27:25 +03:00
$this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
}
2007-02-05 15:06:41 +03:00
public function testTransactionCommit()
{
2006-12-28 00:27:25 +03:00
try {
2007-06-20 02:26:41 +04:00
$this->conn->beginTransaction();
2006-12-28 00:27:25 +03:00
$this->pass();
} catch(Doctrine_Db_Exception $e) {
$this->fail($e->__toString());
}
$this->assertEqual($this->profiler->lastEvent()->getQuery(), null);
$this->assertTrue($this->profiler->lastEvent()->hasEnded());
2007-01-21 21:31:51 +03:00
$this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::BEGIN);
2006-12-28 00:27:25 +03:00
$this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
try {
2007-06-20 02:26:41 +04:00
$this->conn->commit();
2006-12-28 00:27:25 +03:00
$this->pass();
} catch(Doctrine_Db_Exception $e) {
$this->fail($e->__toString());
2007-06-20 02:26:41 +04:00
$this->conn->rollback();
2006-12-28 00:27:25 +03:00
}
$this->assertEqual($this->profiler->lastEvent()->getQuery(), null);
$this->assertTrue($this->profiler->lastEvent()->hasEnded());
2007-01-21 21:31:51 +03:00
$this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::COMMIT);
2006-12-28 00:27:25 +03:00
$this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
}
}