Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- Merge [4494:4495] and [4591:4593].

  • Loading branch information...
commit d5b1e3ac06d054d3710620244ad131f255510c0c 1 parent f9e3332
@sebastianbergmann authored
View
19 PHPUnit/Extensions/Database/DB/DataSet.php
@@ -99,7 +99,7 @@ public function __construct(PHPUnit_Extensions_Database_DB_IDatabaseConnection $
* @param PHPUnit_Extensions_Database_DataSet_ITableMetaData $tableMetaData
* @return unknown
*/
- public static function buildTableSelect(PHPUnit_Extensions_Database_DataSet_ITableMetaData $tableMetaData)
+ public static function buildTableSelect(PHPUnit_Extensions_Database_DataSet_ITableMetaData $tableMetaData, PHPUnit_Extensions_Database_DB_IDatabaseConnection $databaseConnection = NULL)
{
if ($tableMetaData->getTableName() == '') {
$e = new Exception("Empty Table Name");
@@ -107,16 +107,29 @@ public static function buildTableSelect(PHPUnit_Extensions_Database_DataSet_ITab
throw $e;
}
- $columnList = implode(', ', $tableMetaData->getColumns());
+ $columns = $tableMetaData->getColumns();
+ if ($databaseConnection) {
+ $columns = array_map(array($databaseConnection, 'quoteSchemaObject'), $columns);
+ }
+ $columnList = implode(', ', $columns);
+
+ if ($databaseConnection) {
+ $tableName = $databaseConnection->quoteSchemaObject($tableMetaData->getTableName());
+ } else {
+ $tableName = $tableMetaData->getTableName();
+ }
$primaryKeys = $tableMetaData->getPrimaryKeys();
+ if ($databaseConnection) {
+ $primaryKeys = array_map(array($databaseConnection, 'quoteSchemaObject'), $primaryKeys);
+ }
if (count($primaryKeys)) {
$orderBy = 'ORDER BY ' . implode(' ASC, ', $primaryKeys) . ' ASC';
} else {
$orderBy = '';
}
- return "SELECT {$columnList} FROM {$tableMetaData->getTableName()} {$orderBy}";
+ return "SELECT {$columnList} FROM {$tableName} {$orderBy}";
}
/**
View
2  PHPUnit/Extensions/Database/DB/DefaultDatabaseConnection.php
@@ -190,7 +190,7 @@ public function getConnection()
*/
public function getRowCount($tableName, $whereClause = NULL)
{
- $query = "SELECT COUNT(*) FROM {$tableName}";
+ $query = "SELECT COUNT(*) FROM ".$this->quoteSchemaObject($tableName);
if (isset($whereClause)) {
$query .= " WHERE {$whereClause}";
View
36 PHPUnit/Extensions/Database/DB/MetaData.php
@@ -177,9 +177,39 @@ public function getSchema()
*/
public function quoteSchemaObject($object)
{
- return $this->schemaObjectQuoteChar.
- str_replace($this->schemaObjectQuoteChar, $this->schemaObjectQuoteChar.$this->schemaObjectQuoteChar, $object).
- $this->schemaObjectQuoteChar;
+ $parts = explode('.', $object);
+ $quotedParts = array();
+
+ foreach ($parts as $part) {
+ $quotedParts[] = $this->schemaObjectQuoteChar .
+ str_replace($this->schemaObjectQuoteChar, $this->schemaObjectQuoteChar.$this->schemaObjectQuoteChar, $part).
+ $this->schemaObjectQuoteChar;
+ }
+
+ return implode('.', $quotedParts);
+ }
+
+ /**
+ * Seperates the schema and the table from a fully qualified table name.
+ *
+ * Returns an associative array containing the 'schema' and the 'table'.
+ *
+ * @param string $fullTableName
+ * @return array
+ */
+ public function splitTableName($fullTableName)
+ {
+ if (($dot = strpos($fullTableName, '.')) !== FALSE) {
+ return array(
+ 'schema' => substr($fullTableName, 0, $dot),
+ 'table' => substr($fullTableName, $dot + 1)
+ );
+ } else {
+ return array(
+ 'schema' => NULL,
+ 'table' => $fullTableName
+ );
+ }
}
/**
View
4 PHPUnit/Extensions/Database/DB/MetaData/MySQL.php
@@ -98,7 +98,7 @@ public function getTableNames()
*/
public function getTableColumns($tableName)
{
- $query = 'SHOW COLUMNS FROM `' . $tableName . '`';
+ $query = 'SHOW COLUMNS FROM ' . $this->quoteSchemaObject($tableName);
$statement = $this->pdo->prepare($query);
$statement->execute();
@@ -119,7 +119,7 @@ public function getTableColumns($tableName)
*/
public function getTablePrimaryKeys($tableName)
{
- $query = 'SHOW INDEX FROM `' . $tableName . '`';
+ $query = 'SHOW INDEX FROM ' . $this->quoteSchemaObject($tableName);
$statement = $this->pdo->prepare($query);
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
View
14 PHPUnit/Extensions/Database/DB/MetaData/Oci.php
@@ -132,9 +132,18 @@ protected function loadColumnInfo($tableName)
$this->columns[$tableName] = array();
$this->keys[$tableName] = array();
+ $tableParts = $this->splitTableName($tableName);
+
+ if (!empty($tableParts['schema']))
+ {
+ $ownerQuery = " AND OWNER = '{$schema}'";
+ $conOwnerQuery = " AND a.owner = '{$schema}'";
+ }
+
$query = "SELECT DISTINCT COLUMN_NAME
FROM USER_TAB_COLUMNS
- WHERE TABLE_NAME='".$tableName."'
+ WHERE TABLE_NAME='".$tableParts['table']."'
+ $ownerQuery
ORDER BY COLUMN_NAME";
$result = $this->pdo->query($query);
@@ -147,7 +156,8 @@ protected function loadColumnInfo($tableName)
FROM all_constraints a, all_cons_columns b
WHERE a.constraint_type='P'
AND a.constraint_name=b.constraint_name
- AND a.table_name = '".$tableName."' ";
+ $conOwnerQuery
+ AND a.table_name = '".$tableParts['table']."' ";
$result = $this->pdo->query($keyQuery);
View
2  PHPUnit/Extensions/Database/DB/Table.php
@@ -76,7 +76,7 @@ public function __construct(PHPUnit_Extensions_Database_DataSet_ITableMetaData $
{
$this->setTableMetaData($tableMetaData);
- $pdoStatement = $databaseConnection->getConnection()->prepare(PHPUnit_Extensions_Database_DB_DataSet::buildTableSelect($tableMetaData));
+ $pdoStatement = $databaseConnection->getConnection()->prepare(PHPUnit_Extensions_Database_DB_DataSet::buildTableSelect($tableMetaData, $databaseConnection));
$pdoStatement->execute();
$this->data = $pdoStatement->fetchAll(PDO::FETCH_ASSOC);
}
View
88 PHPUnit/Extensions/Database/DataSet/DataSetFilter.php
@@ -78,13 +78,25 @@ class PHPUnit_Extensions_Database_DataSet_DataSetFilter extends PHPUnit_Extensio
* The tables to exclude from the data set.
* @var Array
*/
- protected $excludeTables;
+ protected $excludeTables = array();
+
+ /**
+ * The tables to exclude from the data set.
+ * @var Array
+ */
+ protected $includeTables = array();
/**
* The columns to exclude from the data set.
* @var Array
*/
- protected $excludeColumns;
+ protected $excludeColumns = array();
+
+ /**
+ * The columns to exclude from the data set.
+ * @var Array
+ */
+ protected $includeColumns = array();
/**
* Creates a new filtered data set.
@@ -95,22 +107,24 @@ class PHPUnit_Extensions_Database_DataSet_DataSetFilter extends PHPUnit_Extensio
* to the special string '*'.
*
* @param PHPUnit_Extensions_Database_DataSet_IDataSet $originalDataSet
- * @param Array $excludeTables
+ * @param Array $excludeTables @deprecated use set* methods instead.
*/
- public function __construct(PHPUnit_Extensions_Database_DataSet_IDataSet $originalDataSet, Array $excludeTables)
+ public function __construct(PHPUnit_Extensions_Database_DataSet_IDataSet $originalDataSet, Array $excludeTables = array())
{
$this->originalDataSet = $originalDataSet;
- $this->excludeTables = $excludeTables;
- foreach ($this->excludeTables as $tableName => $values) {
+ $tables = array();
+ foreach ($excludeTables as $tableName => $values) {
if (is_array($values)) {
- $this->excludeColumns[$tableName] = $values;
+ $this->setExcludeColumnsForTable($tableName, $values);
} elseif ($values == '*') {
- $this->excludeTables[] = $tableName;
+ $tables[] = $tableName;
} else {
- $this->excludeColumns[$tableName] = (array)$values;
+ $this->setExcludeColumnsForTable($tableName, (array)$values);
}
}
+
+ $this->addExcludeTables($tables);
}
/**
@@ -129,10 +143,22 @@ protected function createIterator($reverse = FALSE)
/* @var $table PHPUnit_Extensions_Database_DataSet_ITable */
$tableName = $table->getTableMetaData()->getTableName();
- if (in_array($tableName, $this->excludeTables)) {
+ if ((!in_array($tableName, $this->includeTables) && !empty($this->includeTables)) ||
+ in_array($tableName, $this->excludeTables)
+ ) {
continue;
- } elseif (array_key_exists($tableName, $this->excludeColumns)) {
- $new_tables[] = new PHPUnit_Extensions_Database_DataSet_TableFilter($table, $this->excludeColumns[$tableName]);
+ } elseif (!empty($this->excludeColumns[$tableName]) || !empty($this->includeColumns[$tableName])) {
+ $new_table = new PHPUnit_Extensions_Database_DataSet_TableFilter($table);
+
+ if (!empty($this->includeColumns[$tableName])) {
+ $new_table->addIncludeColumns($this->includeColumns[$tableName]);
+ }
+
+ if (!empty($this->excludeColumns[$tableName])) {
+ $new_table->addExcludeColumns($this->excludeColumns[$tableName]);
+ }
+
+ $new_tables[] = $new_table;
} else {
$new_tables[] = $table;
}
@@ -140,5 +166,43 @@ protected function createIterator($reverse = FALSE)
return new PHPUnit_Extensions_Database_DataSet_DefaultTableIterator($new_tables);
}
+
+ /**
+ * Adds tables to be included in the data set.
+ * @param array $tables
+ */
+ public function addIncludeTables(Array $tables)
+ {
+ $this->includeTables = array_unique(array_merge($this->includeTables, $tables));
+ }
+
+ /**
+ * Adds tables to be included in the data set.
+ * @param array $tables
+ */
+ public function addExcludeTables(Array $tables)
+ {
+ $this->excludeTables = array_unique(array_merge($this->excludeTables, $tables));
+ }
+
+ /**
+ * Adds columns to include in the data set for the given table.
+ * @param string $table
+ * @param Array $columns
+ */
+ public function setIncludeColumnsForTable($table, Array $columns)
+ {
+ $this->includeColumns[$table] = $columns;
+ }
+
+ /**
+ * Adds columns to include in the data set for the given table.
+ * @param string $table
+ * @param Array $columns
+ */
+ public function setExcludeColumnsForTable($table, Array $columns)
+ {
+ $this->excludeColumns[$table] = $columns;
+ }
}
?>
View
45 PHPUnit/Extensions/Database/DataSet/TableFilter.php
@@ -73,10 +73,17 @@ class PHPUnit_Extensions_Database_DataSet_TableFilter extends PHPUnit_Extensions
*/
protected $originalTable;
- public function __construct(PHPUnit_Extensions_Database_DataSet_ITable $originalTable, Array $excludeColumns)
+ /**
+ * Creates a new table filter using the original table
+ *
+ * @param $originalTable PHPUnit_Extensions_Database_DataSet_ITable
+ * @param $excludeColumns Array @deprecated, use the set* methods instead.
+ */
+ public function __construct(PHPUnit_Extensions_Database_DataSet_ITable $originalTable, Array $excludeColumns = array())
{
$this->originalTable = $originalTable;
- $this->setTableMetaData(new PHPUnit_Extensions_Database_DataSet_TableMetaDataFilter($originalTable->getTableMetaData(), $excludeColumns));
+ $this->setTableMetaData(new PHPUnit_Extensions_Database_DataSet_TableMetaDataFilter($originalTable->getTableMetaData()));
+ $this->addExcludeColumns($excludeColumns);
}
/**
@@ -103,5 +110,39 @@ public function getValue($row, $column)
throw new InvalidArgumentException("The given row ({$row}) and column ({$column}) do not exist in table {$this->getTableMetaData()->getTableName()}");
}
}
+
+ /**
+ * Sets the columns to include in the table.
+ * @param Array $includeColumns
+ */
+ public function addIncludeColumns(Array $includeColumns)
+ {
+ $this->tableMetaData->addIncludeColumns($includeColumns);
+ }
+
+ /**
+ * Clears the included columns.
+ */
+ public function clearIncludeColumns()
+ {
+ $this->tableMetaData->clearIncludeColumns();
+ }
+
+ /**
+ * Sets the columns to exclude from the table.
+ * @param Array $excludeColumns
+ */
+ public function addExcludeColumns(Array $excludeColumns)
+ {
+ $this->tableMetaData->addExcludeColumns($excludeColumns);
+ }
+
+ /**
+ * Clears the included columns.
+ */
+ public function clearExcludeColumns()
+ {
+ $this->tableMetaData->clearExcludeColumns();
+ }
}
?>
View
63 PHPUnit/Extensions/Database/DataSet/TableMetaDataFilter.php
@@ -52,9 +52,12 @@
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
/**
- * A TableMetaData decorator that allows filtering out columns from another
+ * A TableMetaData decorator that allows filtering columns from another
* metaData object.
*
+ * The if a whitelist (include) filter is specified, then only those columns
+ * will be included.
+ *
* @category Testing
* @package PHPUnit
* @author Mike Lively <m@digitalsandwich.com>
@@ -77,19 +80,25 @@ class PHPUnit_Extensions_Database_DataSet_TableMetaDataFilter extends PHPUnit_Ex
* The columns to exclude from the meta data.
* @var Array
*/
- protected $excludeColumns;
+ protected $excludeColumns = array();
+
+ /**
+ * The columns to include from the meta data.
+ * @var Array
+ */
+ protected $includeColumns = array();
/**
* Creates a new filtered table meta data object filtering out
* $excludeColumns.
*
* @param PHPUnit_Extensions_Database_DataSet_ITableMetaData $originalMetaData
- * @param array $excludeColumns
+ * @param array $excludeColumns - Deprecated. Use the set* methods instead.
*/
- public function __construct(PHPUnit_Extensions_Database_DataSet_ITableMetaData $originalMetaData, Array $excludeColumns)
+ public function __construct(PHPUnit_Extensions_Database_DataSet_ITableMetaData $originalMetaData, Array $excludeColumns = array())
{
$this->originalMetaData = $originalMetaData;
- $this->excludeColumns = $excludeColumns;
+ $this->addExcludeColumns($excludeColumns);
}
/**
@@ -99,7 +108,15 @@ public function __construct(PHPUnit_Extensions_Database_DataSet_ITableMetaData $
*/
public function getColumns()
{
- return array_values(array_diff($this->originalMetaData->getColumns(), $this->excludeColumns));
+ if (!empty($this->includeColumns)) {
+ return array_values(array_intersect($this->originalMetaData->getColumns(), $this->includeColumns));
+ }
+ elseif (!empty($this->excludeColumns)) {
+ return array_values(array_diff($this->originalMetaData->getColumns(), $this->excludeColumns));
+ }
+ else {
+ return $this->originalMetaData->getColumns();
+ }
}
/**
@@ -121,5 +138,39 @@ public function getTableName()
{
return $this->originalMetaData->getTableName();
}
+
+ /**
+ * Sets the columns to include in the table.
+ * @param Array $includeColumns
+ */
+ public function addIncludeColumns(Array $includeColumns)
+ {
+ $this->includeColumns = array_unique(array_merge($this->includeColumns, $includeColumns));
+ }
+
+ /**
+ * Clears the included columns.
+ */
+ public function clearIncludeColumns()
+ {
+ $this->includeColumns = array();
+ }
+
+ /**
+ * Sets the columns to exclude from the table.
+ * @param Array $excludeColumns
+ */
+ public function addExcludeColumns(Array $excludeColumns)
+ {
+ $this->excludeColumns = array_unique(array_merge($this->excludeColumns, $excludeColumns));
+ }
+
+ /**
+ * Clears the excluded columns.
+ */
+ public function clearExcludeColumns()
+ {
+ $this->excludeColumns = array();
+ }
}
?>
View
2  PHPUnit/Extensions/Database/TestCase.php
@@ -202,6 +202,8 @@ protected function setUp()
{
parent::setUp();
+ $this->databaseTester = NULL;
+
$this->getDatabaseTester()->setSetUpOperation($this->getSetUpOperation());
$this->getDatabaseTester()->setDataSet($this->getDataSet());
$this->getDatabaseTester()->onSetUp();
View
52 PHPUnit/Tests/Extensions/Database/DataSet/FilterTest.php
@@ -70,7 +70,7 @@ public function setUp()
);
}
- public function testFilteredDataSet()
+ public function testDeprecatedFilteredDataSetConstructor()
{
$constraint = new PHPUnit_Extensions_Database_Constraint_DataSetIsEqual($this->expectedDataSet);
$dataSet = new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(
@@ -80,10 +80,58 @@ public function testFilteredDataSet()
$filteredDataSet = new PHPUnit_Extensions_Database_DataSet_DataSetFilter($dataSet, array(
'table1' => array('table1_id'),
'table2' => '*',
- 'table3' => array('table3_id')
+ 'table3' => 'table3_id'
));
self::assertThat($filteredDataSet, $constraint);
}
+
+ public function testExcludeFilteredDataSet()
+ {
+ $constraint = new PHPUnit_Extensions_Database_Constraint_DataSetIsEqual($this->expectedDataSet);
+ $dataSet = new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(
+ dirname(__FILE__).'/../_files/XmlDataSets/FilteredTestComparison.xml'
+ );
+
+ $filteredDataSet = new PHPUnit_Extensions_Database_DataSet_DataSetFilter($dataSet);
+
+ $filteredDataSet->addExcludeTables(array('table2'));
+ $filteredDataSet->setExcludeColumnsForTable('table1', array('table1_id'));
+ $filteredDataSet->setExcludeColumnsForTable('table3', array('table3_id'));
+
+ self::assertThat($filteredDataSet, $constraint);
+ }
+
+ public function testIncludeFilteredDataSet()
+ {
+ $constraint = new PHPUnit_Extensions_Database_Constraint_DataSetIsEqual($this->expectedDataSet);
+ $dataSet = new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(
+ dirname(__FILE__).'/../_files/XmlDataSets/FilteredTestComparison.xml'
+ );
+
+ $filteredDataSet = new PHPUnit_Extensions_Database_DataSet_DataSetFilter($dataSet);
+
+ $filteredDataSet->addIncludeTables(array('table1', 'table3'));
+ $filteredDataSet->setIncludeColumnsForTable('table1', array('column1', 'column2', 'column3', 'column4'));
+ $filteredDataSet->setIncludeColumnsForTable('table3', array('column9', 'column10', 'column11', 'column12'));
+
+ self::assertThat($filteredDataSet, $constraint);
+ }
+
+ public function testIncludeExcludeMixedDataSet()
+ {
+ $constraint = new PHPUnit_Extensions_Database_Constraint_DataSetIsEqual($this->expectedDataSet);
+ $dataSet = new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(
+ dirname(__FILE__).'/../_files/XmlDataSets/FilteredTestComparison.xml'
+ );
+
+ $filteredDataSet = new PHPUnit_Extensions_Database_DataSet_DataSetFilter($dataSet);
+
+ $filteredDataSet->addIncludeTables(array('table1', 'table3'));
+ $filteredDataSet->setExcludeColumnsForTable('table1', array('table1_id'));
+ $filteredDataSet->setIncludeColumnsForTable('table3', array('column9', 'column10', 'column11', 'column12'));
+
+ self::assertThat($filteredDataSet, $constraint);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.