forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbProfilerTest.php
165 lines (120 loc) · 4.55 KB
/
DbProfilerTest.php
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| 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@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
+------------------------------------------------------------------------+
*/
class DbProfiler extends Phalcon\Db\Profiler
{
private $_points = 0;
public function beforeStartProfile($profile)
{
$this->_points++;
}
public function afterEndProfile($profile)
{
$this->_points--;
}
public function getPoints()
{
return $this->_points;
}
}
class DbProfilerListener
{
protected $_profiler;
public function __construct()
{
$this->_profiler = new DbProfiler();
}
public function beforeQuery($event, $connection)
{
$this->_profiler->startProfile($connection->getSQLStatement(), $connection->getSQLVariables(), $connection->getSQLBindTypes());
}
public function afterQuery($event, $connection)
{
$this->_profiler->stopProfile();
}
public function getProfiler()
{
return $this->_profiler;
}
}
class DbProfilerTest extends PHPUnit_Framework_TestCase
{
public function testDbMysql()
{
require 'unit-tests/config.db.php';
if (empty($configMysql)) {
$this->markTestSkipped("Skipped");
return;
}
$connection = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql);
$this->_executeTests($connection);
}
public function testDbPostgresql()
{
require 'unit-tests/config.db.php';
if (empty($configPostgresql)) {
$this->markTestSkipped("Skipped");
return;
}
$connection = new Phalcon\Db\Adapter\Pdo\Postgresql($configPostgresql);
$this->_executeTests($connection);
}
public function testDbSqlite()
{
require 'unit-tests/config.db.php';
if (empty($configSqlite)) {
$this->markTestSkipped("Skipped");
return;
}
$connection = new Phalcon\Db\Adapter\Pdo\Sqlite($configSqlite);
$this->_executeTests($connection);
}
public function _executeTests($connection)
{
$eventsManager = new Phalcon\Events\Manager();
$listener = new DbProfilerListener();
$eventsManager->attach('db', $listener);
$connection->setEventsManager($eventsManager);
$connection->query("SELECT * FROM personas LIMIT 3");
$profiler = $listener->getProfiler();
$this->assertEquals($profiler->getNumberTotalStatements(), 1);
$profile = $profiler->getLastProfile();
$this->assertEquals(get_class($profile), 'Phalcon\Db\Profiler\Item');
$this->assertEquals($profile->getSQLStatement(), "SELECT * FROM personas LIMIT 3");
$this->assertEquals(gettype($profile->getInitialTime()), "double");
$this->assertEquals(gettype($profile->getFinalTime()), "double");
$this->assertEquals(gettype($profile->getTotalElapsedSeconds()), "double");
$this->assertTrue($profile->getFinalTime()>$profile->getInitialTime());
$connection->query("SELECT * FROM personas LIMIT 100");
$this->assertEquals($profiler->getNumberTotalStatements(), 2);
$profile = $profiler->getLastProfile();
$this->assertEquals(get_class($profile), 'Phalcon\Db\Profiler\Item');
$this->assertEquals($profile->getSQLStatement(), "SELECT * FROM personas LIMIT 100");
$this->assertTrue($profile->getFinalTime() > $profile->getInitialTime());
$connection->query("SELECT * FROM personas LIMIT 5");
$connection->query("SELECT * FROM personas LIMIT 10");
$connection->query("SELECT * FROM personas LIMIT 15");
$this->assertEquals(count($profiler->getProfiles()), 5);
$this->assertEquals($profiler->getNumberTotalStatements(), 5);
$this->assertEquals(gettype($profiler->getTotalElapsedSeconds()), "double");
$this->assertEquals($profiler->getPoints(), 0);
$profiler->reset();
$this->assertEquals(count($profiler->getProfiles()), 0);
$this->assertEquals($profiler->getNumberTotalStatements(), 0);
}
}