cakephp1.3でUnitTestを書く際は、Simpletestを使っていると思います。(1.x系)
DebugKitとか使えばExplainの結果がみれるし、いらないと言われればいらないのですが、UnitTestの時にExplainも表示させたいとか、DebugKitが使えない状況って意外とあったりします。JSON返すだけの処理しかなかったりとか。。。
んで、やっぱExplainてきなのをSimpletestとかで見れるようにしようとおもいElementを改造することにしました。
まずは「cake/lib/element/sql_dump.php」を「app/view/element/sql_dump.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
|
if (!class_exists('ConnectionManager') || Configure::read('debug') < 2) {
return false;
}
//
$exQuery = array();
$noLogs = !isset($logs);
if ($noLogs):
$sources = ConnectionManager::sourceList();
$logs = array();
foreach ($sources as $source):
$db =& ConnectionManager::getDataSource($source);
if (!$db->isInterfaceSupported('getLog')):
continue;
endif;
$logs[$source] = $db->getLog();
endforeach;
endif;
if ($noLogs || isset($_forced_from_dbo_)):
foreach ($logs as $source => $logInfo):
$text = $logInfo['count'] > 1 ? 'queries' : 'query';
printf(
'<table class="cake-sql-log" id="cakeSqlLog_%s" summary="Cake SQL Log" cellspacing="0" border = "0">',
preg_replace('/[^A-Za-z0-9_]/', '_', uniqid(time(), true))
);
printf('<caption>(%s) %s %s took %s ms</caption>', $source, $logInfo['count'], $text, $logInfo['time']);
?>
<thead>
<tr><th>Nr</th><th>Query</th><th>Error</th><th>Affected</th><th>Num. rows</th><th>Took (ms)</th></tr>
</thead>
<tbody>
<?php
foreach ($logInfo['log'] as $k => $i) :
if ( strpos( $i['query'], 'SELECT') !== false ) {
$exQuery[$k] = $i['query'];
}
echo "<tr><td>" . ($k + 12) . "</td><td>" . h($i['query']) . "</td><td>{$i['error']}</td><td style = \"text-align: right\">{$i['affected']}</td><td style = \"text-align: right\">{$i['numRows']}</td><td style = \"text-align: right\">{$i['took']}</td></tr>\n";
endforeach;
?>
</tbody></table>
<?php
endforeach;
else:
echo '<p>Encountered unexpected $logs cannot generate SQL log</p>';
endif;
// EXPLAIN用の拡張
$exResult = array();
foreach($exQuery as $key => $value ) {
$exTmp = $db->query('EXPLAIN '.$value);
$exResult[] = array('query' => 'EXPLAIN ' .$value, 'result' => $exTmp[0][0] );
}
?>
<?php if ( !empty($exResult) ) :?>
<br />
<br />
<div style="text-align:center">EXPLAIN info</div>
<?php foreach( $exResult as $exKey => $exValue) : ?>
<table class="cake-sql-log" cellspacing="0" border = "0">
<thead>
<tr><th style="width:100px;">select_type</th><th style="width:100px;">table</th><th style="width:80px;">type</th><th>possible_keys</th><th>key</th><th>key_len</th><th>ref</th><th>rows</th><th>Extra</th></tr>
</thead>
<tbody>
<tr style="color:#FF0000;">
<?php foreach( $exValue['result'] as $exIdx => $ex ) : ?>
<?php if ( $exIdx != 'id') :?>
<td>
<?php echo $ex;?>
</td>
<?php endif;?>
<?php endforeach;?>
</tr>
<tr>
</tr>
<tr >
<td colspan="9" style="background-color:#ccc;font-size:8px;">
Query: <?php echo $exValue['query'];?>
</td>
</tr>
</tbody>
</table>
<?php endforeach;?>
<?php endif;?>
|
ちょっとしたときに意外と便利だったりしますよー。