ProfilerTestCase.php 8.01 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<?php
/*
 *  $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>.
 */

/**
 * Doctrine_Connection_Profiler_TestCase
 *
 * @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$
 */
class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase 
{
    public function prepareTables()
    {}
    public function prepareData() 
    {}
40 41
    public function setUp() 
    {}
42

zYne's avatar
zYne committed
43 44 45 46 47 48 49 50 51 52 53 54
    public function testQuery() 
    {
        $this->conn = Doctrine_Manager::getInstance()->openConnection(array('sqlite::memory:'));

        $this->profiler = new Doctrine_Connection_Profiler();

        $this->conn->setListener($this->profiler);

        $this->conn->exec('CREATE TABLE test (id INT)');
        
        $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'CREATE TABLE test (id INT)');
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
55
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::CONN_EXEC);
zYne's avatar
zYne committed
56 57 58 59
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
        
        $this->assertEqual($this->conn->count(), 1);
    }
60

61
    public function testPrepareAndExecute()
zYne's avatar
zYne committed
62 63 64 65 66 67 68
    {

        $stmt  = $this->conn->prepare('INSERT INTO test (id) VALUES (?)');
        $event = $this->profiler->lastEvent();

        $this->assertEqual($event->getQuery(), 'INSERT INTO test (id) VALUES (?)');
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
69
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::CONN_PREPARE);
zYne's avatar
zYne committed
70 71 72 73 74 75
        $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());
76
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::STMT_EXECUTE);
zYne's avatar
zYne committed
77 78 79 80
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));

        $this->assertEqual($this->conn->count(), 2);
    }
81

82
    public function testMultiplePrepareAndExecute()
zYne's avatar
zYne committed
83 84 85 86 87
    {

        $stmt = $this->conn->prepare('INSERT INTO test (id) VALUES (?)');
        $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
88
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::CONN_PREPARE);
zYne's avatar
zYne committed
89 90 91 92 93
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));

        $stmt2 = $this->conn->prepare('INSERT INTO test (id) VALUES (?)');
        $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
94
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::CONN_PREPARE);
zYne's avatar
zYne committed
95
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
96
         /** TODO: strange errors here
zYne's avatar
zYne committed
97 98 99 100 101
        $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());
102
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::EXECUTE);
zYne's avatar
zYne committed
103 104 105
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));

        $this->assertEqual($this->conn->count(), 4);
106
        */
zYne's avatar
zYne committed
107
    }
108
    
zYne's avatar
zYne committed
109 110 111 112 113 114 115 116 117 118 119 120 121
    public function testExecuteStatementMultipleTimes() 
    {
        try {
            $stmt = $this->conn->prepare('INSERT INTO test (id) VALUES (?)');
            $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());
122
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::STMT_EXECUTE);
zYne's avatar
zYne committed
123 124 125 126
        $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());
127
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::STMT_EXECUTE);
zYne's avatar
zYne committed
128 129
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
    }
130

zYne's avatar
zYne committed
131 132 133 134 135 136 137 138 139 140
    public function testTransactionRollback() 
    {
        try {
            $this->conn->beginTransaction();
            $this->pass();
        } catch(Doctrine_Db_Exception $e) {
            $this->fail($e->__toString());
        }
        $this->assertEqual($this->profiler->lastEvent()->getQuery(), null);
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
141
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::TX_BEGIN);
zYne's avatar
zYne committed
142 143 144 145 146 147 148 149 150 151 152
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
        
        try {
            $this->conn->rollback();
            $this->pass();
        } catch(Doctrine_Db_Exception $e) {
            $this->fail($e->__toString());
        }

        $this->assertEqual($this->profiler->lastEvent()->getQuery(), null);
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
153
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::TX_ROLLBACK);
zYne's avatar
zYne committed
154 155
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
    }
156

zYne's avatar
zYne committed
157 158 159 160 161 162 163 164 165 166
    public function testTransactionCommit() 
    {
        try {
            $this->conn->beginTransaction();
            $this->pass();
        } catch(Doctrine_Db_Exception $e) {
            $this->fail($e->__toString());
        }
        $this->assertEqual($this->profiler->lastEvent()->getQuery(), null);
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
167
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::TX_BEGIN);
zYne's avatar
zYne committed
168 169 170 171 172 173 174 175 176 177 178 179
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
        
        try {
            $this->conn->commit();
            $this->pass();
        } catch(Doctrine_Db_Exception $e) {
            $this->fail($e->__toString());
            $this->conn->rollback();
        }

        $this->assertEqual($this->profiler->lastEvent()->getQuery(), null);
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
180
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::TX_COMMIT);
zYne's avatar
zYne committed
181 182 183
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
    }
}