| 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/DefaultTester.php';
|
|
|
50 |
require_once 'PHPUnit/Extensions/Database/DB/DefaultDatabaseConnection.php';
|
|
|
51 |
require_once 'PHPUnit/Extensions/Database/Operation/Factory.php';
|
|
|
52 |
require_once 'PHPUnit/Extensions/Database/Constraint/TableIsEqual.php';
|
|
|
53 |
require_once 'PHPUnit/Extensions/Database/Constraint/DataSetIsEqual.php';
|
|
|
54 |
|
|
|
55 |
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* A TestCase extension that provides functionality for testing and asserting
|
|
|
59 |
* against a real 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 |
abstract class PHPUnit_Extensions_Database_TestCase extends PHPUnit_Framework_TestCase
|
|
|
71 |
{
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* @var PHPUnit_Extensions_Database_ITester
|
|
|
75 |
*/
|
|
|
76 |
protected $databaseTester;
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Closes the specified connection.
|
|
|
80 |
*
|
|
|
81 |
* @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
|
|
|
82 |
*/
|
|
|
83 |
protected function closeConnection(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection)
|
|
|
84 |
{
|
|
|
85 |
$this->getDatabaseTester()->closeConnection($connection);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Returns the test database connection.
|
|
|
90 |
*
|
|
|
91 |
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
|
|
|
92 |
*/
|
|
|
93 |
protected abstract function getConnection();
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Gets the IDatabaseTester for this testCase. If the IDatabaseTester is
|
|
|
97 |
* not set yet, this method calls newDatabaseTester() to obtain a new
|
|
|
98 |
* instance.
|
|
|
99 |
*
|
|
|
100 |
* @return PHPUnit_Extensions_Database_ITester
|
|
|
101 |
*/
|
|
|
102 |
protected function getDatabaseTester()
|
|
|
103 |
{
|
|
|
104 |
if (empty($this->databaseTester)) {
|
|
|
105 |
$this->databaseTester = $this->newDatabaseTester();
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
return $this->databaseTester;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Returns the test dataset.
|
|
|
113 |
*
|
|
|
114 |
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
|
|
|
115 |
*/
|
|
|
116 |
protected abstract function getDataSet();
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Returns the database operation executed in test setup.
|
|
|
120 |
*
|
|
|
121 |
* @return PHPUnit_Extensions_Database_Operation_DatabaseOperation
|
|
|
122 |
*/
|
|
|
123 |
protected function getSetUpOperation()
|
|
|
124 |
{
|
|
|
125 |
return PHPUnit_Extensions_Database_Operation_Factory::CLEAN_INSERT();
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Returns the database operation executed in test cleanup.
|
|
|
130 |
*
|
|
|
131 |
* @return PHPUnit_Extensions_Database_Operation_DatabaseOperation
|
|
|
132 |
*/
|
|
|
133 |
protected function getTearDownOperation()
|
|
|
134 |
{
|
|
|
135 |
return PHPUnit_Extensions_Database_Operation_Factory::NONE();
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Creates a IDatabaseTester for this testCase.
|
|
|
140 |
*
|
|
|
141 |
* @return PHPUnit_Extensions_Database_ITester
|
|
|
142 |
*/
|
|
|
143 |
protected function newDatabaseTester()
|
|
|
144 |
{
|
|
|
145 |
return new PHPUnit_Extensions_Database_DefaultTester($this->getConnection());
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Creates a new DefaultDatabaseConnection using the given PDO connection
|
|
|
150 |
* and database schema name.
|
|
|
151 |
*
|
|
|
152 |
* @param PDO $connection
|
|
|
153 |
* @param string $schema
|
|
|
154 |
* @return PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
|
|
|
155 |
*/
|
|
|
156 |
protected function createDefaultDBConnection(PDO $connection, $schema = '')
|
|
|
157 |
{
|
|
|
158 |
return new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection($connection, $schema);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Creates a new FlatXmlDataSet with the given $xmlFile. (absolute path.)
|
|
|
163 |
*
|
|
|
164 |
* @param string $xmlFile
|
|
|
165 |
* @return PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet
|
|
|
166 |
*/
|
|
|
167 |
protected function createFlatXMLDataSet($xmlFile)
|
|
|
168 |
{
|
|
|
169 |
require_once 'PHPUnit/Extensions/Database/DataSet/FlatXmlDataSet.php';
|
|
|
170 |
return new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet($xmlFile);
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Creates a new XMLDataSet with the given $xmlFile. (absolute path.)
|
|
|
175 |
*
|
|
|
176 |
* @param string $xmlFile
|
|
|
177 |
* @return PHPUnit_Extensions_Database_DataSet_XmlDataSet
|
|
|
178 |
*/
|
|
|
179 |
protected function createXMLDataSet($xmlFile)
|
|
|
180 |
{
|
|
|
181 |
require_once 'PHPUnit/Extensions/Database/DataSet/XmlDataSet.php';
|
|
|
182 |
return new PHPUnit_Extensions_Database_DataSet_XmlDataSet($xmlFile);
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* Returns an operation factory instance that can be used to instantiate
|
|
|
187 |
* new operations.
|
|
|
188 |
*
|
|
|
189 |
* @return PHPUnit_Extensions_Database_Operation_Factory
|
|
|
190 |
*/
|
|
|
191 |
protected function getOperations()
|
|
|
192 |
{
|
|
|
193 |
require_once 'PHPUnit/Extensions/Database/Operation/Factory.php';
|
|
|
194 |
return new PHPUnit_Extensions_Database_Operation_Factory();
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
* Performs operation returned by getSetUpOperation().
|
|
|
199 |
*/
|
|
|
200 |
protected function setUp()
|
|
|
201 |
{
|
|
|
202 |
parent::setUp();
|
|
|
203 |
|
|
|
204 |
$this->databaseTester = NULL;
|
|
|
205 |
|
|
|
206 |
$this->getDatabaseTester()->setSetUpOperation($this->getSetUpOperation());
|
|
|
207 |
$this->getDatabaseTester()->setDataSet($this->getDataSet());
|
|
|
208 |
$this->getDatabaseTester()->onSetUp();
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
/**
|
|
|
212 |
* Performs operation returned by getSetUpOperation().
|
|
|
213 |
*/
|
|
|
214 |
protected function tearDown()
|
|
|
215 |
{
|
|
|
216 |
$this->getDatabaseTester()->setTearDownOperation($this->getTearDownOperation());
|
|
|
217 |
$this->getDatabaseTester()->setDataSet($this->getDataSet());
|
|
|
218 |
$this->getDatabaseTester()->onTearDown();
|
|
|
219 |
|
|
|
220 |
/**
|
|
|
221 |
* Destroy the tester after the test is run to keep DB connections
|
|
|
222 |
* from piling up.
|
|
|
223 |
*/
|
|
|
224 |
$this->databaseTester = NULL;
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* Asserts that two given tables are equal.
|
|
|
229 |
*
|
|
|
230 |
* @param PHPUnit_Extensions_Database_DataSet_ITable $expected
|
|
|
231 |
* @param PHPUnit_Extensions_Database_DataSet_ITable $actual
|
|
|
232 |
* @param string $message
|
|
|
233 |
*/
|
|
|
234 |
public static function assertTablesEqual(PHPUnit_Extensions_Database_DataSet_ITable $expected, PHPUnit_Extensions_Database_DataSet_ITable $actual, $message = '')
|
|
|
235 |
{
|
|
|
236 |
$constraint = new PHPUnit_Extensions_Database_Constraint_TableIsEqual($expected);
|
|
|
237 |
|
|
|
238 |
self::assertThat($actual, $constraint, $message);
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
/**
|
|
|
242 |
* Asserts that two given datasets are equal.
|
|
|
243 |
*
|
|
|
244 |
* @param PHPUnit_Extensions_Database_DataSet_ITable $expected
|
|
|
245 |
* @param PHPUnit_Extensions_Database_DataSet_ITable $actual
|
|
|
246 |
* @param string $message
|
|
|
247 |
*/
|
|
|
248 |
public static function assertDataSetsEqual(PHPUnit_Extensions_Database_DataSet_IDataSet $expected, PHPUnit_Extensions_Database_DataSet_IDataSet $actual, $message = '')
|
|
|
249 |
{
|
|
|
250 |
$constraint = new PHPUnit_Extensions_Database_Constraint_DataSetIsEqual($expected);
|
|
|
251 |
|
|
|
252 |
self::assertThat($actual, $constraint, $message);
|
|
|
253 |
}
|
|
|
254 |
}
|
|
|
255 |
?>
|