Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 23 | abstract class TestCase extends PHPUnit_Framework_TestCase |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * DSN used for the DB backend. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $dsn; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Name of the DB, extracted from DSN. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $db; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Database handler -- to not be constructed twice for one test. |
||
| 41 | * |
||
| 42 | * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler |
||
| 43 | */ |
||
| 44 | protected $handler; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Doctrine Database connection -- to not be constructed twice for one test. |
||
| 48 | * |
||
| 49 | * @var \Doctrine\DBAL\Connection |
||
| 50 | */ |
||
| 51 | protected $connection; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Property which holds the state if this is the initial test run, so that |
||
| 55 | * we should set up the database, or if this is any of the following test |
||
| 56 | * runs, where it is sufficient to reset the database. |
||
| 57 | */ |
||
| 58 | protected static $initial = true; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get data source name. |
||
| 62 | * |
||
| 63 | * The database connection string is read from an optional environment |
||
| 64 | * variable "DATABASE" and defaults to an in-memory SQLite database. |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | protected function getDsn() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get a eZ Doctrine database connection handler. |
||
| 83 | * |
||
| 84 | * Get a ConnectionHandler, which can be used to interact with the configured |
||
| 85 | * database. The database connection string is read from an optional |
||
| 86 | * environment variable "DATABASE" and defaults to an in-memory SQLite |
||
| 87 | * database. |
||
| 88 | * |
||
| 89 | * @return \eZ\Publish\Core\Persistence\Doctrine\ConnectionHandler |
||
| 90 | */ |
||
| 91 | public function getDatabaseHandler() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get native Doctrine database connection. |
||
| 103 | * |
||
| 104 | * @return \Doctrine\DBAL\Connection |
||
| 105 | */ |
||
| 106 | public function getDatabaseConnection() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Resets the database on test setup, so we always operate on a clean |
||
| 117 | * database. |
||
| 118 | */ |
||
| 119 | public function setUp() |
||
| 141 | |||
| 142 | protected function tearDown() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get a text representation of a result set. |
||
| 149 | * |
||
| 150 | * @param array $result |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | protected static function getResultTextRepresentation(array $result) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Inserts database fixture from $file. |
||
| 169 | * |
||
| 170 | * @param string $file |
||
| 171 | */ |
||
| 172 | protected function insertDatabaseFixture($file) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Reset DB sequences. |
||
| 222 | */ |
||
| 223 | public function resetSequences() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Assert query result as correct. |
||
| 240 | * |
||
| 241 | * Builds text representations of the asserted and fetched query result, |
||
| 242 | * based on a eZ\Publish\Core\Persistence\Database\SelectQuery object. Compares them using classic diff for |
||
| 243 | * maximum readability of the differences between expectations and real |
||
| 244 | * results. |
||
| 245 | * |
||
| 246 | * The expectation MUST be passed as a two dimensional array containing |
||
| 247 | * rows of columns. |
||
| 248 | * |
||
| 249 | * @param array $expectation |
||
| 250 | * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query |
||
| 251 | * @param string $message |
||
| 252 | */ |
||
| 253 | public static function assertQueryResult(array $expectation, SelectQuery $query, $message = null) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Asserts correct property values on $object. |
||
| 272 | * |
||
| 273 | * Asserts that for all keys in $properties a corresponding property |
||
| 274 | * exists in $object with the *same* value as in $properties. |
||
| 275 | * |
||
| 276 | * @param array $properties |
||
| 277 | * @param object $object |
||
| 278 | */ |
||
| 279 | protected function assertPropertiesCorrect(array $properties, $object) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Asserts $expStruct equals $actStruct in at least $propertyNames. |
||
| 297 | * |
||
| 298 | * Asserts that properties of $actStruct equal properties of $expStruct (not |
||
| 299 | * vice versa!). If $propertyNames is null, all properties are checked. |
||
| 300 | * Otherwise, $propertyNames provides a white list. |
||
| 301 | * |
||
| 302 | * @param object $expStruct |
||
| 303 | * @param object $actStruct |
||
| 304 | * @param array $propertyNames |
||
| 305 | */ |
||
| 306 | protected function assertStructsEqual( |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Returns public property names in $object. |
||
| 325 | * |
||
| 326 | * @param object $object |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | protected function getPublicPropertyNames($object) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | View Code Duplication | protected static function getInstallationDir() |
|
| 355 | } |
||
| 356 |