| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PHPUnit
|
|
|
4 |
*
|
|
|
5 |
* Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
|
|
|
6 |
* All rights reserved.
|
|
|
7 |
*
|
|
|
8 |
* Redistribution and use in source and binary forms, with or without
|
|
|
9 |
* modification, are permitted provided that the following conditions
|
|
|
10 |
* are met:
|
|
|
11 |
*
|
|
|
12 |
* * Redistributions of source code must retain the above copyright
|
|
|
13 |
* notice, this list of conditions and the following disclaimer.
|
|
|
14 |
*
|
|
|
15 |
* * Redistributions in binary form must reproduce the above copyright
|
|
|
16 |
* notice, this list of conditions and the following disclaimer in
|
|
|
17 |
* the documentation and/or other materials provided with the
|
|
|
18 |
* distribution.
|
|
|
19 |
*
|
|
|
20 |
* * Neither the name of Sebastian Bergmann nor the names of his
|
|
|
21 |
* contributors may be used to endorse or promote products derived
|
|
|
22 |
* from this software without specific prior written permission.
|
|
|
23 |
*
|
|
|
24 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
25 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
26 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
27 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
28 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
29 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
30 |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
31 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
32 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
33 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
34 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
35 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
36 |
*
|
|
|
37 |
* @category Testing
|
|
|
38 |
* @package PHPUnit
|
|
|
39 |
* @author Mike Lively <m@digitalsandwich.com>
|
|
|
40 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
41 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
42 |
* @link http://www.phpunit.de/
|
|
|
43 |
* @since File available since Release 3.2.0
|
|
|
44 |
*/
|
|
|
45 |
|
|
|
46 |
require_once 'PHPUnit/Framework.php';
|
|
|
47 |
require_once 'PHPUnit/Util/Filter.php';
|
|
|
48 |
|
|
|
49 |
require_once 'PHPUnit/Extensions/Database/DataSet/QueryTable.php';
|
|
|
50 |
require_once 'PHPUnit/Extensions/Database/DB/IDatabaseConnection.php';
|
|
|
51 |
require_once 'PHPUnit/Extensions/Database/DB/MetaData.php';
|
|
|
52 |
require_once 'PHPUnit/Extensions/Database/DB/ResultSetTable.php';
|
|
|
53 |
require_once 'PHPUnit/Extensions/Database/DB/DataSet.php';
|
|
|
54 |
require_once 'PHPUnit/Extensions/Database/DB/FilteredDataSet.php';
|
|
|
55 |
|
|
|
56 |
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Provides a basic interface for communicating with a database.
|
|
|
60 |
*
|
|
|
61 |
* @category Testing
|
|
|
62 |
* @package PHPUnit
|
|
|
63 |
* @author Mike Lively <m@digitalsandwich.com>
|
|
|
64 |
* @copyright 2010 Mike Lively <m@digitalsandwich.com>
|
|
|
65 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
66 |
* @version Release: 3.4.15
|
|
|
67 |
* @link http://www.phpunit.de/
|
|
|
68 |
* @since Class available since Release 3.2.0
|
|
|
69 |
*/
|
|
|
70 |
class PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection implements PHPUnit_Extensions_Database_DB_IDatabaseConnection
|
|
|
71 |
{
|
|
|
72 |
/**
|
|
|
73 |
* @var PDO
|
|
|
74 |
*/
|
|
|
75 |
protected $connection;
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* The metadata object used to retrieve table meta data from the database.
|
|
|
79 |
*
|
|
|
80 |
* @var PHPUnit_Extensions_Database_DB_IMetaData
|
|
|
81 |
*/
|
|
|
82 |
protected $metaData;
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Creates a new database connection
|
|
|
86 |
*
|
|
|
87 |
* @param PDO $connection
|
|
|
88 |
* @param string $schema - The name of the database schema you will be testing against.
|
|
|
89 |
*/
|
|
|
90 |
public function __construct(PDO $connection, $schema = '')
|
|
|
91 |
{
|
|
|
92 |
$this->connection = $connection;
|
|
|
93 |
$this->metaData = PHPUnit_Extensions_Database_DB_MetaData::createMetaData($connection, $schema);
|
|
|
94 |
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Close this connection.
|
|
|
99 |
*/
|
|
|
100 |
public function close()
|
|
|
101 |
{
|
|
|
102 |
unset($this->connection);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Returns a database metadata object that can be used to retrieve table
|
|
|
107 |
* meta data from the database.
|
|
|
108 |
*
|
|
|
109 |
* @return PHPUnit_Extensions_Database_DB_IMetaData
|
|
|
110 |
*/
|
|
|
111 |
public function getMetaData()
|
|
|
112 |
{
|
|
|
113 |
return $this->metaData;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Returns the schema for the connection.
|
|
|
118 |
*
|
|
|
119 |
* @return string
|
|
|
120 |
*/
|
|
|
121 |
public function getSchema()
|
|
|
122 |
{
|
|
|
123 |
return $this->getMetaData()->getSchema();
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Creates a dataset containing the specified table names. If no table
|
|
|
128 |
* names are specified then it will created a dataset over the entire
|
|
|
129 |
* database.
|
|
|
130 |
*
|
|
|
131 |
* @param array $tableNames
|
|
|
132 |
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
|
|
|
133 |
* @todo Implement the filtered data set.
|
|
|
134 |
*/
|
|
|
135 |
public function createDataSet(Array $tableNames = NULL)
|
|
|
136 |
{
|
|
|
137 |
if (empty($tableNames)) {
|
|
|
138 |
return new PHPUnit_Extensions_Database_DB_DataSet($this);
|
|
|
139 |
} else {
|
|
|
140 |
return new PHPUnit_Extensions_Database_DB_FilteredDataSet($this, $tableNames);
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* Creates a table with the result of the specified SQL statement.
|
|
|
146 |
*
|
|
|
147 |
* @param string $resultName
|
|
|
148 |
* @param string $sql
|
|
|
149 |
* @return PHPUnit_Extensions_Database_DB_Table
|
|
|
150 |
*/
|
|
|
151 |
public function createQueryTable($resultName, $sql)
|
|
|
152 |
{
|
|
|
153 |
return new PHPUnit_Extensions_Database_DataSet_QueryTable($resultName, $sql, $this);
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Returns this connection database configuration
|
|
|
158 |
*
|
|
|
159 |
* @return PHPUnit_Extensions_Database_Database_DatabaseConfig
|
|
|
160 |
*/
|
|
|
161 |
public function getConfig()
|
|
|
162 |
{
|
|
|
163 |
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Returns a PDO Connection
|
|
|
168 |
*
|
|
|
169 |
* @return PDO
|
|
|
170 |
*/
|
|
|
171 |
public function getConnection()
|
|
|
172 |
{
|
|
|
173 |
return $this->connection;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Returns the number of rows in the given table. You can specify an
|
|
|
178 |
* optional where clause to return a subset of the table.
|
|
|
179 |
*
|
|
|
180 |
* @param string $tableName
|
|
|
181 |
* @param string $whereClause
|
|
|
182 |
* @param int
|
|
|
183 |
*/
|
|
|
184 |
public function getRowCount($tableName, $whereClause = NULL)
|
|
|
185 |
{
|
|
|
186 |
$query = "SELECT COUNT(*) FROM ".$this->quoteSchemaObject($tableName);
|
|
|
187 |
|
|
|
188 |
if (isset($whereClause)) {
|
|
|
189 |
$query .= " WHERE {$whereClause}";
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Returns a quoted schema object. (table name, column name, etc)
|
|
|
195 |
*
|
|
|
196 |
* @param string $object
|
|
|
197 |
* @return string
|
|
|
198 |
*/
|
|
|
199 |
public function quoteSchemaObject($object)
|
|
|
200 |
{
|
|
|
201 |
return $this->getMetaData()->quoteSchemaObject($object);
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
/**
|
|
|
205 |
* Returns the command used to truncate a table.
|
|
|
206 |
*
|
|
|
207 |
* @return string
|
|
|
208 |
*/
|
|
|
209 |
public function getTruncateCommand()
|
|
|
210 |
{
|
|
|
211 |
return $this->getMetaData()->getTruncateCommand();
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
/**
|
|
|
215 |
* Returns true if the connection allows cascading
|
|
|
216 |
*
|
|
|
217 |
* @return bool
|
|
|
218 |
*/
|
|
|
219 |
public function allowsCascading()
|
|
|
220 |
{
|
|
|
221 |
return $this->getMetaData()->allowsCascading();
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
?>
|