ProfilerTestCase.php 7.9 KB
Newer Older
zYne's avatar
zYne committed
1
<?php
zYne's avatar
zYne committed
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
/*
 *  $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_Db_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_Db_Profiler_TestCase extends Doctrine_UnitTestCase 
{
zYne's avatar
zYne committed
36 37 38
    protected $dbh;
    
    protected $profiler;
zYne's avatar
zYne committed
39 40 41 42
    public function prepareTables() 
    {}
    public function prepareData() 
    {}
zYne's avatar
zYne committed
43
    
zYne's avatar
zYne committed
44 45
    public function testQuery() 
    {
zYne's avatar
zYne committed
46
        $this->dbh = Doctrine_Db::getConnection('sqlite::memory:');
zYne's avatar
zYne committed
47 48 49 50 51 52 53 54 55

        $this->profiler = new Doctrine_Db_Profiler();

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

        $this->dbh->query('CREATE TABLE test (id INT)');
        
        $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'CREATE TABLE test (id INT)');
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
zYne's avatar
zYne committed
56
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::QUERY);
zYne's avatar
zYne committed
57 58 59 60
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
        
        $this->assertEqual($this->dbh->count(), 1);
    }
zYne's avatar
zYne committed
61 62
    public function testPrepareAndExecute() 
    {
zYne's avatar
zYne committed
63 64 65 66 67 68

        $stmt  = $this->dbh->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());
zYne's avatar
zYne committed
69
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::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());
zYne's avatar
zYne committed
76
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE);
zYne's avatar
zYne committed
77 78 79 80
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));

        $this->assertEqual($this->dbh->count(), 2);
    }
zYne's avatar
zYne committed
81 82
    public function testMultiplePrepareAndExecute() 
    {
zYne's avatar
zYne committed
83 84 85 86

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

        $stmt2 = $this->dbh->prepare('INSERT INTO test (id) VALUES (?)');
        $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
zYne's avatar
zYne committed
93
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::PREPARE);
zYne's avatar
zYne committed
94 95 96 97 98 99 100
        $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());
zYne's avatar
zYne committed
101
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE);
zYne's avatar
zYne committed
102 103 104 105
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));

        $this->assertEqual($this->dbh->count(), 4);
    }
zYne's avatar
zYne committed
106 107
    public function testExecuteStatementMultipleTimes() 
    {
zYne's avatar
zYne committed
108 109 110 111 112 113 114 115 116 117 118
        try {
            $stmt = $this->dbh->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());
zYne's avatar
zYne committed
119
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE);
zYne's avatar
zYne committed
120 121 122 123
        $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());
zYne's avatar
zYne committed
124
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE);
zYne's avatar
zYne committed
125 126
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
    }
zYne's avatar
zYne committed
127 128
    public function testTransactionRollback() 
    {
zYne's avatar
zYne committed
129 130 131 132 133 134 135 136
        try {
            $this->dbh->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());
zYne's avatar
zYne committed
137
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::BEGIN);
zYne's avatar
zYne committed
138 139 140 141 142 143 144 145 146 147 148
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
        
        try {
            $this->dbh->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());
zYne's avatar
zYne committed
149
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::ROLLBACK);
zYne's avatar
zYne committed
150 151
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
    }
zYne's avatar
zYne committed
152 153
    public function testTransactionCommit() 
    {
zYne's avatar
zYne committed
154 155 156 157 158 159 160 161
        try {
            $this->dbh->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());
zYne's avatar
zYne committed
162
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::BEGIN);
zYne's avatar
zYne committed
163 164 165 166 167 168 169 170 171 172 173 174
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
        
        try {
            $this->dbh->commit();
            $this->pass();
        } catch(Doctrine_Db_Exception $e) {
            $this->fail($e->__toString());
            $this->dbh->rollback();
        }

        $this->assertEqual($this->profiler->lastEvent()->getQuery(), null);
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
zYne's avatar
zYne committed
175
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::COMMIT);
zYne's avatar
zYne committed
176 177 178 179
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
    }
}
?>