]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Extensions/Database/DB/DefaultDatabaseConnection.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Extensions / Database / DB / DefaultDatabaseConnection.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, 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-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 3.2.0
45  */
46
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/Filter.php';
49
50 require_once 'PHPUnit/Extensions/Database/DataSet/QueryTable.php';
51 require_once 'PHPUnit/Extensions/Database/DB/IDatabaseConnection.php';
52 require_once 'PHPUnit/Extensions/Database/DB/MetaData.php';
53 require_once 'PHPUnit/Extensions/Database/DB/ResultSetTable.php';
54 require_once 'PHPUnit/Extensions/Database/DB/DataSet.php';
55 require_once 'PHPUnit/Extensions/Database/DB/FilteredDataSet.php';
56
57 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
58
59 /**
60  * Provides a basic interface for communicating with a database.
61  *
62  * @category   Testing
63  * @package    PHPUnit
64  * @author     Mike Lively <m@digitalsandwich.com>
65  * @copyright  2009 Mike Lively <m@digitalsandwich.com>
66  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
67  * @version    Release: 3.3.17
68  * @link       http://www.phpunit.de/
69  * @since      Class available since Release 3.2.0
70  */
71 class PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection implements PHPUnit_Extensions_Database_DB_IDatabaseConnection
72 {
73     /**
74      * @var PDO
75      */
76     protected $connection;
77
78     /**
79      * @var string
80      */
81     protected $schema;
82
83     /**
84      * The metadata object used to retrieve table meta data from the database.
85      *
86      * @var PHPUnit_Extensions_Database_DB_IMetaData
87      */
88     protected $metaData;
89
90     /**
91      * Creates a new database connection
92      *
93      * @param PDO $connection
94      * @param string $schema - The name of the database schema you will be testing against.
95      */
96     public function __construct(PDO $connection, $schema)
97     {
98         $this->connection = $connection;
99         $this->metaData = PHPUnit_Extensions_Database_DB_MetaData::createMetaData($connection, $schema);
100         $this->schema = $schema;
101
102         $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
103     }
104
105     /**
106      * Close this connection.
107      */
108     public function close()
109     {
110         unset($this->connection);
111     }
112
113     /**
114      * Returns a database metadata object that can be used to retrieve table
115      * meta data from the database.
116      *
117      * @return PHPUnit_Extensions_Database_DB_IMetaData
118      */
119     public function getMetaData()
120     {
121         return $this->metaData;
122     }
123
124     /**
125      * Returns the schema for the connection.
126      *
127      * @return string
128      */
129     public function getSchema()
130     {
131         return $this->schema;
132     }
133
134     /**
135      * Creates a dataset containing the specified table names. If no table
136      * names are specified then it will created a dataset over the entire
137      * database.
138      *
139      * @param array $tableNames
140      * @return PHPUnit_Extensions_Database_DataSet_IDataSet
141      * @todo Implement the filtered data set.
142      */
143     public function createDataSet(Array $tableNames = NULL)
144     {
145         if (empty($tableNames)) {
146             return new PHPUnit_Extensions_Database_DB_DataSet($this);
147         } else {
148             return new PHPUnit_Extensions_Database_DB_FilteredDataSet($this, $tableNames);
149         }
150     }
151
152     /**
153      * Creates a table with the result of the specified SQL statement.
154      *
155      * @param string $resultName
156      * @param string $sql
157      * @return PHPUnit_Extensions_Database_DB_Table
158      */
159     public function createQueryTable($resultName, $sql)
160     {
161         return new PHPUnit_Extensions_Database_DataSet_QueryTable($resultName, $sql, $this);
162     }
163
164     /**
165      * Returns this connection database configuration
166      *
167      * @return PHPUnit_Extensions_Database_Database_DatabaseConfig
168      */
169     public function getConfig()
170     {
171
172     }
173
174     /**
175      * Returns a PDO Connection
176      *
177      * @return PDO
178      */
179     public function getConnection()
180     {
181         return $this->connection;
182     }
183
184     /**
185      * Returns the number of rows in the given table. You can specify an
186      * optional where clause to return a subset of the table.
187      *
188      * @param string $tableName
189      * @param string $whereClause
190      * @param int
191      */
192     public function getRowCount($tableName, $whereClause = NULL)
193     {
194         $query = "SELECT COUNT(*) FROM {$tableName}";
195
196         if (isset($whereClause)) {
197             $query .= " WHERE {$whereClause}";
198         }
199     }
200
201     /**
202      * Returns a quoted schema object. (table name, column name, etc)
203      *
204      * @param string $object
205      * @return string
206      */
207     public function quoteSchemaObject($object)
208     {
209         return $this->getMetaData()->quoteSchemaObject($object);
210     }
211
212     /**
213      * Returns the command used to truncate a table.
214      *
215      * @return string
216      */
217     public function getTruncateCommand()
218     {
219         return $this->getMetaData()->getTruncateCommand();
220     }
221
222     /**
223      * Returns true if the connection allows cascading
224      *
225      * @return bool
226      */
227     public function allowsCascading()
228     {
229         return $this->getMetaData()->allowsCascading();
230     }
231 }
232 ?>