Webtests fixes
[civicrm-core.git] / tests / phpunit / Utils.php
CommitLineData
6a488035
TO
1<?php
2// vim: set si ai expandtab tabstop=4 shiftwidth=4 softtabstop=4:
3
4/**
5 * File for the Utils class
6 *
7 * (PHP 5)
8 *
6c6e6187
TO
9 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
10 * @copyright Copyright CiviCRM LLC (C) 2009
11 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
6a488035 12 * GNU Affero General Public License version 3
6c6e6187
TO
13 * @version $Id: Utils.php 40328 2012-05-11 23:06:13Z allen $
14 * @package CiviCRM
6a488035
TO
15 *
16 * This file is part of CiviCRM
17 *
18 * CiviCRM is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Affero General Public License
20 * as published by the Free Software Foundation; either version 3 of
21 * the License, or (at your option) any later version.
22 *
23 * CiviCRM is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Affero General Public License for more details.
27 *
28 * You should have received a copy of the GNU Affero General Public
29 * License along with this program. If not, see
30 * <http://www.gnu.org/licenses/>.
31 */
32
33/**
34 * Utility functions
6c6e6187 35 * @package CiviCRM
6a488035
TO
36 */
37class Utils {
38
39 /**
40 * PDO for the database
6c6e6187 41 * @var PDO
6a488035
TO
42 */
43 public $pdo;
44
45 /**
46 * Construct an object for this database
47 */
fc3c781d 48 public function __construct($host, $port, $user, $pass) {
6a488035 49 try {
fc3c781d 50 $this->pdo = new PDO("mysql:host={$host}" . ($port ? ";port=$port" : ""),
6a488035
TO
51 $user, $pass,
52 array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE)
53 );
54 }
55 catch(PDOException$e) {
56 echo "Can't connect to MySQL server:" . PHP_EOL . $e->getMessage() . PHP_EOL;
57 exit(1);
58 }
59 }
60
61 /**
62 * Prepare and execute a query
63 *
64 * If the query fails, output a diagnostic message
6c6e6187
TO
65 * @param string Query to run
66 * @return mixed PDOStatement => Results of the query
6a488035
TO
67 * false => Query failed
68 */
00be9182 69 public function do_query($query) {
6a488035
TO
70 // echo "do_query($query)\n";
71 // $stmt = $this->pdo->query( $query, PDO::FETCH_ASSOC );
72 // echo "PDO returned";
73 // var_dump($stmt);
74 $string = preg_replace("/^#[^\n]*$/m", "\n", $query);
75 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
76
77 $queries = preg_split('/;\s*$/m', $string);
78 foreach ($queries as $query) {
79 $query = trim($query);
80 if (!empty($query)) {
81 $result = $this->pdo->query($query);
82 if ($this->pdo->errorCode() == 0) {
83 continue;
84 }
85 else {
86 var_dump($result);
87 var_dump($this->pdo->errorInfo());
88 // die( "Cannot execute $query: " . $this->pdo->errorInfo() );
89 }
90 }
91 }
92
93 /*******
94 if ( $this->pdo->errorCode() == 0 ) {
95 //echo "returning the PDOStmt\n";
96 return $stmt;
97 }
98
99 // operation failed, so output description of where and why
100 $errorInfo = $this->pdo->errorInfo();
101 echo "Oops, can't do query:\n {$query}\n in "
102 . basename( __FILE__) . " line " . __LINE__.":\n "
103 . $errorInfo[0] . ": " . $errorInfo[2] . "\n Call stack:\n";
104 $backtrace = debug_backtrace();
105 $dir_name = dirname( __FILE__ );
106 $cwd_len = strlen( $dir_name ) + 1;
9b873358 107 foreach ($backtrace as $frame ) {
6a488035
TO
108 echo " ";
109 if ( array_key_exists( 'class', $frame ) ) {
110 echo " class {$frame['class']}";
111 if ( array_key_exists( 'function', $frame ) ) {
112 echo " method {$frame['function']}";
113 }
0db6c3e1
TO
114 }
115 else {
6a488035
TO
116 if ( array_key_exists( 'function', $frame ) ) {
117 echo " function {$frame['function']}";
118 }
119 }
120 if ( array_key_exists( 'file', $frame ) ) {
121 echo " file ". substr( $frame['file'], $cwd_len );
122 }
123 if ( array_key_exists( 'line', $frame ) ) {
124 echo " line {$frame['line']}";
125 }
126 echo "\n";
127 }
128 ******/
129 return TRUE;
130 }
131}
132// class Utils
133
134// -- set Emacs parameters --
135// Local variables:
136// mode: php;
137// tab-width: 4
138// c-basic-offset: 4
139// c-hanging-comment-ender-p: nil
140// indent-tabs-mode: nil
141// End: