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
/*
 *  $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>.
 */

/**
zYne's avatar
zYne committed
23
 * Doctrine_Connection_Profiler_TestCase
zYne's avatar
zYne committed
24 25 26 27 28 29 30 31 32 33
 *
 * @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$
 */
zYne's avatar
zYne committed
34
class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase 
zYne's avatar
zYne committed
35
{
zYne's avatar
zYne committed
36
    public function prepareTables()
zYne's avatar
zYne committed
37 38 39
    {}
    public function prepareData() 
    {}
zYne's avatar
zYne committed
40
    
zYne's avatar
zYne committed
41 42
    public function testQuery() 
    {
zYne's avatar
zYne committed
43
        $this->conn = Doctrine_Manager::getInstance()->openConnection(array('sqlite::memory:'));
zYne's avatar
zYne committed
44

zYne's avatar
zYne committed
45
        $this->profiler = new Doctrine_Connection_Profiler();
zYne's avatar
zYne committed
46

zYne's avatar
zYne committed
47
        $this->conn->setListener($this->profiler);
zYne's avatar
zYne committed
48

zYne's avatar
zYne committed
49
        $this->conn->exec('CREATE TABLE test (id INT)');
zYne's avatar
zYne committed
50 51 52
        
        $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'CREATE TABLE test (id INT)');
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
zYne's avatar
zYne committed
53
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXEC);
zYne's avatar
zYne committed
54 55
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));
        
zYne's avatar
zYne committed
56
        $this->assertEqual($this->conn->count(), 1);
zYne's avatar
zYne committed
57
    }
zYne's avatar
zYne committed
58 59
    public function testPrepareAndExecute() 
    {
zYne's avatar
zYne committed
60

zYne's avatar
zYne committed
61
        $stmt  = $this->conn->prepare('INSERT INTO test (id) VALUES (?)');
zYne's avatar
zYne committed
62 63 64 65
        $event = $this->profiler->lastEvent();

        $this->assertEqual($event->getQuery(), 'INSERT INTO test (id) VALUES (?)');
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
zYne's avatar
zYne committed
66
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::PREPARE);
zYne's avatar
zYne committed
67 68 69 70 71 72
        $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
73
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE);
zYne's avatar
zYne committed
74 75
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));

zYne's avatar
zYne committed
76
        $this->assertEqual($this->conn->count(), 2);
zYne's avatar
zYne committed
77
    }
zYne's avatar
zYne committed
78 79
    public function testMultiplePrepareAndExecute() 
    {
zYne's avatar
zYne committed
80

zYne's avatar
zYne committed
81
        $stmt = $this->conn->prepare('INSERT INTO test (id) VALUES (?)');
zYne's avatar
zYne committed
82 83
        $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
zYne's avatar
zYne committed
84
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::PREPARE);
zYne's avatar
zYne committed
85 86
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));

zYne's avatar
zYne committed
87
        $stmt2 = $this->conn->prepare('INSERT INTO test (id) VALUES (?)');
zYne's avatar
zYne committed
88 89
        $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)');
        $this->assertTrue($this->profiler->lastEvent()->hasEnded());
zYne's avatar
zYne committed
90
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::PREPARE);
zYne's avatar
zYne committed
91 92 93 94 95 96 97
        $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
98
        $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE);
zYne's avatar
zYne committed
99 100
        $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs()));

zYne's avatar
zYne committed
101
        $this->assertEqual($this->conn->count(), 4);
102

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

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