Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Commit

Permalink
Merge pull request #155 from richardbrinkman/master
Browse files Browse the repository at this point in the history
Added the ArrayDataSet as mentioned in the documentation.
  • Loading branch information
elazar committed Mar 29, 2015
2 parents 0a0e8f8 + 42ab777 commit 1507040
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
76 changes: 76 additions & 0 deletions PHPUnit/Extensions/Database/DataSet/ArrayDataSet.php
@@ -0,0 +1,76 @@
<?php
/*
* This file is part of DBUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Implements the basic functionality of data sets using a PHP array.
*
* @package DbUnit
* @author Richard Brinkman <richardbrinkman@hotmail.com>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @version Release: @package_version@
* @link http://www.phpunit.de/
* @since Class available since Release 1.3.2
*/
class PHPUnit_Extensions_Database_DataSet_ArrayDataSet extends PHPUnit_Extensions_Database_DataSet_AbstractDataSet
{
/**
* @var array
*/
protected $tables = array();

/**
* Constructor to build a new ArrayDataSet with the given array.
* The array parameter is an associative array of tables where the key is
* the table name and the value an array of rows. Each row is an associative
* array by itself with keys representing the field names and the values the
* actual data.
* For example:
* array(
* "addressbook" => array(
* array("id" => 1, "name" => "...", "address" => "..."),
* array("id" => 2, "name" => "...", "address" => "...")
* )
* )
*
* @param array $data
*/
public function __construct(array $data)
{
foreach ($data AS $tableName => $rows) {
$columns = array();
if (isset($rows[0])) {
$columns = array_keys($rows[0]);
}

$metaData = new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData($tableName, $columns);
$table = new PHPUnit_Extensions_Database_DataSet_DefaultTable($metaData);

foreach ($rows AS $row) {
$table->addRow($row);
}
$this->tables[$tableName] = $table;
}
}

protected function createIterator($reverse = FALSE)
{
return new PHPUnit_Extensions_Database_DataSet_DefaultTableIterator($this->tables, $reverse);
}

public function getTable($tableName)
{
if (!isset($this->tables[$tableName])) {
throw new InvalidArgumentException("$tableName is not a table in the current database.");
}

return $this->tables[$tableName];
}
}
?>
22 changes: 22 additions & 0 deletions PHPUnit/Extensions/Database/TestCase.php
Expand Up @@ -111,6 +111,28 @@ protected function createDefaultDBConnection(PDO $connection, $schema = '')
return new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection($connection, $schema);
}

/**
* Creates a new ArrayDataSet with the given array.
* The array parameter is an associative array of tables where the key is
* the table name and the value an array of rows. Each row is an associative
* array by itself with keys representing the field names and the values the
* actual data.
* For example:
* array(
* "addressbook" => array(
* array("id" => 1, "name" => "...", "address" => "..."),
* array("id" => 2, "name" => "...", "address" => "...")
* )
* )
*
* @param array $data
* @return PHPUnit_Extensions_Database_DataSet_ArrayDataSet
*/
protected function createArrayDataSet(array $data)
{
return new PHPUnit_Extensions_Database_DataSet_ArrayDataSet($data);
}

/**
* Creates a new FlatXmlDataSet with the given $xmlFile. (absolute path.)
*
Expand Down

0 comments on commit 1507040

Please sign in to comment.